From 6288a721f5ea59a59b4304a7b70c92d7755e8aa8 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Mon, 15 Apr 2024 21:00:49 -0400 Subject: [PATCH] Delay span bug when Self resolves to DefKind::{Mod,Trait} --- .../src/hir_ty_lowering/mod.rs | 11 +++++++ tests/ui/resolve/incorrect-self-res.rs | 17 +++++++++++ tests/ui/resolve/incorrect-self-res.stderr | 30 +++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 tests/ui/resolve/incorrect-self-res.rs create mode 100644 tests/ui/resolve/incorrect-self-res.stderr diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs index 63aeb165a48..b15bf54234d 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs @@ -1879,6 +1879,17 @@ pub fn lower_path( self.set_tainted_by_errors(e); Ty::new_error(self.tcx(), e) } + Res::Def(..) => { + assert_eq!( + path.segments.get(0).map(|seg| seg.ident.name), + Some(kw::SelfUpper), + "only expected incorrect resolution for `Self`" + ); + Ty::new_error( + self.tcx(), + self.tcx().dcx().span_delayed_bug(span, "incorrect resolution for `Self`"), + ) + } _ => span_bug!(span, "unexpected resolution: {:?}", path.res), } } diff --git a/tests/ui/resolve/incorrect-self-res.rs b/tests/ui/resolve/incorrect-self-res.rs new file mode 100644 index 00000000000..ca97e698994 --- /dev/null +++ b/tests/ui/resolve/incorrect-self-res.rs @@ -0,0 +1,17 @@ +fn module() { + fn test(&mut self) { + //~^ ERROR `self` parameter is only allowed in associated functions + } + mod Self {} + //~^ ERROR expected identifier, found keyword `Self` +} + +fn trait_() { + fn test(&mut self) { + //~^ ERROR `self` parameter is only allowed in associated functions + } + trait Self {} + //~^ ERROR expected identifier, found keyword `Self` +} + +fn main() {} diff --git a/tests/ui/resolve/incorrect-self-res.stderr b/tests/ui/resolve/incorrect-self-res.stderr new file mode 100644 index 00000000000..406bfb98011 --- /dev/null +++ b/tests/ui/resolve/incorrect-self-res.stderr @@ -0,0 +1,30 @@ +error: expected identifier, found keyword `Self` + --> $DIR/incorrect-self-res.rs:5:9 + | +LL | mod Self {} + | ^^^^ expected identifier, found keyword + +error: expected identifier, found keyword `Self` + --> $DIR/incorrect-self-res.rs:13:11 + | +LL | trait Self {} + | ^^^^ expected identifier, found keyword + +error: `self` parameter is only allowed in associated functions + --> $DIR/incorrect-self-res.rs:2:13 + | +LL | fn test(&mut self) { + | ^^^^^^^^^ not semantically valid as function parameter + | + = note: associated functions are those in `impl` or `trait` definitions + +error: `self` parameter is only allowed in associated functions + --> $DIR/incorrect-self-res.rs:10:13 + | +LL | fn test(&mut self) { + | ^^^^^^^^^ not semantically valid as function parameter + | + = note: associated functions are those in `impl` or `trait` definitions + +error: aborting due to 4 previous errors +