Commit 0e0dac2

Browse files
authored
Rollup merge of #148865 - lcnr:gat-inference-hack, r=BoxyUwU
move GAT inference prevention hack The structure of `fn assemble_and_merge_candidates` is quite messy and the differences between `Host` and `NormalizesTo` goals is large enough that we should split them entirely. Intend to change this for rust-lang/trait-system-refactor-initiative#245 by mentoring someone: https://rust-lang.zulipchat.com/#narrow/channel/364551-t-types.2Ftrait-system-refactor/topic/ask.20for.20help/near/554696331. Think it's still fine to merge this PR without that larger change. fixesrust-lang/trait-system-refactor-initiative#256 r? `@BoxyUwU`
2 parents 3bc1eaa + 15b02a9 commit 0e0dac2

4 files changed

Lines changed: 104 additions & 47 deletions

File tree

‎compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,11 +1125,12 @@ where
11251125
/// treat the alias as rigid.
11261126
///
11271127
/// See trait-system-refactor-initiative#124 for more details.
1128-
#[instrument(level = "debug",skip(self, inject_normalize_to_rigid_candidate), ret)]
1128+
#[instrument(level = "debug",skip_all, fields(proven_via, goal), ret)]
11291129
pub(super)fnassemble_and_merge_candidates<G:GoalKind<D>>(
11301130
&mutself,
11311131
proven_via:Option<TraitGoalProvenVia>,
11321132
goal:Goal<I,G>,
1133+
inject_forced_ambiguity_candidate:implFnOnce(&mutEvalCtxt<'_,D>) -> Option<QueryResult<I>>,
11331134
inject_normalize_to_rigid_candidate:implFnOnce(&mutEvalCtxt<'_,D>) -> QueryResult<I>,
11341135
) -> QueryResult<I>{
11351136
letSome(proven_via) = proven_via else{
@@ -1149,15 +1150,24 @@ where
11491150
// `tests/ui/next-solver/alias-bound-shadowed-by-env.rs`.
11501151
let(mut candidates, _) = self
11511152
.assemble_and_evaluate_candidates(goal,AssembleCandidatesFrom::EnvAndBounds);
1153+
debug!(?candidates);
1154+
1155+
// If the trait goal has been proven by using the environment, we want to treat
1156+
// aliases as rigid if there are no applicable projection bounds in the environment.
1157+
if candidates.is_empty(){
1158+
returninject_normalize_to_rigid_candidate(self);
1159+
}
1160+
1161+
// If we're normalizing an GAT, we bail if using a where-bound would constrain
1162+
// its generic arguments.
1163+
ifletSome(result) = inject_forced_ambiguity_candidate(self){
1164+
return result;
1165+
}
11521166

11531167
// We still need to prefer where-bounds over alias-bounds however.
11541168
// See `tests/ui/winnowing/norm-where-bound-gt-alias-bound.rs`.
11551169
if candidates.iter().any(|c| matches!(c.source,CandidateSource::ParamEnv(_))){
11561170
candidates.retain(|c| matches!(c.source,CandidateSource::ParamEnv(_)));
1157-
}elseif candidates.is_empty(){
1158-
// If the trait goal has been proven by using the environment, we want to treat
1159-
// aliases as rigid if there are no applicable projection bounds in the environment.
1160-
returninject_normalize_to_rigid_candidate(self);
11611171
}
11621172

11631173
ifletSome((response, _)) = self.try_merge_candidates(&candidates){

‎compiler/rustc_next_trait_solver/src/solve/effect_goals.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,6 @@ where
446446
goal.with(ecx.cx(), goal.predicate.trait_ref);
447447
ecx.compute_trait_goal(trait_goal)
448448
})?;
449-
self.assemble_and_merge_candidates(proven_via, goal, |_ecx| Err(NoSolution))
449+
self.assemble_and_merge_candidates(proven_via, goal, |_ecx| None, |_ecx| Err(NoSolution))
450450
}
451451
}

‎compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs‎

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,49 @@ where
3939
let trait_goal:Goal<I, ty::TraitPredicate<I>> = goal.with(cx, trait_ref);
4040
ecx.compute_trait_goal(trait_goal)
4141
})?;
42-
self.assemble_and_merge_candidates(proven_via, goal, |ecx| {
43-
ecx.probe(|&result| ProbeKind::RigidAlias{ result }).enter(|this| {
44-
this.structurally_instantiate_normalizes_to_term(
45-
goal,
46-
goal.predicate.alias,
47-
);
48-
this.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
49-
})
50-
})
42+
self.assemble_and_merge_candidates(
43+
proven_via,
44+
goal,
45+
|ecx| {
46+
// FIXME(generic_associated_types): Addresses aggressive inference in #92917.
47+
//
48+
// If this type is a GAT with currently unconstrained arguments, we do not
49+
// want to normalize it via a candidate which only applies for a specific
50+
// instantiation. We could otherwise keep the GAT as rigid and succeed this way.
51+
// See tests/ui/generic-associated-types/no-incomplete-gat-arg-inference.rs.
52+
//
53+
// This only avoids normalization if a GAT argument is fully unconstrained.
54+
// This is quite arbitrary but fixing it causes some ambiguity, see #125196.
55+
for arg in goal.predicate.alias.own_args(cx).iter(){
56+
letSome(term) = arg.as_term()else{
57+
continue;
58+
};
59+
match ecx.structurally_normalize_term(goal.param_env, term){
60+
Ok(term) => {
61+
if term.is_infer(){
62+
returnSome(
63+
ecx.evaluate_added_goals_and_make_canonical_response(
64+
Certainty::AMBIGUOUS,
65+
),
66+
);
67+
}
68+
}
69+
Err(NoSolution) => returnSome(Err(NoSolution)),
70+
}
71+
}
72+
73+
None
74+
},
75+
|ecx| {
76+
ecx.probe(|&result| ProbeKind::RigidAlias{ result }).enter(|this| {
77+
this.structurally_instantiate_normalizes_to_term(
78+
goal,
79+
goal.predicate.alias,
80+
);
81+
this.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
82+
})
83+
},
84+
)
5185
}
5286
ty::AliasTermKind::InherentTy | ty::AliasTermKind::InherentConst => {
5387
self.normalize_inherent_associated_term(goal)
@@ -132,39 +166,7 @@ where
132166
then:implFnOnce(&mutEvalCtxt<'_,D>) -> QueryResult<I>,
133167
) -> QueryResult<I>{
134168
let cx = ecx.cx();
135-
// FIXME(generic_associated_types): Addresses aggressive inference in #92917.
136-
//
137-
// If this type is a GAT with currently unconstrained arguments, we do not
138-
// want to normalize it via a candidate which only applies for a specific
139-
// instantiation. We could otherwise keep the GAT as rigid and succeed this way.
140-
// See tests/ui/generic-associated-types/no-incomplete-gat-arg-inference.rs.
141-
//
142-
// This only avoids normalization if the GAT arguments are fully unconstrained.
143-
// This is quite arbitrary but fixing it causes some ambiguity, see #125196.
144-
match goal.predicate.alias.kind(cx){
145-
ty::AliasTermKind::ProjectionTy | ty::AliasTermKind::ProjectionConst => {
146-
for arg in goal.predicate.alias.own_args(cx).iter(){
147-
letSome(term) = arg.as_term()else{
148-
continue;
149-
};
150-
let term = ecx.structurally_normalize_term(goal.param_env, term)?;
151-
if term.is_infer(){
152-
return ecx.evaluate_added_goals_and_make_canonical_response(
153-
Certainty::AMBIGUOUS,
154-
);
155-
}
156-
}
157-
}
158-
ty::AliasTermKind::OpaqueTy
159-
| ty::AliasTermKind::InherentTy
160-
| ty::AliasTermKind::InherentConst
161-
| ty::AliasTermKind::FreeTy
162-
| ty::AliasTermKind::FreeConst
163-
| ty::AliasTermKind::UnevaluatedConst => {}
164-
}
165-
166169
let projection_pred = assumption.as_projection_clause().unwrap();
167-
168170
let assumption_projection_pred = ecx.instantiate_binder_with_infer(projection_pred);
169171
ecx.eq(goal.param_env, goal.predicate.alias, assumption_projection_pred.projection_term)?;
170172

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//@ check-pass
2+
//@ revisions: current next
3+
//@ ignore-compare-mode-next-solver (explicit revisions)
4+
//@[next] compile-flags: -Znext-solver
5+
6+
// Regression test for trait-system-refactor-initiative#256. The ambiguous
7+
// GAT arg check previously happened before checking whether the projection
8+
// candidate actually applied in the new trait solver.
9+
//
10+
// This meant we didn't consider `T::Assoc<_>` to be a rigid alias, resulting
11+
// in an inference failure.
12+
13+
pubtraitProj{
14+
typeAssoc<T>;
15+
}
16+
17+
traitId{
18+
typeThis;
19+
}
20+
impl<T>IdforT{
21+
typeThis = T;
22+
}
23+
24+
// This previously compiled as the "assumption would incompletely constrain GAT args"
25+
// check happened in each individual assumption after the `DeepRejectCtxt` fast path.
26+
fnwith_fast_reject<T,U>(x:T::Assoc<u32>)
27+
where
28+
T:Proj,
29+
U:Proj<Assoc<i32> = u32>,
30+
{
31+
let _:T::Assoc<_> = x;
32+
}
33+
34+
// This previously failed with ambiguity as that check did happen before we actually
35+
// equated the goal with the assumption. Due to the alias in the where-clause
36+
// we didn't fast-reject this candidate.
37+
fnno_fast_reject<T,U>(x:T::Assoc<u32>)
38+
where
39+
T:Proj,
40+
<UasId>::This:Proj<Assoc<i32> = u32>,
41+
{
42+
let _:T::Assoc<_> = x;
43+
}
44+
45+
fnmain(){}

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Commit 0e0dac2

Browse files
authored
Rollup merge of #148865 - lcnr:gat-inference-hack, r=BoxyUwU
move GAT inference prevention hack The structure of `fn assemble_and_merge_candidates` is quite messy and the differences between `Host` and `NormalizesTo` goals is large enough that we should split them entirely. Intend to change this for rust-lang/trait-system-refactor-initiative#245 by mentoring someone: https://rust-lang.zulipchat.com/#narrow/channel/364551-t-types.2Ftrait-system-refactor/topic/ask.20for.20help/near/554696331. Think it's still fine to merge this PR without that larger change. fixesrust-lang/trait-system-refactor-initiative#256 r? `@BoxyUwU`
2 parents 3bc1eaa + 15b02a9 commit 0e0dac2

4 files changed

Lines changed: 104 additions & 47 deletions

File tree

‎compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,11 +1125,12 @@ where
11251125
/// treat the alias as rigid.
11261126
///
11271127
/// See trait-system-refactor-initiative#124 for more details.
1128-
#[instrument(level = "debug",skip(self, inject_normalize_to_rigid_candidate), ret)]
1128+
#[instrument(level = "debug",skip_all, fields(proven_via, goal), ret)]
11291129
pub(super)fnassemble_and_merge_candidates<G:GoalKind<D>>(
11301130
&mutself,
11311131
proven_via:Option<TraitGoalProvenVia>,
11321132
goal:Goal<I,G>,
1133+
inject_forced_ambiguity_candidate:implFnOnce(&mutEvalCtxt<'_,D>) -> Option<QueryResult<I>>,
11331134
inject_normalize_to_rigid_candidate:implFnOnce(&mutEvalCtxt<'_,D>) -> QueryResult<I>,
11341135
) -> QueryResult<I>{
11351136
letSome(proven_via) = proven_via else{
@@ -1149,15 +1150,24 @@ where
11491150
// `tests/ui/next-solver/alias-bound-shadowed-by-env.rs`.
11501151
let(mut candidates, _) = self
11511152
.assemble_and_evaluate_candidates(goal,AssembleCandidatesFrom::EnvAndBounds);
1153+
debug!(?candidates);
1154+
1155+
// If the trait goal has been proven by using the environment, we want to treat
1156+
// aliases as rigid if there are no applicable projection bounds in the environment.
1157+
if candidates.is_empty(){
1158+
returninject_normalize_to_rigid_candidate(self);
1159+
}
1160+
1161+
// If we're normalizing an GAT, we bail if using a where-bound would constrain
1162+
// its generic arguments.
1163+
ifletSome(result) = inject_forced_ambiguity_candidate(self){
1164+
return result;
1165+
}
11521166

11531167
// We still need to prefer where-bounds over alias-bounds however.
11541168
// See `tests/ui/winnowing/norm-where-bound-gt-alias-bound.rs`.
11551169
if candidates.iter().any(|c| matches!(c.source,CandidateSource::ParamEnv(_))){
11561170
candidates.retain(|c| matches!(c.source,CandidateSource::ParamEnv(_)));
1157-
}elseif candidates.is_empty(){
1158-
// If the trait goal has been proven by using the environment, we want to treat
1159-
// aliases as rigid if there are no applicable projection bounds in the environment.
1160-
returninject_normalize_to_rigid_candidate(self);
11611171
}
11621172

11631173
ifletSome((response, _)) = self.try_merge_candidates(&candidates){

‎compiler/rustc_next_trait_solver/src/solve/effect_goals.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,6 @@ where
446446
goal.with(ecx.cx(), goal.predicate.trait_ref);
447447
ecx.compute_trait_goal(trait_goal)
448448
})?;
449-
self.assemble_and_merge_candidates(proven_via, goal, |_ecx| Err(NoSolution))
449+
self.assemble_and_merge_candidates(proven_via, goal, |_ecx| None, |_ecx| Err(NoSolution))
450450
}
451451
}

‎compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs‎

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,49 @@ where
3939
let trait_goal:Goal<I, ty::TraitPredicate<I>> = goal.with(cx, trait_ref);
4040
ecx.compute_trait_goal(trait_goal)
4141
})?;
42-
self.assemble_and_merge_candidates(proven_via, goal, |ecx| {
43-
ecx.probe(|&result| ProbeKind::RigidAlias{ result }).enter(|this| {
44-
this.structurally_instantiate_normalizes_to_term(
45-
goal,
46-
goal.predicate.alias,
47-
);
48-
this.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
49-
})
50-
})
42+
self.assemble_and_merge_candidates(
43+
proven_via,
44+
goal,
45+
|ecx| {
46+
// FIXME(generic_associated_types): Addresses aggressive inference in #92917.
47+
//
48+
// If this type is a GAT with currently unconstrained arguments, we do not
49+
// want to normalize it via a candidate which only applies for a specific
50+
// instantiation. We could otherwise keep the GAT as rigid and succeed this way.
51+
// See tests/ui/generic-associated-types/no-incomplete-gat-arg-inference.rs.
52+
//
53+
// This only avoids normalization if a GAT argument is fully unconstrained.
54+
// This is quite arbitrary but fixing it causes some ambiguity, see #125196.
55+
for arg in goal.predicate.alias.own_args(cx).iter(){
56+
letSome(term) = arg.as_term()else{
57+
continue;
58+
};
59+
match ecx.structurally_normalize_term(goal.param_env, term){
60+
Ok(term) => {
61+
if term.is_infer(){
62+
returnSome(
63+
ecx.evaluate_added_goals_and_make_canonical_response(
64+
Certainty::AMBIGUOUS,
65+
),
66+
);
67+
}
68+
}
69+
Err(NoSolution) => returnSome(Err(NoSolution)),
70+
}
71+
}
72+
73+
None
74+
},
75+
|ecx| {
76+
ecx.probe(|&result| ProbeKind::RigidAlias{ result }).enter(|this| {
77+
this.structurally_instantiate_normalizes_to_term(
78+
goal,
79+
goal.predicate.alias,
80+
);
81+
this.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
82+
})
83+
},
84+
)
5185
}
5286
ty::AliasTermKind::InherentTy | ty::AliasTermKind::InherentConst => {
5387
self.normalize_inherent_associated_term(goal)
@@ -132,39 +166,7 @@ where
132166
then:implFnOnce(&mutEvalCtxt<'_,D>) -> QueryResult<I>,
133167
) -> QueryResult<I>{
134168
let cx = ecx.cx();
135-
// FIXME(generic_associated_types): Addresses aggressive inference in #92917.
136-
//
137-
// If this type is a GAT with currently unconstrained arguments, we do not
138-
// want to normalize it via a candidate which only applies for a specific
139-
// instantiation. We could otherwise keep the GAT as rigid and succeed this way.
140-
// See tests/ui/generic-associated-types/no-incomplete-gat-arg-inference.rs.
141-
//
142-
// This only avoids normalization if the GAT arguments are fully unconstrained.
143-
// This is quite arbitrary but fixing it causes some ambiguity, see #125196.
144-
match goal.predicate.alias.kind(cx){
145-
ty::AliasTermKind::ProjectionTy | ty::AliasTermKind::ProjectionConst => {
146-
for arg in goal.predicate.alias.own_args(cx).iter(){
147-
letSome(term) = arg.as_term()else{
148-
continue;
149-
};
150-
let term = ecx.structurally_normalize_term(goal.param_env, term)?;
151-
if term.is_infer(){
152-
return ecx.evaluate_added_goals_and_make_canonical_response(
153-
Certainty::AMBIGUOUS,
154-
);
155-
}
156-
}
157-
}
158-
ty::AliasTermKind::OpaqueTy
159-
| ty::AliasTermKind::InherentTy
160-
| ty::AliasTermKind::InherentConst
161-
| ty::AliasTermKind::FreeTy
162-
| ty::AliasTermKind::FreeConst
163-
| ty::AliasTermKind::UnevaluatedConst => {}
164-
}
165-
166169
let projection_pred = assumption.as_projection_clause().unwrap();
167-
168170
let assumption_projection_pred = ecx.instantiate_binder_with_infer(projection_pred);
169171
ecx.eq(goal.param_env, goal.predicate.alias, assumption_projection_pred.projection_term)?;
170172

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//@ check-pass
2+
//@ revisions: current next
3+
//@ ignore-compare-mode-next-solver (explicit revisions)
4+
//@[next] compile-flags: -Znext-solver
5+
6+
// Regression test for trait-system-refactor-initiative#256. The ambiguous
7+
// GAT arg check previously happened before checking whether the projection
8+
// candidate actually applied in the new trait solver.
9+
//
10+
// This meant we didn't consider `T::Assoc<_>` to be a rigid alias, resulting
11+
// in an inference failure.
12+
13+
pubtraitProj{
14+
typeAssoc<T>;
15+
}
16+
17+
traitId{
18+
typeThis;
19+
}
20+
impl<T>IdforT{
21+
typeThis = T;
22+
}
23+
24+
// This previously compiled as the "assumption would incompletely constrain GAT args"
25+
// check happened in each individual assumption after the `DeepRejectCtxt` fast path.
26+
fnwith_fast_reject<T,U>(x:T::Assoc<u32>)
27+
where
28+
T:Proj,
29+
U:Proj<Assoc<i32> = u32>,
30+
{
31+
let _:T::Assoc<_> = x;
32+
}
33+
34+
// This previously failed with ambiguity as that check did happen before we actually
35+
// equated the goal with the assumption. Due to the alias in the where-clause
36+
// we didn't fast-reject this candidate.
37+
fnno_fast_reject<T,U>(x:T::Assoc<u32>)
38+
where
39+
T:Proj,
40+
<UasId>::This:Proj<Assoc<i32> = u32>,
41+
{
42+
let _:T::Assoc<_> = x;
43+
}
44+
45+
fnmain(){}

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 0e0dac2

Browse files
authored
Rollup merge of #148865 - lcnr:gat-inference-hack, r=BoxyUwU
move GAT inference prevention hack The structure of `fn assemble_and_merge_candidates` is quite messy and the differences between `Host` and `NormalizesTo` goals is large enough that we should split them entirely. Intend to change this for rust-lang/trait-system-refactor-initiative#245 by mentoring someone: https://rust-lang.zulipchat.com/#narrow/channel/364551-t-types.2Ftrait-system-refactor/topic/ask.20for.20help/near/554696331. Think it's still fine to merge this PR without that larger change. fixesrust-lang/trait-system-refactor-initiative#256 r? `@BoxyUwU`
2 parents 3bc1eaa + 15b02a9 commit 0e0dac2

4 files changed

Lines changed: 104 additions & 47 deletions

File tree

‎compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,11 +1125,12 @@ where
11251125
/// treat the alias as rigid.
11261126
///
11271127
/// See trait-system-refactor-initiative#124 for more details.
1128-
#[instrument(level = "debug",skip(self, inject_normalize_to_rigid_candidate), ret)]
1128+
#[instrument(level = "debug",skip_all, fields(proven_via, goal), ret)]
11291129
pub(super)fnassemble_and_merge_candidates<G:GoalKind<D>>(
11301130
&mutself,
11311131
proven_via:Option<TraitGoalProvenVia>,
11321132
goal:Goal<I,G>,
1133+
inject_forced_ambiguity_candidate:implFnOnce(&mutEvalCtxt<'_,D>) -> Option<QueryResult<I>>,
11331134
inject_normalize_to_rigid_candidate:implFnOnce(&mutEvalCtxt<'_,D>) -> QueryResult<I>,
11341135
) -> QueryResult<I>{
11351136
letSome(proven_via) = proven_via else{
@@ -1149,15 +1150,24 @@ where
11491150
// `tests/ui/next-solver/alias-bound-shadowed-by-env.rs`.
11501151
let(mut candidates, _) = self
11511152
.assemble_and_evaluate_candidates(goal,AssembleCandidatesFrom::EnvAndBounds);
1153+
debug!(?candidates);
1154+
1155+
// If the trait goal has been proven by using the environment, we want to treat
1156+
// aliases as rigid if there are no applicable projection bounds in the environment.
1157+
if candidates.is_empty(){
1158+
returninject_normalize_to_rigid_candidate(self);
1159+
}
1160+
1161+
// If we're normalizing an GAT, we bail if using a where-bound would constrain
1162+
// its generic arguments.
1163+
ifletSome(result) = inject_forced_ambiguity_candidate(self){
1164+
return result;
1165+
}
11521166

11531167
// We still need to prefer where-bounds over alias-bounds however.
11541168
// See `tests/ui/winnowing/norm-where-bound-gt-alias-bound.rs`.
11551169
if candidates.iter().any(|c| matches!(c.source,CandidateSource::ParamEnv(_))){
11561170
candidates.retain(|c| matches!(c.source,CandidateSource::ParamEnv(_)));
1157-
}elseif candidates.is_empty(){
1158-
// If the trait goal has been proven by using the environment, we want to treat
1159-
// aliases as rigid if there are no applicable projection bounds in the environment.
1160-
returninject_normalize_to_rigid_candidate(self);
11611171
}
11621172

11631173
ifletSome((response, _)) = self.try_merge_candidates(&candidates){

‎compiler/rustc_next_trait_solver/src/solve/effect_goals.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,6 @@ where
446446
goal.with(ecx.cx(), goal.predicate.trait_ref);
447447
ecx.compute_trait_goal(trait_goal)
448448
})?;
449-
self.assemble_and_merge_candidates(proven_via, goal, |_ecx| Err(NoSolution))
449+
self.assemble_and_merge_candidates(proven_via, goal, |_ecx| None, |_ecx| Err(NoSolution))
450450
}
451451
}

‎compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs‎

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,49 @@ where
3939
let trait_goal:Goal<I, ty::TraitPredicate<I>> = goal.with(cx, trait_ref);
4040
ecx.compute_trait_goal(trait_goal)
4141
})?;
42-
self.assemble_and_merge_candidates(proven_via, goal, |ecx| {
43-
ecx.probe(|&result| ProbeKind::RigidAlias{ result }).enter(|this| {
44-
this.structurally_instantiate_normalizes_to_term(
45-
goal,
46-
goal.predicate.alias,
47-
);
48-
this.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
49-
})
50-
})
42+
self.assemble_and_merge_candidates(
43+
proven_via,
44+
goal,
45+
|ecx| {
46+
// FIXME(generic_associated_types): Addresses aggressive inference in #92917.
47+
//
48+
// If this type is a GAT with currently unconstrained arguments, we do not
49+
// want to normalize it via a candidate which only applies for a specific
50+
// instantiation. We could otherwise keep the GAT as rigid and succeed this way.
51+
// See tests/ui/generic-associated-types/no-incomplete-gat-arg-inference.rs.
52+
//
53+
// This only avoids normalization if a GAT argument is fully unconstrained.
54+
// This is quite arbitrary but fixing it causes some ambiguity, see #125196.
55+
for arg in goal.predicate.alias.own_args(cx).iter(){
56+
letSome(term) = arg.as_term()else{
57+
continue;
58+
};
59+
match ecx.structurally_normalize_term(goal.param_env, term){
60+
Ok(term) => {
61+
if term.is_infer(){
62+
returnSome(
63+
ecx.evaluate_added_goals_and_make_canonical_response(
64+
Certainty::AMBIGUOUS,
65+
),
66+
);
67+
}
68+
}
69+
Err(NoSolution) => returnSome(Err(NoSolution)),
70+
}
71+
}
72+
73+
None
74+
},
75+
|ecx| {
76+
ecx.probe(|&result| ProbeKind::RigidAlias{ result }).enter(|this| {
77+
this.structurally_instantiate_normalizes_to_term(
78+
goal,
79+
goal.predicate.alias,
80+
);
81+
this.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
82+
})
83+
},
84+
)
5185
}
5286
ty::AliasTermKind::InherentTy | ty::AliasTermKind::InherentConst => {
5387
self.normalize_inherent_associated_term(goal)
@@ -132,39 +166,7 @@ where
132166
then:implFnOnce(&mutEvalCtxt<'_,D>) -> QueryResult<I>,
133167
) -> QueryResult<I>{
134168
let cx = ecx.cx();
135-
// FIXME(generic_associated_types): Addresses aggressive inference in #92917.
136-
//
137-
// If this type is a GAT with currently unconstrained arguments, we do not
138-
// want to normalize it via a candidate which only applies for a specific
139-
// instantiation. We could otherwise keep the GAT as rigid and succeed this way.
140-
// See tests/ui/generic-associated-types/no-incomplete-gat-arg-inference.rs.
141-
//
142-
// This only avoids normalization if the GAT arguments are fully unconstrained.
143-
// This is quite arbitrary but fixing it causes some ambiguity, see #125196.
144-
match goal.predicate.alias.kind(cx){
145-
ty::AliasTermKind::ProjectionTy | ty::AliasTermKind::ProjectionConst => {
146-
for arg in goal.predicate.alias.own_args(cx).iter(){
147-
letSome(term) = arg.as_term()else{
148-
continue;
149-
};
150-
let term = ecx.structurally_normalize_term(goal.param_env, term)?;
151-
if term.is_infer(){
152-
return ecx.evaluate_added_goals_and_make_canonical_response(
153-
Certainty::AMBIGUOUS,
154-
);
155-
}
156-
}
157-
}
158-
ty::AliasTermKind::OpaqueTy
159-
| ty::AliasTermKind::InherentTy
160-
| ty::AliasTermKind::InherentConst
161-
| ty::AliasTermKind::FreeTy
162-
| ty::AliasTermKind::FreeConst
163-
| ty::AliasTermKind::UnevaluatedConst => {}
164-
}
165-
166169
let projection_pred = assumption.as_projection_clause().unwrap();
167-
168170
let assumption_projection_pred = ecx.instantiate_binder_with_infer(projection_pred);
169171
ecx.eq(goal.param_env, goal.predicate.alias, assumption_projection_pred.projection_term)?;
170172

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//@ check-pass
2+
//@ revisions: current next
3+
//@ ignore-compare-mode-next-solver (explicit revisions)
4+
//@[next] compile-flags: -Znext-solver
5+
6+
// Regression test for trait-system-refactor-initiative#256. The ambiguous
7+
// GAT arg check previously happened before checking whether the projection
8+
// candidate actually applied in the new trait solver.
9+
//
10+
// This meant we didn't consider `T::Assoc<_>` to be a rigid alias, resulting
11+
// in an inference failure.
12+
13+
pubtraitProj{
14+
typeAssoc<T>;
15+
}
16+
17+
traitId{
18+
typeThis;
19+
}
20+
impl<T>IdforT{
21+
typeThis = T;
22+
}
23+
24+
// This previously compiled as the "assumption would incompletely constrain GAT args"
25+
// check happened in each individual assumption after the `DeepRejectCtxt` fast path.
26+
fnwith_fast_reject<T,U>(x:T::Assoc<u32>)
27+
where
28+
T:Proj,
29+
U:Proj<Assoc<i32> = u32>,
30+
{
31+
let _:T::Assoc<_> = x;
32+
}
33+
34+
// This previously failed with ambiguity as that check did happen before we actually
35+
// equated the goal with the assumption. Due to the alias in the where-clause
36+
// we didn't fast-reject this candidate.
37+
fnno_fast_reject<T,U>(x:T::Assoc<u32>)
38+
where
39+
T:Proj,
40+
<UasId>::This:Proj<Assoc<i32> = u32>,
41+
{
42+
let _:T::Assoc<_> = x;
43+
}
44+
45+
fnmain(){}

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 0e0dac2

Browse files
authored
Rollup merge of #148865 - lcnr:gat-inference-hack, r=BoxyUwU
move GAT inference prevention hack The structure of `fn assemble_and_merge_candidates` is quite messy and the differences between `Host` and `NormalizesTo` goals is large enough that we should split them entirely. Intend to change this for rust-lang/trait-system-refactor-initiative#245 by mentoring someone: https://rust-lang.zulipchat.com/#narrow/channel/364551-t-types.2Ftrait-system-refactor/topic/ask.20for.20help/near/554696331. Think it's still fine to merge this PR without that larger change. fixesrust-lang/trait-system-refactor-initiative#256 r? `@BoxyUwU`
2 parents 3bc1eaa + 15b02a9 commit 0e0dac2

4 files changed

Lines changed: 104 additions & 47 deletions

File tree

‎compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,11 +1125,12 @@ where
11251125
/// treat the alias as rigid.
11261126
///
11271127
/// See trait-system-refactor-initiative#124 for more details.
1128-
#[instrument(level = "debug",skip(self, inject_normalize_to_rigid_candidate), ret)]
1128+
#[instrument(level = "debug",skip_all, fields(proven_via, goal), ret)]
11291129
pub(super)fnassemble_and_merge_candidates<G:GoalKind<D>>(
11301130
&mutself,
11311131
proven_via:Option<TraitGoalProvenVia>,
11321132
goal:Goal<I,G>,
1133+
inject_forced_ambiguity_candidate:implFnOnce(&mutEvalCtxt<'_,D>) -> Option<QueryResult<I>>,
11331134
inject_normalize_to_rigid_candidate:implFnOnce(&mutEvalCtxt<'_,D>) -> QueryResult<I>,
11341135
) -> QueryResult<I>{
11351136
letSome(proven_via) = proven_via else{
@@ -1149,15 +1150,24 @@ where
11491150
// `tests/ui/next-solver/alias-bound-shadowed-by-env.rs`.
11501151
let(mut candidates, _) = self
11511152
.assemble_and_evaluate_candidates(goal,AssembleCandidatesFrom::EnvAndBounds);
1153+
debug!(?candidates);
1154+
1155+
// If the trait goal has been proven by using the environment, we want to treat
1156+
// aliases as rigid if there are no applicable projection bounds in the environment.
1157+
if candidates.is_empty(){
1158+
returninject_normalize_to_rigid_candidate(self);
1159+
}
1160+
1161+
// If we're normalizing an GAT, we bail if using a where-bound would constrain
1162+
// its generic arguments.
1163+
ifletSome(result) = inject_forced_ambiguity_candidate(self){
1164+
return result;
1165+
}
11521166

11531167
// We still need to prefer where-bounds over alias-bounds however.
11541168
// See `tests/ui/winnowing/norm-where-bound-gt-alias-bound.rs`.
11551169
if candidates.iter().any(|c| matches!(c.source,CandidateSource::ParamEnv(_))){
11561170
candidates.retain(|c| matches!(c.source,CandidateSource::ParamEnv(_)));
1157-
}elseif candidates.is_empty(){
1158-
// If the trait goal has been proven by using the environment, we want to treat
1159-
// aliases as rigid if there are no applicable projection bounds in the environment.
1160-
returninject_normalize_to_rigid_candidate(self);
11611171
}
11621172

11631173
ifletSome((response, _)) = self.try_merge_candidates(&candidates){

‎compiler/rustc_next_trait_solver/src/solve/effect_goals.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,6 @@ where
446446
goal.with(ecx.cx(), goal.predicate.trait_ref);
447447
ecx.compute_trait_goal(trait_goal)
448448
})?;
449-
self.assemble_and_merge_candidates(proven_via, goal, |_ecx| Err(NoSolution))
449+
self.assemble_and_merge_candidates(proven_via, goal, |_ecx| None, |_ecx| Err(NoSolution))
450450
}
451451
}

‎compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs‎

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,49 @@ where
3939
let trait_goal:Goal<I, ty::TraitPredicate<I>> = goal.with(cx, trait_ref);
4040
ecx.compute_trait_goal(trait_goal)
4141
})?;
42-
self.assemble_and_merge_candidates(proven_via, goal, |ecx| {
43-
ecx.probe(|&result| ProbeKind::RigidAlias{ result }).enter(|this| {
44-
this.structurally_instantiate_normalizes_to_term(
45-
goal,
46-
goal.predicate.alias,
47-
);
48-
this.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
49-
})
50-
})
42+
self.assemble_and_merge_candidates(
43+
proven_via,
44+
goal,
45+
|ecx| {
46+
// FIXME(generic_associated_types): Addresses aggressive inference in #92917.
47+
//
48+
// If this type is a GAT with currently unconstrained arguments, we do not
49+
// want to normalize it via a candidate which only applies for a specific
50+
// instantiation. We could otherwise keep the GAT as rigid and succeed this way.
51+
// See tests/ui/generic-associated-types/no-incomplete-gat-arg-inference.rs.
52+
//
53+
// This only avoids normalization if a GAT argument is fully unconstrained.
54+
// This is quite arbitrary but fixing it causes some ambiguity, see #125196.
55+
for arg in goal.predicate.alias.own_args(cx).iter(){
56+
letSome(term) = arg.as_term()else{
57+
continue;
58+
};
59+
match ecx.structurally_normalize_term(goal.param_env, term){
60+
Ok(term) => {
61+
if term.is_infer(){
62+
returnSome(
63+
ecx.evaluate_added_goals_and_make_canonical_response(
64+
Certainty::AMBIGUOUS,
65+
),
66+
);
67+
}
68+
}
69+
Err(NoSolution) => returnSome(Err(NoSolution)),
70+
}
71+
}
72+
73+
None
74+
},
75+
|ecx| {
76+
ecx.probe(|&result| ProbeKind::RigidAlias{ result }).enter(|this| {
77+
this.structurally_instantiate_normalizes_to_term(
78+
goal,
79+
goal.predicate.alias,
80+
);
81+
this.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
82+
})
83+
},
84+
)
5185
}
5286
ty::AliasTermKind::InherentTy | ty::AliasTermKind::InherentConst => {
5387
self.normalize_inherent_associated_term(goal)
@@ -132,39 +166,7 @@ where
132166
then:implFnOnce(&mutEvalCtxt<'_,D>) -> QueryResult<I>,
133167
) -> QueryResult<I>{
134168
let cx = ecx.cx();
135-
// FIXME(generic_associated_types): Addresses aggressive inference in #92917.
136-
//
137-
// If this type is a GAT with currently unconstrained arguments, we do not
138-
// want to normalize it via a candidate which only applies for a specific
139-
// instantiation. We could otherwise keep the GAT as rigid and succeed this way.
140-
// See tests/ui/generic-associated-types/no-incomplete-gat-arg-inference.rs.
141-
//
142-
// This only avoids normalization if the GAT arguments are fully unconstrained.
143-
// This is quite arbitrary but fixing it causes some ambiguity, see #125196.
144-
match goal.predicate.alias.kind(cx){
145-
ty::AliasTermKind::ProjectionTy | ty::AliasTermKind::ProjectionConst => {
146-
for arg in goal.predicate.alias.own_args(cx).iter(){
147-
letSome(term) = arg.as_term()else{
148-
continue;
149-
};
150-
let term = ecx.structurally_normalize_term(goal.param_env, term)?;
151-
if term.is_infer(){
152-
return ecx.evaluate_added_goals_and_make_canonical_response(
153-
Certainty::AMBIGUOUS,
154-
);
155-
}
156-
}
157-
}
158-
ty::AliasTermKind::OpaqueTy
159-
| ty::AliasTermKind::InherentTy
160-
| ty::AliasTermKind::InherentConst
161-
| ty::AliasTermKind::FreeTy
162-
| ty::AliasTermKind::FreeConst
163-
| ty::AliasTermKind::UnevaluatedConst => {}
164-
}
165-
166169
let projection_pred = assumption.as_projection_clause().unwrap();
167-
168170
let assumption_projection_pred = ecx.instantiate_binder_with_infer(projection_pred);
169171
ecx.eq(goal.param_env, goal.predicate.alias, assumption_projection_pred.projection_term)?;
170172

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//@ check-pass
2+
//@ revisions: current next
3+
//@ ignore-compare-mode-next-solver (explicit revisions)
4+
//@[next] compile-flags: -Znext-solver
5+
6+
// Regression test for trait-system-refactor-initiative#256. The ambiguous
7+
// GAT arg check previously happened before checking whether the projection
8+
// candidate actually applied in the new trait solver.
9+
//
10+
// This meant we didn't consider `T::Assoc<_>` to be a rigid alias, resulting
11+
// in an inference failure.
12+
13+
pubtraitProj{
14+
typeAssoc<T>;
15+
}
16+
17+
traitId{
18+
typeThis;
19+
}
20+
impl<T>IdforT{
21+
typeThis = T;
22+
}
23+
24+
// This previously compiled as the "assumption would incompletely constrain GAT args"
25+
// check happened in each individual assumption after the `DeepRejectCtxt` fast path.
26+
fnwith_fast_reject<T,U>(x:T::Assoc<u32>)
27+
where
28+
T:Proj,
29+
U:Proj<Assoc<i32> = u32>,
30+
{
31+
let _:T::Assoc<_> = x;
32+
}
33+
34+
// This previously failed with ambiguity as that check did happen before we actually
35+
// equated the goal with the assumption. Due to the alias in the where-clause
36+
// we didn't fast-reject this candidate.
37+
fnno_fast_reject<T,U>(x:T::Assoc<u32>)
38+
where
39+
T:Proj,
40+
<UasId>::This:Proj<Assoc<i32> = u32>,
41+
{
42+
let _:T::Assoc<_> = x;
43+
}
44+
45+
fnmain(){}

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Commit 0e0dac2

Browse files
authored
Rollup merge of #148865 - lcnr:gat-inference-hack, r=BoxyUwU
move GAT inference prevention hack The structure of `fn assemble_and_merge_candidates` is quite messy and the differences between `Host` and `NormalizesTo` goals is large enough that we should split them entirely. Intend to change this for rust-lang/trait-system-refactor-initiative#245 by mentoring someone: https://rust-lang.zulipchat.com/#narrow/channel/364551-t-types.2Ftrait-system-refactor/topic/ask.20for.20help/near/554696331. Think it's still fine to merge this PR without that larger change. fixesrust-lang/trait-system-refactor-initiative#256 r? `@BoxyUwU`
2 parents 3bc1eaa + 15b02a9 commit 0e0dac2

4 files changed

Lines changed: 104 additions & 47 deletions

File tree

‎compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,11 +1125,12 @@ where
11251125
/// treat the alias as rigid.
11261126
///
11271127
/// See trait-system-refactor-initiative#124 for more details.
1128-
#[instrument(level = "debug",skip(self, inject_normalize_to_rigid_candidate), ret)]
1128+
#[instrument(level = "debug",skip_all, fields(proven_via, goal), ret)]
11291129
pub(super)fnassemble_and_merge_candidates<G:GoalKind<D>>(
11301130
&mutself,
11311131
proven_via:Option<TraitGoalProvenVia>,
11321132
goal:Goal<I,G>,
1133+
inject_forced_ambiguity_candidate:implFnOnce(&mutEvalCtxt<'_,D>) -> Option<QueryResult<I>>,
11331134
inject_normalize_to_rigid_candidate:implFnOnce(&mutEvalCtxt<'_,D>) -> QueryResult<I>,
11341135
) -> QueryResult<I>{
11351136
letSome(proven_via) = proven_via else{
@@ -1149,15 +1150,24 @@ where
11491150
// `tests/ui/next-solver/alias-bound-shadowed-by-env.rs`.
11501151
let(mut candidates, _) = self
11511152
.assemble_and_evaluate_candidates(goal,AssembleCandidatesFrom::EnvAndBounds);
1153+
debug!(?candidates);
1154+
1155+
// If the trait goal has been proven by using the environment, we want to treat
1156+
// aliases as rigid if there are no applicable projection bounds in the environment.
1157+
if candidates.is_empty(){
1158+
returninject_normalize_to_rigid_candidate(self);
1159+
}
1160+
1161+
// If we're normalizing an GAT, we bail if using a where-bound would constrain
1162+
// its generic arguments.
1163+
ifletSome(result) = inject_forced_ambiguity_candidate(self){
1164+
return result;
1165+
}
11521166

11531167
// We still need to prefer where-bounds over alias-bounds however.
11541168
// See `tests/ui/winnowing/norm-where-bound-gt-alias-bound.rs`.
11551169
if candidates.iter().any(|c| matches!(c.source,CandidateSource::ParamEnv(_))){
11561170
candidates.retain(|c| matches!(c.source,CandidateSource::ParamEnv(_)));
1157-
}elseif candidates.is_empty(){
1158-
// If the trait goal has been proven by using the environment, we want to treat
1159-
// aliases as rigid if there are no applicable projection bounds in the environment.
1160-
returninject_normalize_to_rigid_candidate(self);
11611171
}
11621172

11631173
ifletSome((response, _)) = self.try_merge_candidates(&candidates){

‎compiler/rustc_next_trait_solver/src/solve/effect_goals.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,6 @@ where
446446
goal.with(ecx.cx(), goal.predicate.trait_ref);
447447
ecx.compute_trait_goal(trait_goal)
448448
})?;
449-
self.assemble_and_merge_candidates(proven_via, goal, |_ecx| Err(NoSolution))
449+
self.assemble_and_merge_candidates(proven_via, goal, |_ecx| None, |_ecx| Err(NoSolution))
450450
}
451451
}

‎compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs‎

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,49 @@ where
3939
let trait_goal:Goal<I, ty::TraitPredicate<I>> = goal.with(cx, trait_ref);
4040
ecx.compute_trait_goal(trait_goal)
4141
})?;
42-
self.assemble_and_merge_candidates(proven_via, goal, |ecx| {
43-
ecx.probe(|&result| ProbeKind::RigidAlias{ result }).enter(|this| {
44-
this.structurally_instantiate_normalizes_to_term(
45-
goal,
46-
goal.predicate.alias,
47-
);
48-
this.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
49-
})
50-
})
42+
self.assemble_and_merge_candidates(
43+
proven_via,
44+
goal,
45+
|ecx| {
46+
// FIXME(generic_associated_types): Addresses aggressive inference in #92917.
47+
//
48+
// If this type is a GAT with currently unconstrained arguments, we do not
49+
// want to normalize it via a candidate which only applies for a specific
50+
// instantiation. We could otherwise keep the GAT as rigid and succeed this way.
51+
// See tests/ui/generic-associated-types/no-incomplete-gat-arg-inference.rs.
52+
//
53+
// This only avoids normalization if a GAT argument is fully unconstrained.
54+
// This is quite arbitrary but fixing it causes some ambiguity, see #125196.
55+
for arg in goal.predicate.alias.own_args(cx).iter(){
56+
letSome(term) = arg.as_term()else{
57+
continue;
58+
};
59+
match ecx.structurally_normalize_term(goal.param_env, term){
60+
Ok(term) => {
61+
if term.is_infer(){
62+
returnSome(
63+
ecx.evaluate_added_goals_and_make_canonical_response(
64+
Certainty::AMBIGUOUS,
65+
),
66+
);
67+
}
68+
}
69+
Err(NoSolution) => returnSome(Err(NoSolution)),
70+
}
71+
}
72+
73+
None
74+
},
75+
|ecx| {
76+
ecx.probe(|&result| ProbeKind::RigidAlias{ result }).enter(|this| {
77+
this.structurally_instantiate_normalizes_to_term(
78+
goal,
79+
goal.predicate.alias,
80+
);
81+
this.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
82+
})
83+
},
84+
)
5185
}
5286
ty::AliasTermKind::InherentTy | ty::AliasTermKind::InherentConst => {
5387
self.normalize_inherent_associated_term(goal)
@@ -132,39 +166,7 @@ where
132166
then:implFnOnce(&mutEvalCtxt<'_,D>) -> QueryResult<I>,
133167
) -> QueryResult<I>{
134168
let cx = ecx.cx();
135-
// FIXME(generic_associated_types): Addresses aggressive inference in #92917.
136-
//
137-
// If this type is a GAT with currently unconstrained arguments, we do not
138-
// want to normalize it via a candidate which only applies for a specific
139-
// instantiation. We could otherwise keep the GAT as rigid and succeed this way.
140-
// See tests/ui/generic-associated-types/no-incomplete-gat-arg-inference.rs.
141-
//
142-
// This only avoids normalization if the GAT arguments are fully unconstrained.
143-
// This is quite arbitrary but fixing it causes some ambiguity, see #125196.
144-
match goal.predicate.alias.kind(cx){
145-
ty::AliasTermKind::ProjectionTy | ty::AliasTermKind::ProjectionConst => {
146-
for arg in goal.predicate.alias.own_args(cx).iter(){
147-
letSome(term) = arg.as_term()else{
148-
continue;
149-
};
150-
let term = ecx.structurally_normalize_term(goal.param_env, term)?;
151-
if term.is_infer(){
152-
return ecx.evaluate_added_goals_and_make_canonical_response(
153-
Certainty::AMBIGUOUS,
154-
);
155-
}
156-
}
157-
}
158-
ty::AliasTermKind::OpaqueTy
159-
| ty::AliasTermKind::InherentTy
160-
| ty::AliasTermKind::InherentConst
161-
| ty::AliasTermKind::FreeTy
162-
| ty::AliasTermKind::FreeConst
163-
| ty::AliasTermKind::UnevaluatedConst => {}
164-
}
165-
166169
let projection_pred = assumption.as_projection_clause().unwrap();
167-
168170
let assumption_projection_pred = ecx.instantiate_binder_with_infer(projection_pred);
169171
ecx.eq(goal.param_env, goal.predicate.alias, assumption_projection_pred.projection_term)?;
170172

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//@ check-pass
2+
//@ revisions: current next
3+
//@ ignore-compare-mode-next-solver (explicit revisions)
4+
//@[next] compile-flags: -Znext-solver
5+
6+
// Regression test for trait-system-refactor-initiative#256. The ambiguous
7+
// GAT arg check previously happened before checking whether the projection
8+
// candidate actually applied in the new trait solver.
9+
//
10+
// This meant we didn't consider `T::Assoc<_>` to be a rigid alias, resulting
11+
// in an inference failure.
12+
13+
pubtraitProj{
14+
typeAssoc<T>;
15+
}
16+
17+
traitId{
18+
typeThis;
19+
}
20+
impl<T>IdforT{
21+
typeThis = T;
22+
}
23+
24+
// This previously compiled as the "assumption would incompletely constrain GAT args"
25+
// check happened in each individual assumption after the `DeepRejectCtxt` fast path.
26+
fnwith_fast_reject<T,U>(x:T::Assoc<u32>)
27+
where
28+
T:Proj,
29+
U:Proj<Assoc<i32> = u32>,
30+
{
31+
let _:T::Assoc<_> = x;
32+
}
33+
34+
// This previously failed with ambiguity as that check did happen before we actually
35+
// equated the goal with the assumption. Due to the alias in the where-clause
36+
// we didn't fast-reject this candidate.
37+
fnno_fast_reject<T,U>(x:T::Assoc<u32>)
38+
where
39+
T:Proj,
40+
<UasId>::This:Proj<Assoc<i32> = u32>,
41+
{
42+
let _:T::Assoc<_> = x;
43+
}
44+
45+
fnmain(){}

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 0e0dac2

Browse files
authored
Rollup merge of #148865 - lcnr:gat-inference-hack, r=BoxyUwU
move GAT inference prevention hack The structure of `fn assemble_and_merge_candidates` is quite messy and the differences between `Host` and `NormalizesTo` goals is large enough that we should split them entirely. Intend to change this for rust-lang/trait-system-refactor-initiative#245 by mentoring someone: https://rust-lang.zulipchat.com/#narrow/channel/364551-t-types.2Ftrait-system-refactor/topic/ask.20for.20help/near/554696331. Think it's still fine to merge this PR without that larger change. fixesrust-lang/trait-system-refactor-initiative#256 r? `@BoxyUwU`
2 parents 3bc1eaa + 15b02a9 commit 0e0dac2

4 files changed

Lines changed: 104 additions & 47 deletions

File tree

‎compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,11 +1125,12 @@ where
11251125
/// treat the alias as rigid.
11261126
///
11271127
/// See trait-system-refactor-initiative#124 for more details.
1128-
#[instrument(level = "debug",skip(self, inject_normalize_to_rigid_candidate), ret)]
1128+
#[instrument(level = "debug",skip_all, fields(proven_via, goal), ret)]
11291129
pub(super)fnassemble_and_merge_candidates<G:GoalKind<D>>(
11301130
&mutself,
11311131
proven_via:Option<TraitGoalProvenVia>,
11321132
goal:Goal<I,G>,
1133+
inject_forced_ambiguity_candidate:implFnOnce(&mutEvalCtxt<'_,D>) -> Option<QueryResult<I>>,
11331134
inject_normalize_to_rigid_candidate:implFnOnce(&mutEvalCtxt<'_,D>) -> QueryResult<I>,
11341135
) -> QueryResult<I>{
11351136
letSome(proven_via) = proven_via else{
@@ -1149,15 +1150,24 @@ where
11491150
// `tests/ui/next-solver/alias-bound-shadowed-by-env.rs`.
11501151
let(mut candidates, _) = self
11511152
.assemble_and_evaluate_candidates(goal,AssembleCandidatesFrom::EnvAndBounds);
1153+
debug!(?candidates);
1154+
1155+
// If the trait goal has been proven by using the environment, we want to treat
1156+
// aliases as rigid if there are no applicable projection bounds in the environment.
1157+
if candidates.is_empty(){
1158+
returninject_normalize_to_rigid_candidate(self);
1159+
}
1160+
1161+
// If we're normalizing an GAT, we bail if using a where-bound would constrain
1162+
// its generic arguments.
1163+
ifletSome(result) = inject_forced_ambiguity_candidate(self){
1164+
return result;
1165+
}
11521166

11531167
// We still need to prefer where-bounds over alias-bounds however.
11541168
// See `tests/ui/winnowing/norm-where-bound-gt-alias-bound.rs`.
11551169
if candidates.iter().any(|c| matches!(c.source,CandidateSource::ParamEnv(_))){
11561170
candidates.retain(|c| matches!(c.source,CandidateSource::ParamEnv(_)));
1157-
}elseif candidates.is_empty(){
1158-
// If the trait goal has been proven by using the environment, we want to treat
1159-
// aliases as rigid if there are no applicable projection bounds in the environment.
1160-
returninject_normalize_to_rigid_candidate(self);
11611171
}
11621172

11631173
ifletSome((response, _)) = self.try_merge_candidates(&candidates){

‎compiler/rustc_next_trait_solver/src/solve/effect_goals.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,6 @@ where
446446
goal.with(ecx.cx(), goal.predicate.trait_ref);
447447
ecx.compute_trait_goal(trait_goal)
448448
})?;
449-
self.assemble_and_merge_candidates(proven_via, goal, |_ecx| Err(NoSolution))
449+
self.assemble_and_merge_candidates(proven_via, goal, |_ecx| None, |_ecx| Err(NoSolution))
450450
}
451451
}

‎compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs‎

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,49 @@ where
3939
let trait_goal:Goal<I, ty::TraitPredicate<I>> = goal.with(cx, trait_ref);
4040
ecx.compute_trait_goal(trait_goal)
4141
})?;
42-
self.assemble_and_merge_candidates(proven_via, goal, |ecx| {
43-
ecx.probe(|&result| ProbeKind::RigidAlias{ result }).enter(|this| {
44-
this.structurally_instantiate_normalizes_to_term(
45-
goal,
46-
goal.predicate.alias,
47-
);
48-
this.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
49-
})
50-
})
42+
self.assemble_and_merge_candidates(
43+
proven_via,
44+
goal,
45+
|ecx| {
46+
// FIXME(generic_associated_types): Addresses aggressive inference in #92917.
47+
//
48+
// If this type is a GAT with currently unconstrained arguments, we do not
49+
// want to normalize it via a candidate which only applies for a specific
50+
// instantiation. We could otherwise keep the GAT as rigid and succeed this way.
51+
// See tests/ui/generic-associated-types/no-incomplete-gat-arg-inference.rs.
52+
//
53+
// This only avoids normalization if a GAT argument is fully unconstrained.
54+
// This is quite arbitrary but fixing it causes some ambiguity, see #125196.
55+
for arg in goal.predicate.alias.own_args(cx).iter(){
56+
letSome(term) = arg.as_term()else{
57+
continue;
58+
};
59+
match ecx.structurally_normalize_term(goal.param_env, term){
60+
Ok(term) => {
61+
if term.is_infer(){
62+
returnSome(
63+
ecx.evaluate_added_goals_and_make_canonical_response(
64+
Certainty::AMBIGUOUS,
65+
),
66+
);
67+
}
68+
}
69+
Err(NoSolution) => returnSome(Err(NoSolution)),
70+
}
71+
}
72+
73+
None
74+
},
75+
|ecx| {
76+
ecx.probe(|&result| ProbeKind::RigidAlias{ result }).enter(|this| {
77+
this.structurally_instantiate_normalizes_to_term(
78+
goal,
79+
goal.predicate.alias,
80+
);
81+
this.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
82+
})
83+
},
84+
)
5185
}
5286
ty::AliasTermKind::InherentTy | ty::AliasTermKind::InherentConst => {
5387
self.normalize_inherent_associated_term(goal)
@@ -132,39 +166,7 @@ where
132166
then:implFnOnce(&mutEvalCtxt<'_,D>) -> QueryResult<I>,
133167
) -> QueryResult<I>{
134168
let cx = ecx.cx();
135-
// FIXME(generic_associated_types): Addresses aggressive inference in #92917.
136-
//
137-
// If this type is a GAT with currently unconstrained arguments, we do not
138-
// want to normalize it via a candidate which only applies for a specific
139-
// instantiation. We could otherwise keep the GAT as rigid and succeed this way.
140-
// See tests/ui/generic-associated-types/no-incomplete-gat-arg-inference.rs.
141-
//
142-
// This only avoids normalization if the GAT arguments are fully unconstrained.
143-
// This is quite arbitrary but fixing it causes some ambiguity, see #125196.
144-
match goal.predicate.alias.kind(cx){
145-
ty::AliasTermKind::ProjectionTy | ty::AliasTermKind::ProjectionConst => {
146-
for arg in goal.predicate.alias.own_args(cx).iter(){
147-
letSome(term) = arg.as_term()else{
148-
continue;
149-
};
150-
let term = ecx.structurally_normalize_term(goal.param_env, term)?;
151-
if term.is_infer(){
152-
return ecx.evaluate_added_goals_and_make_canonical_response(
153-
Certainty::AMBIGUOUS,
154-
);
155-
}
156-
}
157-
}
158-
ty::AliasTermKind::OpaqueTy
159-
| ty::AliasTermKind::InherentTy
160-
| ty::AliasTermKind::InherentConst
161-
| ty::AliasTermKind::FreeTy
162-
| ty::AliasTermKind::FreeConst
163-
| ty::AliasTermKind::UnevaluatedConst => {}
164-
}
165-
166169
let projection_pred = assumption.as_projection_clause().unwrap();
167-
168170
let assumption_projection_pred = ecx.instantiate_binder_with_infer(projection_pred);
169171
ecx.eq(goal.param_env, goal.predicate.alias, assumption_projection_pred.projection_term)?;
170172

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//@ check-pass
2+
//@ revisions: current next
3+
//@ ignore-compare-mode-next-solver (explicit revisions)
4+
//@[next] compile-flags: -Znext-solver
5+
6+
// Regression test for trait-system-refactor-initiative#256. The ambiguous
7+
// GAT arg check previously happened before checking whether the projection
8+
// candidate actually applied in the new trait solver.
9+
//
10+
// This meant we didn't consider `T::Assoc<_>` to be a rigid alias, resulting
11+
// in an inference failure.
12+
13+
pubtraitProj{
14+
typeAssoc<T>;
15+
}
16+
17+
traitId{
18+
typeThis;
19+
}
20+
impl<T>IdforT{
21+
typeThis = T;
22+
}
23+
24+
// This previously compiled as the "assumption would incompletely constrain GAT args"
25+
// check happened in each individual assumption after the `DeepRejectCtxt` fast path.
26+
fnwith_fast_reject<T,U>(x:T::Assoc<u32>)
27+
where
28+
T:Proj,
29+
U:Proj<Assoc<i32> = u32>,
30+
{
31+
let _:T::Assoc<_> = x;
32+
}
33+
34+
// This previously failed with ambiguity as that check did happen before we actually
35+
// equated the goal with the assumption. Due to the alias in the where-clause
36+
// we didn't fast-reject this candidate.
37+
fnno_fast_reject<T,U>(x:T::Assoc<u32>)
38+
where
39+
T:Proj,
40+
<UasId>::This:Proj<Assoc<i32> = u32>,
41+
{
42+
let _:T::Assoc<_> = x;
43+
}
44+
45+
fnmain(){}

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 0e0dac2

Browse files
authored
Rollup merge of #148865 - lcnr:gat-inference-hack, r=BoxyUwU
move GAT inference prevention hack The structure of `fn assemble_and_merge_candidates` is quite messy and the differences between `Host` and `NormalizesTo` goals is large enough that we should split them entirely. Intend to change this for rust-lang/trait-system-refactor-initiative#245 by mentoring someone: https://rust-lang.zulipchat.com/#narrow/channel/364551-t-types.2Ftrait-system-refactor/topic/ask.20for.20help/near/554696331. Think it's still fine to merge this PR without that larger change. fixesrust-lang/trait-system-refactor-initiative#256 r? `@BoxyUwU`
2 parents 3bc1eaa + 15b02a9 commit 0e0dac2

4 files changed

Lines changed: 104 additions & 47 deletions

File tree

‎compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,11 +1125,12 @@ where
11251125
/// treat the alias as rigid.
11261126
///
11271127
/// See trait-system-refactor-initiative#124 for more details.
1128-
#[instrument(level = "debug",skip(self, inject_normalize_to_rigid_candidate), ret)]
1128+
#[instrument(level = "debug",skip_all, fields(proven_via, goal), ret)]
11291129
pub(super)fnassemble_and_merge_candidates<G:GoalKind<D>>(
11301130
&mutself,
11311131
proven_via:Option<TraitGoalProvenVia>,
11321132
goal:Goal<I,G>,
1133+
inject_forced_ambiguity_candidate:implFnOnce(&mutEvalCtxt<'_,D>) -> Option<QueryResult<I>>,
11331134
inject_normalize_to_rigid_candidate:implFnOnce(&mutEvalCtxt<'_,D>) -> QueryResult<I>,
11341135
) -> QueryResult<I>{
11351136
letSome(proven_via) = proven_via else{
@@ -1149,15 +1150,24 @@ where
11491150
// `tests/ui/next-solver/alias-bound-shadowed-by-env.rs`.
11501151
let(mut candidates, _) = self
11511152
.assemble_and_evaluate_candidates(goal,AssembleCandidatesFrom::EnvAndBounds);
1153+
debug!(?candidates);
1154+
1155+
// If the trait goal has been proven by using the environment, we want to treat
1156+
// aliases as rigid if there are no applicable projection bounds in the environment.
1157+
if candidates.is_empty(){
1158+
returninject_normalize_to_rigid_candidate(self);
1159+
}
1160+
1161+
// If we're normalizing an GAT, we bail if using a where-bound would constrain
1162+
// its generic arguments.
1163+
ifletSome(result) = inject_forced_ambiguity_candidate(self){
1164+
return result;
1165+
}
11521166

11531167
// We still need to prefer where-bounds over alias-bounds however.
11541168
// See `tests/ui/winnowing/norm-where-bound-gt-alias-bound.rs`.
11551169
if candidates.iter().any(|c| matches!(c.source,CandidateSource::ParamEnv(_))){
11561170
candidates.retain(|c| matches!(c.source,CandidateSource::ParamEnv(_)));
1157-
}elseif candidates.is_empty(){
1158-
// If the trait goal has been proven by using the environment, we want to treat
1159-
// aliases as rigid if there are no applicable projection bounds in the environment.
1160-
returninject_normalize_to_rigid_candidate(self);
11611171
}
11621172

11631173
ifletSome((response, _)) = self.try_merge_candidates(&candidates){

‎compiler/rustc_next_trait_solver/src/solve/effect_goals.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,6 @@ where
446446
goal.with(ecx.cx(), goal.predicate.trait_ref);
447447
ecx.compute_trait_goal(trait_goal)
448448
})?;
449-
self.assemble_and_merge_candidates(proven_via, goal, |_ecx| Err(NoSolution))
449+
self.assemble_and_merge_candidates(proven_via, goal, |_ecx| None, |_ecx| Err(NoSolution))
450450
}
451451
}

‎compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs‎

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,49 @@ where
3939
let trait_goal:Goal<I, ty::TraitPredicate<I>> = goal.with(cx, trait_ref);
4040
ecx.compute_trait_goal(trait_goal)
4141
})?;
42-
self.assemble_and_merge_candidates(proven_via, goal, |ecx| {
43-
ecx.probe(|&result| ProbeKind::RigidAlias{ result }).enter(|this| {
44-
this.structurally_instantiate_normalizes_to_term(
45-
goal,
46-
goal.predicate.alias,
47-
);
48-
this.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
49-
})
50-
})
42+
self.assemble_and_merge_candidates(
43+
proven_via,
44+
goal,
45+
|ecx| {
46+
// FIXME(generic_associated_types): Addresses aggressive inference in #92917.
47+
//
48+
// If this type is a GAT with currently unconstrained arguments, we do not
49+
// want to normalize it via a candidate which only applies for a specific
50+
// instantiation. We could otherwise keep the GAT as rigid and succeed this way.
51+
// See tests/ui/generic-associated-types/no-incomplete-gat-arg-inference.rs.
52+
//
53+
// This only avoids normalization if a GAT argument is fully unconstrained.
54+
// This is quite arbitrary but fixing it causes some ambiguity, see #125196.
55+
for arg in goal.predicate.alias.own_args(cx).iter(){
56+
letSome(term) = arg.as_term()else{
57+
continue;
58+
};
59+
match ecx.structurally_normalize_term(goal.param_env, term){
60+
Ok(term) => {
61+
if term.is_infer(){
62+
returnSome(
63+
ecx.evaluate_added_goals_and_make_canonical_response(
64+
Certainty::AMBIGUOUS,
65+
),
66+
);
67+
}
68+
}
69+
Err(NoSolution) => returnSome(Err(NoSolution)),
70+
}
71+
}
72+
73+
None
74+
},
75+
|ecx| {
76+
ecx.probe(|&result| ProbeKind::RigidAlias{ result }).enter(|this| {
77+
this.structurally_instantiate_normalizes_to_term(
78+
goal,
79+
goal.predicate.alias,
80+
);
81+
this.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
82+
})
83+
},
84+
)
5185
}
5286
ty::AliasTermKind::InherentTy | ty::AliasTermKind::InherentConst => {
5387
self.normalize_inherent_associated_term(goal)
@@ -132,39 +166,7 @@ where
132166
then:implFnOnce(&mutEvalCtxt<'_,D>) -> QueryResult<I>,
133167
) -> QueryResult<I>{
134168
let cx = ecx.cx();
135-
// FIXME(generic_associated_types): Addresses aggressive inference in #92917.
136-
//
137-
// If this type is a GAT with currently unconstrained arguments, we do not
138-
// want to normalize it via a candidate which only applies for a specific
139-
// instantiation. We could otherwise keep the GAT as rigid and succeed this way.
140-
// See tests/ui/generic-associated-types/no-incomplete-gat-arg-inference.rs.
141-
//
142-
// This only avoids normalization if the GAT arguments are fully unconstrained.
143-
// This is quite arbitrary but fixing it causes some ambiguity, see #125196.
144-
match goal.predicate.alias.kind(cx){
145-
ty::AliasTermKind::ProjectionTy | ty::AliasTermKind::ProjectionConst => {
146-
for arg in goal.predicate.alias.own_args(cx).iter(){
147-
letSome(term) = arg.as_term()else{
148-
continue;
149-
};
150-
let term = ecx.structurally_normalize_term(goal.param_env, term)?;
151-
if term.is_infer(){
152-
return ecx.evaluate_added_goals_and_make_canonical_response(
153-
Certainty::AMBIGUOUS,
154-
);
155-
}
156-
}
157-
}
158-
ty::AliasTermKind::OpaqueTy
159-
| ty::AliasTermKind::InherentTy
160-
| ty::AliasTermKind::InherentConst
161-
| ty::AliasTermKind::FreeTy
162-
| ty::AliasTermKind::FreeConst
163-
| ty::AliasTermKind::UnevaluatedConst => {}
164-
}
165-
166169
let projection_pred = assumption.as_projection_clause().unwrap();
167-
168170
let assumption_projection_pred = ecx.instantiate_binder_with_infer(projection_pred);
169171
ecx.eq(goal.param_env, goal.predicate.alias, assumption_projection_pred.projection_term)?;
170172

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//@ check-pass
2+
//@ revisions: current next
3+
//@ ignore-compare-mode-next-solver (explicit revisions)
4+
//@[next] compile-flags: -Znext-solver
5+
6+
// Regression test for trait-system-refactor-initiative#256. The ambiguous
7+
// GAT arg check previously happened before checking whether the projection
8+
// candidate actually applied in the new trait solver.
9+
//
10+
// This meant we didn't consider `T::Assoc<_>` to be a rigid alias, resulting
11+
// in an inference failure.
12+
13+
pubtraitProj{
14+
typeAssoc<T>;
15+
}
16+
17+
traitId{
18+
typeThis;
19+
}
20+
impl<T>IdforT{
21+
typeThis = T;
22+
}
23+
24+
// This previously compiled as the "assumption would incompletely constrain GAT args"
25+
// check happened in each individual assumption after the `DeepRejectCtxt` fast path.
26+
fnwith_fast_reject<T,U>(x:T::Assoc<u32>)
27+
where
28+
T:Proj,
29+
U:Proj<Assoc<i32> = u32>,
30+
{
31+
let _:T::Assoc<_> = x;
32+
}
33+
34+
// This previously failed with ambiguity as that check did happen before we actually
35+
// equated the goal with the assumption. Due to the alias in the where-clause
36+
// we didn't fast-reject this candidate.
37+
fnno_fast_reject<T,U>(x:T::Assoc<u32>)
38+
where
39+
T:Proj,
40+
<UasId>::This:Proj<Assoc<i32> = u32>,
41+
{
42+
let _:T::Assoc<_> = x;
43+
}
44+
45+
fnmain(){}

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Commit 0e0dac2

Browse files
authored
Rollup merge of #148865 - lcnr:gat-inference-hack, r=BoxyUwU
move GAT inference prevention hack The structure of `fn assemble_and_merge_candidates` is quite messy and the differences between `Host` and `NormalizesTo` goals is large enough that we should split them entirely. Intend to change this for rust-lang/trait-system-refactor-initiative#245 by mentoring someone: https://rust-lang.zulipchat.com/#narrow/channel/364551-t-types.2Ftrait-system-refactor/topic/ask.20for.20help/near/554696331. Think it's still fine to merge this PR without that larger change. fixesrust-lang/trait-system-refactor-initiative#256 r? `@BoxyUwU`
2 parents 3bc1eaa + 15b02a9 commit 0e0dac2

4 files changed

Lines changed: 104 additions & 47 deletions

File tree

‎compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,11 +1125,12 @@ where
11251125
/// treat the alias as rigid.
11261126
///
11271127
/// See trait-system-refactor-initiative#124 for more details.
1128-
#[instrument(level = "debug",skip(self, inject_normalize_to_rigid_candidate), ret)]
1128+
#[instrument(level = "debug",skip_all, fields(proven_via, goal), ret)]
11291129
pub(super)fnassemble_and_merge_candidates<G:GoalKind<D>>(
11301130
&mutself,
11311131
proven_via:Option<TraitGoalProvenVia>,
11321132
goal:Goal<I,G>,
1133+
inject_forced_ambiguity_candidate:implFnOnce(&mutEvalCtxt<'_,D>) -> Option<QueryResult<I>>,
11331134
inject_normalize_to_rigid_candidate:implFnOnce(&mutEvalCtxt<'_,D>) -> QueryResult<I>,
11341135
) -> QueryResult<I>{
11351136
letSome(proven_via) = proven_via else{
@@ -1149,15 +1150,24 @@ where
11491150
// `tests/ui/next-solver/alias-bound-shadowed-by-env.rs`.
11501151
let(mut candidates, _) = self
11511152
.assemble_and_evaluate_candidates(goal,AssembleCandidatesFrom::EnvAndBounds);
1153+
debug!(?candidates);
1154+
1155+
// If the trait goal has been proven by using the environment, we want to treat
1156+
// aliases as rigid if there are no applicable projection bounds in the environment.
1157+
if candidates.is_empty(){
1158+
returninject_normalize_to_rigid_candidate(self);
1159+
}
1160+
1161+
// If we're normalizing an GAT, we bail if using a where-bound would constrain
1162+
// its generic arguments.
1163+
ifletSome(result) = inject_forced_ambiguity_candidate(self){
1164+
return result;
1165+
}
11521166

11531167
// We still need to prefer where-bounds over alias-bounds however.
11541168
// See `tests/ui/winnowing/norm-where-bound-gt-alias-bound.rs`.
11551169
if candidates.iter().any(|c| matches!(c.source,CandidateSource::ParamEnv(_))){
11561170
candidates.retain(|c| matches!(c.source,CandidateSource::ParamEnv(_)));
1157-
}elseif candidates.is_empty(){
1158-
// If the trait goal has been proven by using the environment, we want to treat
1159-
// aliases as rigid if there are no applicable projection bounds in the environment.
1160-
returninject_normalize_to_rigid_candidate(self);
11611171
}
11621172

11631173
ifletSome((response, _)) = self.try_merge_candidates(&candidates){

‎compiler/rustc_next_trait_solver/src/solve/effect_goals.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,6 @@ where
446446
goal.with(ecx.cx(), goal.predicate.trait_ref);
447447
ecx.compute_trait_goal(trait_goal)
448448
})?;
449-
self.assemble_and_merge_candidates(proven_via, goal, |_ecx| Err(NoSolution))
449+
self.assemble_and_merge_candidates(proven_via, goal, |_ecx| None, |_ecx| Err(NoSolution))
450450
}
451451
}

‎compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs‎

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,49 @@ where
3939
let trait_goal:Goal<I, ty::TraitPredicate<I>> = goal.with(cx, trait_ref);
4040
ecx.compute_trait_goal(trait_goal)
4141
})?;
42-
self.assemble_and_merge_candidates(proven_via, goal, |ecx| {
43-
ecx.probe(|&result| ProbeKind::RigidAlias{ result }).enter(|this| {
44-
this.structurally_instantiate_normalizes_to_term(
45-
goal,
46-
goal.predicate.alias,
47-
);
48-
this.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
49-
})
50-
})
42+
self.assemble_and_merge_candidates(
43+
proven_via,
44+
goal,
45+
|ecx| {
46+
// FIXME(generic_associated_types): Addresses aggressive inference in #92917.
47+
//
48+
// If this type is a GAT with currently unconstrained arguments, we do not
49+
// want to normalize it via a candidate which only applies for a specific
50+
// instantiation. We could otherwise keep the GAT as rigid and succeed this way.
51+
// See tests/ui/generic-associated-types/no-incomplete-gat-arg-inference.rs.
52+
//
53+
// This only avoids normalization if a GAT argument is fully unconstrained.
54+
// This is quite arbitrary but fixing it causes some ambiguity, see #125196.
55+
for arg in goal.predicate.alias.own_args(cx).iter(){
56+
letSome(term) = arg.as_term()else{
57+
continue;
58+
};
59+
match ecx.structurally_normalize_term(goal.param_env, term){
60+
Ok(term) => {
61+
if term.is_infer(){
62+
returnSome(
63+
ecx.evaluate_added_goals_and_make_canonical_response(
64+
Certainty::AMBIGUOUS,
65+
),
66+
);
67+
}
68+
}
69+
Err(NoSolution) => returnSome(Err(NoSolution)),
70+
}
71+
}
72+
73+
None
74+
},
75+
|ecx| {
76+
ecx.probe(|&result| ProbeKind::RigidAlias{ result }).enter(|this| {
77+
this.structurally_instantiate_normalizes_to_term(
78+
goal,
79+
goal.predicate.alias,
80+
);
81+
this.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
82+
})
83+
},
84+
)
5185
}
5286
ty::AliasTermKind::InherentTy | ty::AliasTermKind::InherentConst => {
5387
self.normalize_inherent_associated_term(goal)
@@ -132,39 +166,7 @@ where
132166
then:implFnOnce(&mutEvalCtxt<'_,D>) -> QueryResult<I>,
133167
) -> QueryResult<I>{
134168
let cx = ecx.cx();
135-
// FIXME(generic_associated_types): Addresses aggressive inference in #92917.
136-
//
137-
// If this type is a GAT with currently unconstrained arguments, we do not
138-
// want to normalize it via a candidate which only applies for a specific
139-
// instantiation. We could otherwise keep the GAT as rigid and succeed this way.
140-
// See tests/ui/generic-associated-types/no-incomplete-gat-arg-inference.rs.
141-
//
142-
// This only avoids normalization if the GAT arguments are fully unconstrained.
143-
// This is quite arbitrary but fixing it causes some ambiguity, see #125196.
144-
match goal.predicate.alias.kind(cx){
145-
ty::AliasTermKind::ProjectionTy | ty::AliasTermKind::ProjectionConst => {
146-
for arg in goal.predicate.alias.own_args(cx).iter(){
147-
letSome(term) = arg.as_term()else{
148-
continue;
149-
};
150-
let term = ecx.structurally_normalize_term(goal.param_env, term)?;
151-
if term.is_infer(){
152-
return ecx.evaluate_added_goals_and_make_canonical_response(
153-
Certainty::AMBIGUOUS,
154-
);
155-
}
156-
}
157-
}
158-
ty::AliasTermKind::OpaqueTy
159-
| ty::AliasTermKind::InherentTy
160-
| ty::AliasTermKind::InherentConst
161-
| ty::AliasTermKind::FreeTy
162-
| ty::AliasTermKind::FreeConst
163-
| ty::AliasTermKind::UnevaluatedConst => {}
164-
}
165-
166169
let projection_pred = assumption.as_projection_clause().unwrap();
167-
168170
let assumption_projection_pred = ecx.instantiate_binder_with_infer(projection_pred);
169171
ecx.eq(goal.param_env, goal.predicate.alias, assumption_projection_pred.projection_term)?;
170172

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//@ check-pass
2+
//@ revisions: current next
3+
//@ ignore-compare-mode-next-solver (explicit revisions)
4+
//@[next] compile-flags: -Znext-solver
5+
6+
// Regression test for trait-system-refactor-initiative#256. The ambiguous
7+
// GAT arg check previously happened before checking whether the projection
8+
// candidate actually applied in the new trait solver.
9+
//
10+
// This meant we didn't consider `T::Assoc<_>` to be a rigid alias, resulting
11+
// in an inference failure.
12+
13+
pubtraitProj{
14+
typeAssoc<T>;
15+
}
16+
17+
traitId{
18+
typeThis;
19+
}
20+
impl<T>IdforT{
21+
typeThis = T;
22+
}
23+
24+
// This previously compiled as the "assumption would incompletely constrain GAT args"
25+
// check happened in each individual assumption after the `DeepRejectCtxt` fast path.
26+
fnwith_fast_reject<T,U>(x:T::Assoc<u32>)
27+
where
28+
T:Proj,
29+
U:Proj<Assoc<i32> = u32>,
30+
{
31+
let _:T::Assoc<_> = x;
32+
}
33+
34+
// This previously failed with ambiguity as that check did happen before we actually
35+
// equated the goal with the assumption. Due to the alias in the where-clause
36+
// we didn't fast-reject this candidate.
37+
fnno_fast_reject<T,U>(x:T::Assoc<u32>)
38+
where
39+
T:Proj,
40+
<UasId>::This:Proj<Assoc<i32> = u32>,
41+
{
42+
let _:T::Assoc<_> = x;
43+
}
44+
45+
fnmain(){}

0 commit comments

Comments
 (0)