Skip to content

Commit 6288a72

Browse files
Delay span bug when Self resolves to DefKind::{Mod,Trait}
1 parent ccfcd95 commit 6288a72

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
@@ -1879,6 +1879,17 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
18791879
self.set_tainted_by_errors(e);
18801880
Ty::new_error(self.tcx(), e)
18811881
}
1882+
Res::Def(..) => {
1883+
assert_eq!(
1884+
path.segments.get(0).map(|seg| seg.ident.name),
1885+
Some(kw::SelfUpper),
1886+
"only expected incorrect resolution for `Self`"
1887+
);
1888+
Ty::new_error(
1889+
self.tcx(),
1890+
self.tcx().dcx().span_delayed_bug(span, "incorrect resolution for `Self`"),
1891+
)
1892+
}
18821893
_ => span_bug!(span, "unexpected resolution: {:?}", path.res),
18831894
}
18841895
}
+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)