Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate resolution for SelfCtor too. #111020

Merged
merged 1 commit into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {

let sm = self.tcx.sess.source_map();
let def_id = match outer_res {
Res::SelfTyParam { .. } => {
Res::SelfTyParam { .. } | Res::SelfCtor(_) => {
err.span_label(span, "can't use `Self` here");
return err;
}
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_resolve/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1171,7 +1171,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
return Res::Err;
}
}
Res::Def(DefKind::TyParam, _) | Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } => {
Res::Def(DefKind::TyParam, _)
| Res::SelfTyParam { .. }
| Res::SelfTyAlias { .. }
| Res::SelfCtor(_) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a strange place to report this error because neither SelfTyAlias nor SelfCtor have anything to do with type parameters, but seems fine in the meantime.

for rib in ribs {
let has_generic_params: HasGenericParams = match rib.kind {
NormalRibKind
Expand Down
17 changes: 17 additions & 0 deletions tests/ui/self/self-ctor-inner-const.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Verify that we ban usage of `Self` as constructor from inner items.

struct S0<T>(T);

impl<T> S0<T> {
fn foo() {
const C: S0<u8> = Self(0);
//~^ ERROR can't use generic parameters from outer function
fn bar() -> Self {
//~^ ERROR can't use generic parameters from outer function
Self(0)
//~^ ERROR can't use generic parameters from outer function
}
}
}

fn main() {}
33 changes: 33 additions & 0 deletions tests/ui/self/self-ctor-inner-const.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
error[E0401]: can't use generic parameters from outer function
--> $DIR/self-ctor-inner-const.rs:7:27
|
LL | const C: S0<u8> = Self(0);
| ^^^^
| |
| use of generic parameter from outer function
| can't use `Self` here

error[E0401]: can't use generic parameters from outer function
--> $DIR/self-ctor-inner-const.rs:9:21
|
LL | impl<T> S0<T> {
| ---- `Self` type implicitly declared here, by this `impl`
...
LL | fn bar() -> Self {
| ^^^^
| |
| use of generic parameter from outer function
| use a type here instead

error[E0401]: can't use generic parameters from outer function
--> $DIR/self-ctor-inner-const.rs:11:13
|
LL | Self(0)
| ^^^^
| |
| use of generic parameter from outer function
| can't use `Self` here

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0401`.