Skip to content

Commit 7ee8b47

Browse files
committed
Make const trait aliases work in next solver
1 parent c60a01a commit 7ee8b47

File tree

5 files changed

+45
-46
lines changed

5 files changed

+45
-46
lines changed

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,9 +1013,11 @@ pub(super) fn const_conditions<'tcx>(
10131013
Node::Item(item) => match item.kind {
10141014
hir::ItemKind::Impl(impl_) => (impl_.generics, None, false),
10151015
hir::ItemKind::Fn { generics, .. } => (generics, None, false),
1016-
hir::ItemKind::TraitAlias(_, _, generics, supertraits)
1017-
| hir::ItemKind::Trait(_, _, _, _, generics, supertraits, _) => {
1018-
(generics, Some((item.owner_id.def_id, supertraits)), false)
1016+
hir::ItemKind::Trait(_, _, _, _, generics, supertraits, _) => {
1017+
(generics, Some((Some(item.owner_id.def_id), supertraits)), false)
1018+
}
1019+
hir::ItemKind::TraitAlias(_, _, generics, supertraits) => {
1020+
(generics, Some((None, supertraits)), false)
10191021
}
10201022
_ => bug!("const_conditions called on wrong item: {def_id:?}"),
10211023
},
@@ -1071,12 +1073,14 @@ pub(super) fn const_conditions<'tcx>(
10711073
}
10721074

10731075
if let Some((def_id, supertraits)) = trait_def_id_and_supertraits {
1074-
// We've checked above that the trait is conditionally const.
1075-
bounds.push((
1076-
ty::Binder::dummy(ty::TraitRef::identity(tcx, def_id.to_def_id()))
1077-
.to_host_effect_clause(tcx, ty::BoundConstness::Maybe),
1078-
DUMMY_SP,
1079-
));
1076+
if let Some(def_id) = def_id {
1077+
// We've checked above that the trait is conditionally const.
1078+
bounds.push((
1079+
ty::Binder::dummy(ty::TraitRef::identity(tcx, def_id.to_def_id()))
1080+
.to_host_effect_clause(tcx, ty::BoundConstness::Maybe),
1081+
DUMMY_SP,
1082+
));
1083+
}
10801084

10811085
icx.lowerer().lower_bounds(
10821086
tcx.types.self_param,

compiler/rustc_next_trait_solver/src/solve/effect_goals.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,35 @@ where
196196
}
197197

198198
fn consider_trait_alias_candidate(
199-
_ecx: &mut EvalCtxt<'_, D>,
200-
_goal: Goal<I, Self>,
199+
ecx: &mut EvalCtxt<'_, D>,
200+
goal: Goal<I, Self>,
201201
) -> Result<Candidate<I>, NoSolution> {
202-
unreachable!("trait aliases are never const")
202+
let cx = ecx.cx();
203+
204+
ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| {
205+
let where_clause_bounds = cx
206+
.predicates_of(goal.predicate.def_id())
207+
.iter_instantiated(cx, goal.predicate.trait_ref.args)
208+
.map(|p| goal.with(cx, p));
209+
ecx.add_goals(GoalSource::Misc, where_clause_bounds);
210+
211+
let const_conditions = cx
212+
.const_conditions(goal.predicate.def_id())
213+
.iter_instantiated(cx, goal.predicate.trait_ref.args)
214+
.map(|bound_trait_ref| {
215+
goal.with(
216+
cx,
217+
bound_trait_ref.to_host_effect_clause(cx, goal.predicate.constness),
218+
)
219+
});
220+
// While you could think of trait aliases to have a single builtin impl
221+
// which uses its implied trait bounds as where-clauses, using
222+
// `GoalSource::ImplWhereClause` here would be incorrect, as we also
223+
// impl them, which means we're "stepping out of the impl constructor"
224+
// again. To handle this, we treat these cycles as ambiguous for now.
225+
ecx.add_goals(GoalSource::Misc, const_conditions);
226+
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
227+
})
203228
}
204229

205230
fn consider_builtin_sizedness_candidates(
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:20:11
2+
--> $DIR/trait_alias.rs:22:11
33
|
44
LL | x.baz();
55
| ^^^
66

7-
error[E0277]: the trait bound `(): const Foo` is not satisfied
8-
--> $DIR/trait_alias.rs:25: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:16: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.pass.stderr

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

tests/ui/consts/trait_alias.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#![feature(trait_alias, const_trait_impl)]
22
//@ revisions: pass fail
3+
//@ compile-flags: -Znext-solver
4+
//@[pass] check-pass
35

46
const trait Bar {
57
fn bar(&self) {}
@@ -23,6 +25,5 @@ const fn foo<T: [const] Foo>(x: &T) {
2325
}
2426

2527
const _: () = foo(&());
26-
//~^ ERROR: `(): const Foo` is not satisfied
2728

2829
fn main() {}

0 commit comments

Comments
 (0)