Skip to content

Commit 8362e60

Browse files
committed
Make const trait aliases work in the old solver
1 parent 567c74d commit 8362e60

File tree

5 files changed

+44
-35
lines changed

5 files changed

+44
-35
lines changed

compiler/rustc_trait_selection/src/traits/effects.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ pub fn evaluate_host_effect_obligation<'tcx>(
6969
Err(EvaluationFailure::NoSolution) => {}
7070
}
7171

72+
match evaluate_host_effect_from_trait_alias(selcx, obligation) {
73+
Ok(result) => return Ok(result),
74+
Err(EvaluationFailure::Ambiguous) => return Err(EvaluationFailure::Ambiguous),
75+
Err(EvaluationFailure::NoSolution) => {}
76+
}
77+
7278
Err(EvaluationFailure::NoSolution)
7379
}
7480

@@ -498,3 +504,37 @@ fn evaluate_host_effect_from_selection_candidate<'tcx>(
498504
}
499505
})
500506
}
507+
508+
fn evaluate_host_effect_from_trait_alias<'tcx>(
509+
selcx: &mut SelectionContext<'_, 'tcx>,
510+
obligation: &HostEffectObligation<'tcx>,
511+
) -> Result<ThinVec<PredicateObligation<'tcx>>, EvaluationFailure> {
512+
let tcx = selcx.tcx();
513+
let def_id = obligation.predicate.def_id();
514+
if !tcx.trait_is_alias(def_id) {
515+
return Err(EvaluationFailure::NoSolution);
516+
}
517+
518+
Ok(tcx
519+
.const_conditions(def_id)
520+
.instantiate(tcx, obligation.predicate.trait_ref.args)
521+
.into_iter()
522+
.map(|(trait_ref, span)| {
523+
Obligation::new(
524+
tcx,
525+
obligation.cause.clone().derived_host_cause(
526+
ty::Binder::dummy(obligation.predicate),
527+
|derived| {
528+
ObligationCauseCode::ImplDerivedHost(Box::new(ImplDerivedHostCause {
529+
derived,
530+
impl_def_id: def_id,
531+
span,
532+
}))
533+
},
534+
),
535+
obligation.param_env,
536+
trait_ref.to_host_effect_clause(tcx, obligation.predicate.constness),
537+
)
538+
})
539+
.collect())
540+
}
Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,9 @@
11
error[E0277]: the trait bound `T: [const] Baz` is not satisfied
2-
--> $DIR/trait_alias.rs:23:11
2+
--> $DIR/trait_alias.rs:24:11
33
|
44
LL | x.baz();
55
| ^^^
66

7-
error[E0277]: the trait bound `(): const Foo` is not satisfied
8-
--> $DIR/trait_alias.rs:28:19
9-
|
10-
LL | const _: () = foo(&());
11-
| --- ^^^
12-
| |
13-
| required by a bound introduced by this call
14-
|
15-
note: required by a bound in `foo`
16-
--> $DIR/trait_alias.rs:19:17
17-
|
18-
LL | const fn foo<T: [const] Foo>(x: &T) {
19-
| ^^^^^^^^^^^ required by this bound in `foo`
20-
21-
error: aborting due to 2 previous errors
7+
error: aborting due to 1 previous error
228

239
For more information about this error, try `rustc --explain E0277`.

tests/ui/consts/trait_alias.next_fail.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0277]: the trait bound `T: [const] Baz` is not satisfied
2-
--> $DIR/trait_alias.rs:23:11
2+
--> $DIR/trait_alias.rs:24:11
33
|
44
LL | x.baz();
55
| ^^^

tests/ui/consts/trait_alias.pass.stderr

Lines changed: 0 additions & 17 deletions
This file was deleted.

tests/ui/consts/trait_alias.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//@[next_pass] compile-flags: -Znext-solver
44
//@[next_fail] compile-flags: -Znext-solver
55
//@[next_pass] check-pass
6+
//@[pass] check-pass
67

78
const trait Bar {
89
fn bar(&self) {}
@@ -26,6 +27,5 @@ const fn foo<T: [const] Foo>(x: &T) {
2627
}
2728

2829
const _: () = foo(&());
29-
//[fail,pass]~^ ERROR: `(): const Foo` is not satisfied
3030

3131
fn main() {}

0 commit comments

Comments
 (0)