-
-
Notifications
You must be signed in to change notification settings - Fork 14.5k
Pull implied bound computation out of borrowck #152051
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
Draft
tiif
wants to merge
9
commits into
rust-lang:main
Choose a base branch
from
tiif:implied-bound-opaque
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+562
−92
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5943c60
Add test
tiif 4249a9b
Strip implied bound computation from borrowck
tiif 24ccc9e
Fix some error handling, and the new test passed
tiif f290a2e
Replace erased regions with existential variables
tiif c0822f3
More error handling
tiif 44f6dec
stderr for the newly added test
tiif fbe4e14
More clean up
tiif 6895fd4
debug: add bunch of assert
tiif ae4c5af
remove the params getting from closure and other stuff
tiif 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| // TODO: add description later | ||
|
|
||
| use rustc_infer::infer::canonical::Canonical; | ||
| use rustc_infer::infer::{TyCtxtInferExt, canonical}; | ||
| use rustc_infer::traits::ObligationCause; | ||
| use rustc_infer::traits::query::OutlivesBound; | ||
| use rustc_middle::query::Providers; | ||
| use rustc_middle::ty::TyCtxt; | ||
| use rustc_trait_selection::traits::ObligationCtxt; | ||
| use rustc_trait_selection::traits::query::type_op::implied_outlives_bounds::compute_implied_outlives_bounds_inner; | ||
|
|
||
| use crate::hir::def::DefKind; | ||
| use crate::ty::solve::NoSolution; | ||
| use crate::ty::{CanonicalVarValues, GenericArg, GenericArgs}; | ||
| use crate::universal_regions::{compute_inputs_and_output_non_nll, defining_ty_non_nll}; | ||
| use crate::{LocalDefId, ParamEnv, RegionVariableOrigin, Ty, TypingMode, fold_regions, ty}; | ||
|
|
||
| pub(crate) fn provide(p: &mut Providers) { | ||
| *p = Providers { compute_outlives_bounds_rename, ..*p }; | ||
| } | ||
|
|
||
| fn compute_outlives_bounds_rename<'tcx>( | ||
| tcx: TyCtxt<'tcx>, | ||
| mir_def: LocalDefId, | ||
| ) -> Result< | ||
| &'tcx Canonical<'tcx, canonical::QueryResponse<'tcx, Vec<OutlivesBound<'tcx>>>>, | ||
| NoSolution, | ||
| > { | ||
| let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis()); | ||
| let ocx = ObligationCtxt::new(&infcx); | ||
| let param_env = ParamEnv::empty(); | ||
| let defining_ty = defining_ty_non_nll(&infcx, mir_def); | ||
| let defining_ty_def_id = defining_ty.def_id().expect_local(); | ||
|
|
||
| let unnormalized_input_output_tys = | ||
| compute_inputs_and_output_non_nll(&infcx, mir_def, defining_ty); | ||
| let unnormalized_input_output_tys = tcx | ||
| .liberate_late_bound_regions(defining_ty_def_id.to_def_id(), unnormalized_input_output_tys); | ||
|
|
||
| let span = tcx.def_span(defining_ty_def_id); | ||
| let mut outlives_bounds: Vec<OutlivesBound<'tcx>> = vec![]; | ||
| let mut norm_sig_tys: Vec<Ty<'_>> = vec![]; | ||
|
|
||
| for ty in unnormalized_input_output_tys { | ||
| // Replace erased regions with fresh region variables. | ||
| let ty = fold_regions(tcx, ty, |re, _dbi| match re.kind() { | ||
| ty::ReErased => infcx.next_region_var(RegionVariableOrigin::Misc(span)), | ||
| _ => re, | ||
| }); | ||
|
|
||
| // We add implied bounds from both the unnormalized and normalized ty. | ||
| // See issue #87748 | ||
| if let Ok(bounds) = compute_implied_outlives_bounds_inner(&ocx, param_env, ty, span, false) | ||
| { | ||
| outlives_bounds.extend(bounds); | ||
| } | ||
|
|
||
| let Ok(norm_ty) = | ||
| ocx.deeply_normalize(&ObligationCause::dummy_with_span(span), param_env, ty) | ||
| else { | ||
| // Even if deeply normalize returns no solution, we still need to store the ty for canonicalization later. | ||
| norm_sig_tys.push(ty); | ||
| continue; | ||
| }; | ||
|
|
||
| // Currently `implied_outlives_bounds` will normalize the provided | ||
| // `Ty`, despite this it's still important to normalize the ty ourselves | ||
| // as normalization may introduce new region variables (#136547). | ||
| // | ||
| // If we do not add implied bounds for the type involving these new | ||
| // region variables then we'll wind up with the normalized form of | ||
| // the signature having not-wf types due to unsatisfied region | ||
| // constraints. | ||
| // | ||
| // Note: we need this in examples like | ||
| // ``` | ||
| // trait Foo { | ||
| // type Bar; | ||
| // fn foo(&self) -> &Self::Bar; | ||
| // } | ||
| // impl Foo for () { | ||
| // type Bar = (); | ||
| // fn foo(&self) -> &() {} | ||
| // } | ||
| // ``` | ||
| // Both &Self::Bar and &() are WF | ||
| if ty != norm_ty { | ||
| if let Ok(bounds) = | ||
| compute_implied_outlives_bounds_inner(&ocx, param_env, norm_ty, span, false) | ||
| { | ||
| outlives_bounds.extend(bounds); | ||
|
Comment on lines
+87
to
+91
Member
Author
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. and this is the second one. |
||
| } | ||
| } | ||
|
|
||
| norm_sig_tys.push(norm_ty); | ||
| } | ||
|
|
||
| // Add implied bounds from impl header. | ||
| // | ||
| // We don't use `assumed_wf_types` to source the entire set of implied bounds for | ||
| // a few reasons: | ||
| // - `DefiningTy` for closure has the `&'env Self` type while `assumed_wf_types` doesn't | ||
| // - We compute implied bounds from the unnormalized types in the `DefiningTy` but do not | ||
| // do so for types in impl headers | ||
| // - We must compute the normalized signature and then compute implied bounds from that | ||
| // in order to connect any unconstrained region vars created during normalization to | ||
| // the types of the locals corresponding to the inputs and outputs of the item. (#136547) | ||
| if matches!(tcx.def_kind(defining_ty_def_id), DefKind::AssocFn | DefKind::AssocConst) { | ||
| for &(ty, _) in tcx.assumed_wf_types(tcx.local_parent(defining_ty_def_id)) { | ||
| // Replace erased regions with fresh region variables. | ||
| let ty = fold_regions(tcx, ty, |re, _dbi| match re.kind() { | ||
| ty::ReErased => infcx.next_region_var(RegionVariableOrigin::Misc(span)), | ||
| _ => re, | ||
| }); | ||
|
|
||
| let Ok(norm_ty) = | ||
| ocx.deeply_normalize(&ObligationCause::dummy_with_span(span), param_env, ty) | ||
| else { | ||
| continue; | ||
| }; | ||
|
|
||
| // We currently add implied bounds from the normalized ty only. | ||
| // This is more conservative and matches wfcheck behavior. | ||
| if let Ok(bounds) = | ||
| compute_implied_outlives_bounds_inner(&ocx, param_env, norm_ty, span, false) | ||
| { | ||
| outlives_bounds.extend(bounds); | ||
| } | ||
| } | ||
| } | ||
| // Get early and late bound params. | ||
| let typeck_root_def_id = tcx.typeck_root_def_id(mir_def.to_def_id()); | ||
| let params = GenericArgs::identity_for_item(tcx, typeck_root_def_id); | ||
|
|
||
| let var_value = tcx.mk_args_from_iter( | ||
| params.iter().chain(norm_sig_tys.iter().map(|ty| GenericArg::from(*ty))), | ||
| ); | ||
|
|
||
| let var_values: CanonicalVarValues<TyCtxt<'_>> = | ||
| CanonicalVarValues { var_values: tcx.mk_args(var_value) }; | ||
|
|
||
| ocx.make_canonicalized_query_response(var_values, outlives_bounds) | ||
| } | ||
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
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.
we have a first implied bound computation call here