Skip to content

Commit 8345340

Browse files
committed
Validate resolution for SelfCtor too.
1 parent 89158e2 commit 8345340

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
550550

551551
let sm = self.tcx.sess.source_map();
552552
let def_id = match outer_res {
553-
Res::SelfTyParam { .. } => {
553+
Res::SelfTyParam { .. } | Res::SelfCtor(_) => {
554554
err.span_label(span, "can't use `Self` here");
555555
return err;
556556
}

compiler/rustc_resolve/src/ident.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
11711171
return Res::Err;
11721172
}
11731173
}
1174-
Res::Def(DefKind::TyParam, _) | Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } => {
1174+
Res::Def(DefKind::TyParam, _)
1175+
| Res::SelfTyParam { .. }
1176+
| Res::SelfTyAlias { .. }
1177+
| Res::SelfCtor(_) => {
11751178
for rib in ribs {
11761179
let has_generic_params: HasGenericParams = match rib.kind {
11771180
NormalRibKind
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Verify that we ban usage of `Self` as constructor from inner items.
2+
3+
struct S0<T>(T);
4+
5+
impl<T> S0<T> {
6+
fn foo() {
7+
const C: S0<u8> = Self(0);
8+
//~^ ERROR can't use generic parameters from outer function
9+
fn bar() -> Self {
10+
//~^ ERROR can't use generic parameters from outer function
11+
Self(0)
12+
//~^ ERROR can't use generic parameters from outer function
13+
}
14+
}
15+
}
16+
17+
fn main() {}
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
error[E0401]: can't use generic parameters from outer function
2+
--> $DIR/self-ctor-inner-const.rs:7:27
3+
|
4+
LL | const C: S0<u8> = Self(0);
5+
| ^^^^
6+
| |
7+
| use of generic parameter from outer function
8+
| can't use `Self` here
9+
10+
error[E0401]: can't use generic parameters from outer function
11+
--> $DIR/self-ctor-inner-const.rs:9:21
12+
|
13+
LL | impl<T> S0<T> {
14+
| ---- `Self` type implicitly declared here, by this `impl`
15+
...
16+
LL | fn bar() -> Self {
17+
| ^^^^
18+
| |
19+
| use of generic parameter from outer function
20+
| use a type here instead
21+
22+
error[E0401]: can't use generic parameters from outer function
23+
--> $DIR/self-ctor-inner-const.rs:11:13
24+
|
25+
LL | Self(0)
26+
| ^^^^
27+
| |
28+
| use of generic parameter from outer function
29+
| can't use `Self` here
30+
31+
error: aborting due to 3 previous errors
32+
33+
For more information about this error, try `rustc --explain E0401`.

0 commit comments

Comments
 (0)