Skip to content

Commit 90af17d

Browse files
authored
Rollup merge of #123997 - compiler-errors:self-res, r=fmease
Delay span bug when `Self` kw resolves to `DefKind::{Mod,Trait}` Catch the case where `kw::Self` is recovered in the parser and causes us to subsequently resolve `&self`'s implicit type to something that's not a type. This check could be made more accurate, though I'm not sure how hard we have to try here. Fixes #123988
2 parents 8229a34 + 6288a72 commit 90af17d

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1866,6 +1866,17 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
18661866
self.set_tainted_by_errors(e);
18671867
Ty::new_error(self.tcx(), e)
18681868
}
1869+
Res::Def(..) => {
1870+
assert_eq!(
1871+
path.segments.get(0).map(|seg| seg.ident.name),
1872+
Some(kw::SelfUpper),
1873+
"only expected incorrect resolution for `Self`"
1874+
);
1875+
Ty::new_error(
1876+
self.tcx(),
1877+
self.tcx().dcx().span_delayed_bug(span, "incorrect resolution for `Self`"),
1878+
)
1879+
}
18691880
_ => span_bug!(span, "unexpected resolution: {:?}", path.res),
18701881
}
18711882
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
fn module() {
2+
fn test(&mut self) {
3+
//~^ ERROR `self` parameter is only allowed in associated functions
4+
}
5+
mod Self {}
6+
//~^ ERROR expected identifier, found keyword `Self`
7+
}
8+
9+
fn trait_() {
10+
fn test(&mut self) {
11+
//~^ ERROR `self` parameter is only allowed in associated functions
12+
}
13+
trait Self {}
14+
//~^ ERROR expected identifier, found keyword `Self`
15+
}
16+
17+
fn main() {}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
error: expected identifier, found keyword `Self`
2+
--> $DIR/incorrect-self-res.rs:5:9
3+
|
4+
LL | mod Self {}
5+
| ^^^^ expected identifier, found keyword
6+
7+
error: expected identifier, found keyword `Self`
8+
--> $DIR/incorrect-self-res.rs:13:11
9+
|
10+
LL | trait Self {}
11+
| ^^^^ expected identifier, found keyword
12+
13+
error: `self` parameter is only allowed in associated functions
14+
--> $DIR/incorrect-self-res.rs:2:13
15+
|
16+
LL | fn test(&mut self) {
17+
| ^^^^^^^^^ not semantically valid as function parameter
18+
|
19+
= note: associated functions are those in `impl` or `trait` definitions
20+
21+
error: `self` parameter is only allowed in associated functions
22+
--> $DIR/incorrect-self-res.rs:10:13
23+
|
24+
LL | fn test(&mut self) {
25+
| ^^^^^^^^^ not semantically valid as function parameter
26+
|
27+
= note: associated functions are those in `impl` or `trait` definitions
28+
29+
error: aborting due to 4 previous errors
30+

0 commit comments

Comments
 (0)