-
Notifications
You must be signed in to change notification settings - Fork 13.4k
remove sub_relations
from the InferCtxt
#119989
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
91535ad
remove `sub_relations` from infcx, recompute in diagnostics
lcnr f392a87
freshen: resolve root vars
lcnr f7cdff8
overflow errors: change source to a concrete enum
lcnr 49dc0f2
do not use <: in subtyping overflow msg
lcnr c71484e
change error messages to be incorrect, but more helpful
lcnr 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
81 changes: 81 additions & 0 deletions
81
compiler/rustc_infer/src/infer/error_reporting/sub_relations.rs
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use rustc_data_structures::fx::FxHashMap; | ||
use rustc_data_structures::undo_log::NoUndo; | ||
use rustc_data_structures::unify as ut; | ||
use rustc_middle::ty; | ||
|
||
use crate::infer::InferCtxt; | ||
|
||
#[derive(Debug, Copy, Clone, PartialEq)] | ||
struct SubId(u32); | ||
impl ut::UnifyKey for SubId { | ||
type Value = (); | ||
#[inline] | ||
fn index(&self) -> u32 { | ||
self.0 | ||
} | ||
#[inline] | ||
fn from_index(i: u32) -> SubId { | ||
SubId(i) | ||
} | ||
fn tag() -> &'static str { | ||
"SubId" | ||
} | ||
} | ||
|
||
/// When reporting ambiguity errors, we sometimes want to | ||
/// treat all inference vars which are subtypes of each | ||
/// others as if they are equal. For this case we compute | ||
/// the transitive closure of our subtype obligations here. | ||
/// | ||
/// E.g. when encountering ambiguity errors, we want to suggest | ||
/// specifying some method argument or to add a type annotation | ||
/// to a local variable. Because subtyping cannot change the | ||
/// shape of a type, it's fine if the cause of the ambiguity error | ||
/// is only related to the suggested variable via subtyping. | ||
/// | ||
/// Even for something like `let x = returns_arg(); x.method();` the | ||
/// type of `x` is only a supertype of the argument of `returns_arg`. We | ||
/// still want to suggest specifying the type of the argument. | ||
#[derive(Default)] | ||
pub struct SubRelations { | ||
map: FxHashMap<ty::TyVid, SubId>, | ||
table: ut::UnificationTableStorage<SubId>, | ||
} | ||
|
||
impl SubRelations { | ||
fn get_id<'tcx>(&mut self, infcx: &InferCtxt<'tcx>, vid: ty::TyVid) -> SubId { | ||
let root_vid = infcx.root_var(vid); | ||
*self.map.entry(root_vid).or_insert_with(|| self.table.with_log(&mut NoUndo).new_key(())) | ||
} | ||
|
||
pub fn add_constraints<'tcx>( | ||
&mut self, | ||
infcx: &InferCtxt<'tcx>, | ||
obls: impl IntoIterator<Item = ty::Predicate<'tcx>>, | ||
) { | ||
for p in obls { | ||
let (a, b) = match p.kind().skip_binder() { | ||
ty::PredicateKind::Subtype(ty::SubtypePredicate { a_is_expected: _, a, b }) => { | ||
(a, b) | ||
} | ||
ty::PredicateKind::Coerce(ty::CoercePredicate { a, b }) => (a, b), | ||
_ => continue, | ||
}; | ||
|
||
match (a.kind(), b.kind()) { | ||
(&ty::Infer(ty::TyVar(a_vid)), &ty::Infer(ty::TyVar(b_vid))) => { | ||
let a = self.get_id(infcx, a_vid); | ||
let b = self.get_id(infcx, b_vid); | ||
self.table.with_log(&mut NoUndo).unify_var_var(a, b).unwrap(); | ||
} | ||
_ => continue, | ||
} | ||
} | ||
} | ||
|
||
pub fn unified<'tcx>(&mut self, infcx: &InferCtxt<'tcx>, a: ty::TyVid, b: ty::TyVid) -> bool { | ||
let a = self.get_id(infcx, a); | ||
let b = self.get_id(infcx, b); | ||
self.table.with_log(&mut NoUndo).unioned(a, b) | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.