-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
better errors when resolving bad Self in impl block #93971
Closed
compiler-errors
wants to merge
1
commit into
rust-lang:master
from
compiler-errors:better-self-errors
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ use rustc_ast_lowering::ResolverAstLowering; | |
use rustc_data_structures::fx::{FxHashMap, FxHashSet}; | ||
use rustc_errors::DiagnosticId; | ||
use rustc_hir::def::Namespace::{self, *}; | ||
use rustc_hir::def::{self, CtorKind, DefKind, PartialRes, PerNS}; | ||
use rustc_hir::def::{self, CtorKind, DefKind, PartialRes, PerNS, ResImpl}; | ||
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX}; | ||
use rustc_hir::{PrimTy, TraitCandidate}; | ||
use rustc_middle::{bug, span_bug}; | ||
|
@@ -911,9 +911,19 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { | |
self.with_current_self_item(item, |this| { | ||
this.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| { | ||
let item_def_id = this.r.local_def_id(item.id).to_def_id(); | ||
this.with_self_rib(Res::SelfTy(None, Some((item_def_id, false))), |this| { | ||
visit::walk_item(this, item); | ||
}); | ||
this.with_self_rib( | ||
Res::SelfTy( | ||
None, | ||
Some(ResImpl { | ||
def_id: item_def_id, | ||
generics_allowed: true, | ||
in_trait_ref: false, | ||
}), | ||
), | ||
|this| { | ||
visit::walk_item(this, item); | ||
}, | ||
); | ||
}); | ||
}); | ||
} | ||
|
@@ -1211,6 +1221,14 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { | |
result | ||
} | ||
|
||
fn with_no_self_type<T>(&mut self, f: impl FnOnce(&mut Self) -> T) -> T { | ||
// Handle nested impls (inside fn bodies) | ||
let previous_value = replace(&mut self.diagnostic_metadata.current_self_type, None); | ||
let result = f(self); | ||
self.diagnostic_metadata.current_self_type = previous_value; | ||
result | ||
} | ||
|
||
fn with_current_self_item<T>(&mut self, self_item: &Item, f: impl FnOnce(&mut Self) -> T) -> T { | ||
let previous_value = | ||
replace(&mut self.diagnostic_metadata.current_self_item, Some(self_item.id)); | ||
|
@@ -1295,30 +1313,52 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { | |
debug!("resolve_implementation"); | ||
// If applicable, create a rib for the type parameters. | ||
self.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| { | ||
// Dummy self type for better errors if `Self` is used in the trait path. | ||
this.with_self_rib(Res::SelfTy(None, None), |this| { | ||
// FIXME(compiler-errors): `Self` may still exist in the value namespace, | ||
// so we might be able to resolve an outer `Self` in, e.g., a const generic default. | ||
this.with_no_self_type(|this| { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this required? IIUC, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's probably not useful in that case. |
||
// Resolve the self type. | ||
this.visit_ty(self_type); | ||
}); | ||
let item_def_id = this.r.local_def_id(item_id); | ||
// Resolve the trait reference, if necessary. | ||
this.with_optional_trait_ref(opt_trait_reference.as_ref(), |this, trait_id| { | ||
let item_def_id = this.r.local_def_id(item_id); | ||
|
||
// Register the trait definitions from here. | ||
if let Some(trait_id) = trait_id { | ||
this.r.trait_impls.entry(trait_id).or_default().push(item_def_id); | ||
} | ||
|
||
let item_def_id = item_def_id.to_def_id(); | ||
this.with_self_rib(Res::SelfTy(trait_id, Some((item_def_id, false))), |this| { | ||
if let Some(trait_ref) = opt_trait_reference.as_ref() { | ||
// Resolve type arguments in the trait path. | ||
visit::walk_trait_ref(this, trait_ref); | ||
} | ||
// Resolve the self type. | ||
this.visit_ty(self_type); | ||
// Resolve the generic parameters. | ||
this.visit_generics(generics); | ||
// Resolve the items within the impl. | ||
this.with_current_self_type(self_type, |this| { | ||
this.with_self_rib_ns(ValueNS, Res::SelfCtor(item_def_id), |this| { | ||
if let Some(trait_ref) = opt_trait_reference.as_ref() { | ||
this.with_self_rib( | ||
Res::SelfTy( | ||
trait_id, | ||
Some(ResImpl { | ||
def_id: item_def_id, | ||
generics_allowed: true, | ||
in_trait_ref: true, | ||
}), | ||
), | ||
|this| { | ||
// Resolve type arguments in the trait path. | ||
visit::walk_trait_ref(this, trait_ref); | ||
}, | ||
); | ||
} | ||
this.with_self_rib( | ||
Res::SelfTy( | ||
trait_id, | ||
Some(ResImpl { | ||
def_id: item_def_id, | ||
generics_allowed: true, | ||
in_trait_ref: false, | ||
}), | ||
), | ||
|this| { | ||
// Resolve the generic parameters. | ||
this.visit_generics(generics); | ||
// Resolve the items within the impl. | ||
this.with_current_self_type(self_type, |this| { | ||
this.with_self_rib_ns(ValueNS, Res::SelfCtor(item_def_id), |this| { | ||
debug!("resolve_implementation with_self_rib_ns(ValueNS, ...)"); | ||
for item in impl_items { | ||
use crate::ResolutionError::*; | ||
|
@@ -1414,8 +1454,9 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { | |
} | ||
} | ||
}); | ||
}); | ||
}); | ||
}); | ||
}, | ||
); | ||
}); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,10 @@ | ||
error[E0391]: cycle detected when computing type of `<impl at $DIR/issue-23305.rs:5:1: 5:24>` | ||
error: `Self` is not allowed in the self type of an `impl` block | ||
--> $DIR/issue-23305.rs:5:16 | ||
| | ||
LL | impl dyn ToNbt<Self> {} | ||
| ^^^^ | ||
| | ||
= note: ...which immediately requires computing type of `<impl at $DIR/issue-23305.rs:5:1: 5:24>` again | ||
note: cycle used when collecting item types in top-level module | ||
--> $DIR/issue-23305.rs:1:1 | ||
| | ||
LL | pub trait ToNbt<T> { | ||
| ^^^^^^^^^^^^^^^^^^ | ||
= note: referencing `Self` in this position would produce an infinitely recursive type | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0391`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand where this flag is used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We specifically need to deny this:
But we should not deny this:
So we use this flag during astconv to catch cases (like the former) where we have an associated type with a
Self
in the path, which without this flag would try to elaborate the supertraits of the trait ref, looking for one with the associated type.Since resolving the trait ref would itself require resolving an associated type in the trait ref, we get a cycle error.