-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
implied bounds: normalize in the proper param_env #105982
Closed
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
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,34 +1,45 @@ | ||
use crate::rustc_middle::ty::DefIdTree; | ||
use rustc_hir::{def::DefKind, def_id::DefId}; | ||
use rustc_middle::ty::{self, Ty, TyCtxt}; | ||
use rustc_middle::traits::query::NoSolution; | ||
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable}; | ||
|
||
pub fn provide(providers: &mut ty::query::Providers) { | ||
*providers = ty::query::Providers { assumed_wf_types, ..*providers }; | ||
} | ||
|
||
fn assumed_wf_types<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &'tcx ty::List<Ty<'tcx>> { | ||
fn assumed_wf_types<'tcx>( | ||
tcx: TyCtxt<'tcx>, | ||
def_id: DefId, | ||
) -> ty::EarlyBinder<ty::Binder<'tcx, &'tcx ty::List<Ty<'tcx>>>> { | ||
match tcx.def_kind(def_id) { | ||
DefKind::Fn => { | ||
let sig = tcx.fn_sig(def_id); | ||
let liberated_sig = tcx.liberate_late_bound_regions(def_id, sig); | ||
liberated_sig.inputs_and_output | ||
} | ||
DefKind::Fn => ty::EarlyBinder(from_fn_sig(tcx, def_id)), | ||
DefKind::AssocFn => { | ||
let sig = tcx.fn_sig(def_id); | ||
let liberated_sig = tcx.liberate_late_bound_regions(def_id, sig); | ||
let mut assumed_wf_types: Vec<_> = | ||
tcx.assumed_wf_types(tcx.parent(def_id)).as_slice().into(); | ||
assumed_wf_types.extend(liberated_sig.inputs_and_output); | ||
tcx.intern_type_list(&assumed_wf_types) | ||
let from_sig = from_fn_sig(tcx, def_id); | ||
let from_impl = tcx.assumed_wf_types(tcx.parent(def_id)).subst_identity(); | ||
|
||
let assumed_wf_types = from_impl.no_bound_vars().unwrap().into_iter(); | ||
let assumed_wf_types = assumed_wf_types.chain(from_sig.skip_binder()); | ||
ty::EarlyBinder(from_sig.rebind(tcx.mk_type_list(assumed_wf_types))) | ||
} | ||
DefKind::Impl => { | ||
let unnormalized = match tcx.impl_trait_ref(def_id) { | ||
Some(trait_ref) => tcx.mk_type_list(trait_ref.substs.types()), | ||
// Only the impl self type | ||
None => tcx.intern_type_list(&[tcx.type_of(def_id)]), | ||
}; | ||
|
||
// FIXME(@lcnr): rustc currently does not check wf for types | ||
// pre-normalization, meaning that implied bounds from unnormalized types | ||
// are sometimes incorrect. See #100910 for more details. | ||
// | ||
// Not adding the unnormalized types here mostly fixes that, except | ||
// that there are projections which are still ambiguous in the item definition | ||
// but do normalize successfully when using the item, see #98543. | ||
let normalized = | ||
normalize_ignoring_obligations(tcx, tcx.param_env(def_id), unnormalized) | ||
.unwrap_or_else(|_| ty::List::empty()); | ||
ty::EarlyBinder(ty::Binder::dummy(normalized)) | ||
} | ||
DefKind::Impl => match tcx.impl_trait_ref(def_id) { | ||
Some(trait_ref) => { | ||
let types: Vec<_> = trait_ref.substs.types().collect(); | ||
tcx.intern_type_list(&types) | ||
} | ||
// Only the impl self type | ||
None => tcx.intern_type_list(&[tcx.type_of(def_id)]), | ||
}, | ||
DefKind::AssocConst | DefKind::AssocTy => tcx.assumed_wf_types(tcx.parent(def_id)), | ||
DefKind::Mod | ||
| DefKind::Struct | ||
|
@@ -56,6 +67,38 @@ fn assumed_wf_types<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &'tcx ty::List<Ty | |
| DefKind::LifetimeParam | ||
| DefKind::GlobalAsm | ||
| DefKind::Closure | ||
| DefKind::Generator => ty::List::empty(), | ||
| DefKind::Generator => ty::EarlyBinder(ty::Binder::dummy(ty::List::empty())), | ||
} | ||
} | ||
|
||
fn from_fn_sig<'tcx>( | ||
tcx: TyCtxt<'tcx>, | ||
def_id: DefId, | ||
) -> ty::Binder<'tcx, &'tcx ty::List<Ty<'tcx>>> { | ||
// FIXME(#84533): We probably shouldn't use output types in implied bounds. | ||
// This would reject this fn `fn f<'a, 'b>() -> &'a &'b () { .. }`. | ||
let unnormalized = tcx.fn_sig(def_id).map_bound(|sig| sig.inputs_and_output); | ||
let normalized = normalize_ignoring_obligations(tcx, tcx.param_env(def_id), unnormalized) | ||
.unwrap_or_else(|_| ty::Binder::dummy(ty::List::empty())); | ||
|
||
// FIXME(#105948): Use unnormalized types for implied bounds as well. | ||
normalized | ||
} | ||
|
||
fn normalize_ignoring_obligations<'tcx, T: TypeFoldable<'tcx>>( | ||
tcx: TyCtxt<'tcx>, | ||
param_env: ty::ParamEnv<'tcx>, | ||
value: T, | ||
) -> Result<T, NoSolution> { | ||
use rustc_infer::infer::TyCtxtInferExt as _; | ||
use rustc_infer::traits::ObligationCause; | ||
use rustc_trait_selection::traits::query::normalize::QueryNormalizeExt as _; | ||
|
||
let infcx = tcx.infer_ctxt().build(); | ||
let normalized = infcx | ||
.at(&ObligationCause::dummy(), param_env) | ||
.query_normalize(value) | ||
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.
During analysis |
||
.map(|normalized| infcx.resolve_vars_if_possible(normalized.value))?; | ||
assert!(!normalized.needs_infer()); | ||
Ok(normalized) | ||
} |
4 changes: 2 additions & 2 deletions
4
src/test/ui/associated-types/associated-types-for-unimpl-trait.stderr
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
4 changes: 2 additions & 2 deletions
4
src/test/ui/associated-types/associated-types-no-suitable-bound.stderr
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
4 changes: 2 additions & 2 deletions
4
src/test/ui/associated-types/associated-types-no-suitable-supertrait-2.stderr
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
8 changes: 4 additions & 4 deletions
8
src/test/ui/associated-types/associated-types-no-suitable-supertrait.stderr
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
4 changes: 2 additions & 2 deletions
4
...ted-types/associated-types-projection-to-unrelated-trait-in-method-without-default.stderr
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
8 changes: 4 additions & 4 deletions
8
src/test/ui/const-generics/generic_const_exprs/let-bindings.stderr
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
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 don't think this should be how we fix #84533 because you'd still have an builtin impl with an associated type which isn't well formed.
The correct fix is to actually check that the return type is well formed even without calling the function. This needs implications for binders though.
Until then something like #106807 might work as a stopgap (again having the issue of unnormalized projections though)