Rollup merge of #62417 - alexreg:fix-self-in-type-alias, r=pnkfelix

Fix ICEs when `Self` is used in type aliases

I think it is right just to disallow this at resolution stage rather than let typeck produce a cyclic error. This is in line with previous behaviour. There was probably no need at all for the change that introduced this bug in #57428, so I've simply reversed it.

Fixes #62263, #62364, #62305.

r? @eddyb
This commit is contained in:
Mazdak Farrokhzad 2019-07-09 21:01:48 +02:00 committed by GitHub
commit 2c2062e83b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 61 additions and 24 deletions

View File

@ -197,11 +197,11 @@ pub struct Compiler {
#[derive(PartialEq, Eq, Copy, Clone, Debug)] #[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub enum DocTests { pub enum DocTests {
// Default, run normal tests and doc tests. /// Run normal tests and doc tests (default).
Yes, Yes,
// Do not run any doc tests. /// Do not run any doc tests.
No, No,
// Only run doc tests. /// Only run doc tests.
Only, Only,
} }
@ -221,10 +221,10 @@ pub enum GitRepo {
/// methods specifically on this structure itself (to make it easier to /// methods specifically on this structure itself (to make it easier to
/// organize). /// organize).
pub struct Build { pub struct Build {
// User-specified configuration via config.toml /// User-specified configuration from `config.toml`.
config: Config, config: Config,
// Derived properties from the above two configurations // Properties derived from the above configuration
src: PathBuf, src: PathBuf,
out: PathBuf, out: PathBuf,
rust_info: channel::GitInfo, rust_info: channel::GitInfo,
@ -240,12 +240,12 @@ pub struct Build {
doc_tests: DocTests, doc_tests: DocTests,
verbosity: usize, verbosity: usize,
// Targets for which to build. // Targets for which to build
build: Interned<String>, build: Interned<String>,
hosts: Vec<Interned<String>>, hosts: Vec<Interned<String>>,
targets: Vec<Interned<String>>, targets: Vec<Interned<String>>,
// Stage 0 (downloaded) compiler and cargo or their local rust equivalents. // Stage 0 (downloaded) compiler and cargo or their local rust equivalents
initial_rustc: PathBuf, initial_rustc: PathBuf,
initial_cargo: PathBuf, initial_cargo: PathBuf,
@ -255,7 +255,7 @@ pub struct Build {
cxx: HashMap<Interned<String>, cc::Tool>, cxx: HashMap<Interned<String>, cc::Tool>,
ar: HashMap<Interned<String>, PathBuf>, ar: HashMap<Interned<String>, PathBuf>,
ranlib: HashMap<Interned<String>, PathBuf>, ranlib: HashMap<Interned<String>, PathBuf>,
// Misc // Miscellaneous
crates: HashMap<Interned<String>, Crate>, crates: HashMap<Interned<String>, Crate>,
is_sudo: bool, is_sudo: bool,
ci_env: CiEnv, ci_env: CiEnv,

View File

@ -2523,17 +2523,7 @@ impl<'a> Resolver<'a> {
debug!("(resolving item) resolving {} ({:?})", name, item.node); debug!("(resolving item) resolving {} ({:?})", name, item.node);
match item.node { match item.node {
ItemKind::Ty(_, ref generics) => { ItemKind::Ty(_, ref generics) |
self.with_current_self_item(item, |this| {
this.with_generic_param_rib(HasGenericParams(generics, ItemRibKind), |this| {
let item_def_id = this.definitions.local_def_id(item.id);
this.with_self_rib(Res::SelfTy(Some(item_def_id), None), |this| {
visit::walk_item(this, item)
})
})
});
}
ItemKind::Existential(_, ref generics) | ItemKind::Existential(_, ref generics) |
ItemKind::Fn(_, _, ref generics, _) => { ItemKind::Fn(_, _, ref generics, _) => {
self.with_generic_param_rib( self.with_generic_param_rib(

View File

@ -4457,7 +4457,7 @@ pub fn check_bounds_are_used<'tcx>(tcx: TyCtxt<'tcx>, generics: &ty::Generics, t
return; return;
} }
// Make a vector of booleans initially false, set to true when used. // Make a vector of booleans initially `false`; set to `true` when used.
let mut types_used = vec![false; own_counts.types]; let mut types_used = vec![false; own_counts.types];
for leaf_ty in ty.walk() { for leaf_ty in ty.walk() {
@ -4466,7 +4466,7 @@ pub fn check_bounds_are_used<'tcx>(tcx: TyCtxt<'tcx>, generics: &ty::Generics, t
types_used[index as usize - own_counts.lifetimes] = true; types_used[index as usize - own_counts.lifetimes] = true;
} else if let ty::Error = leaf_ty.sty { } else if let ty::Error = leaf_ty.sty {
// If there is already another error, do not emit // If there is already another error, do not emit
// an error for not using a type Parameter. // an error for not using a type parameter.
assert!(tcx.sess.has_errors()); assert!(tcx.sess.has_errors());
return; return;
} }

View File

@ -1,17 +1,17 @@
error: only `u8` can be cast into `char` error: only `u8` can be cast into `char`
--> $DIR/cast_char.rs:4:23 --> $DIR/cast-char.rs:4:23
| |
LL | const XYZ: char = 0x1F888 as char; LL | const XYZ: char = 0x1F888 as char;
| ^^^^^^^^^^^^^^^ help: use a `char` literal instead: `'\u{1F888}'` | ^^^^^^^^^^^^^^^ help: use a `char` literal instead: `'\u{1F888}'`
| |
note: lint level defined here note: lint level defined here
--> $DIR/cast_char.rs:1:9 --> $DIR/cast-char.rs:1:9
| |
LL | #![deny(overflowing_literals)] LL | #![deny(overflowing_literals)]
| ^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^
error: only `u8` can be cast into `char` error: only `u8` can be cast into `char`
--> $DIR/cast_char.rs:6:22 --> $DIR/cast-char.rs:6:22
| |
LL | const XY: char = 129160 as char; LL | const XY: char = 129160 as char;
| ^^^^^^^^^^^^^^ help: use a `char` literal instead: `'\u{1F888}'` | ^^^^^^^^^^^^^^ help: use a `char` literal instead: `'\u{1F888}'`

View File

@ -0,0 +1,8 @@
pub trait Trait {
type A;
}
pub type Alias = dyn Trait<A = Self::A>;
//~^ ERROR failed to resolve: use of undeclared type or module `Self` [E0433]
fn main() {}

View File

@ -0,0 +1,9 @@
error[E0433]: failed to resolve: use of undeclared type or module `Self`
--> $DIR/issue-62263-self-in-atb.rs:5:32
|
LL | pub type Alias = dyn Trait<A = Self::A>;
| ^^^^ use of undeclared type or module `Self`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0433`.

View File

@ -0,0 +1,4 @@
type Alias = Self::Target;
//~^ ERROR failed to resolve: use of undeclared type or module `Self` [E0433]
fn main() {}

View File

@ -0,0 +1,9 @@
error[E0433]: failed to resolve: use of undeclared type or module `Self`
--> $DIR/issue-62305-self-assoc-ty.rs:1:14
|
LL | type Alias = Self::Target;
| ^^^^ use of undeclared type or module `Self`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0433`.

View File

@ -0,0 +1,8 @@
struct Struct<P1> {
field: P1,
}
type Alias<'a> = Struct<&'a Self>;
//~^ ERROR cannot find type `Self` in this scope [E0411]
fn main() {}

View File

@ -0,0 +1,9 @@
error[E0411]: cannot find type `Self` in this scope
--> $DIR/issue-62364-self-ty-arg.rs:5:29
|
LL | type Alias<'a> = Struct<&'a Self>;
| ^^^^ `Self` is only available in impls, traits, and type definitions
error: aborting due to previous error
For more information about this error, try `rustc --explain E0411`.