Skip to content

Commit 943c0f2

Browse files
committed
add next solver trait assembly
1 parent d05a184 commit 943c0f2

File tree

8 files changed

+66
-10
lines changed

8 files changed

+66
-10
lines changed

compiler/rustc_middle/src/ty/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,8 @@ bidirectional_lang_item_map! {
807807
CoroutineReturn,
808808
CoroutineYield,
809809
DynMetadata,
810+
FieldBase,
811+
FieldType,
810812
FutureOutput,
811813
Metadata,
812814
// tidy-alphabetical-end

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,11 @@ where
351351
ecx: &mut EvalCtxt<'_, D>,
352352
goal: Goal<I, Self>,
353353
) -> Vec<Candidate<I>>;
354+
355+
fn consider_builtin_field_candidate(
356+
ecx: &mut EvalCtxt<'_, D>,
357+
goal: Goal<I, Self>,
358+
) -> Result<Candidate<I>, NoSolution>;
354359
}
355360

356361
/// Allows callers of `assemble_and_evaluate_candidates` to choose whether to limit
@@ -606,6 +611,7 @@ where
606611
Some(SolverTraitLangItem::BikeshedGuaranteedNoDrop) => {
607612
G::consider_builtin_bikeshed_guaranteed_no_drop_candidate(self, goal)
608613
}
614+
Some(SolverTraitLangItem::Field) => G::consider_builtin_field_candidate(self, goal),
609615
_ => Err(NoSolution),
610616
}
611617
};

compiler/rustc_next_trait_solver/src/solve/effect_goals.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,13 @@ where
385385
) -> Vec<Candidate<I>> {
386386
unreachable!("Unsize is not const")
387387
}
388+
389+
fn consider_builtin_field_candidate(
390+
_ecx: &mut EvalCtxt<'_, D>,
391+
_goal: Goal<I, Self>,
392+
) -> Result<Candidate<I>, NoSolution> {
393+
unreachable!("Field is not const")
394+
}
388395
}
389396

390397
impl<D, I> EvalCtxt<'_, D>

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,26 @@ where
970970
) -> Result<Candidate<I>, NoSolution> {
971971
unreachable!("`BikeshedGuaranteedNoDrop` does not have an associated type: {:?}", goal)
972972
}
973+
fn consider_builtin_field_candidate(
974+
ecx: &mut EvalCtxt<'_, D>,
975+
goal: Goal<<D as SolverDelegate>::Interner, Self>,
976+
) -> Result<Candidate<I>, NoSolution> {
977+
let cx = ecx.cx();
978+
let ty::Field(container, field_path) = goal.predicate.self_ty().kind() else {
979+
panic!("only `field_of!()` can implement `Field`")
980+
};
981+
let ty = if cx.is_lang_item(goal.predicate.def_id(), SolverLangItem::FieldBase) {
982+
container
983+
} else if cx.is_lang_item(goal.predicate.def_id(), SolverLangItem::FieldType) {
984+
field_path.field_ty(cx, container)
985+
} else {
986+
panic!("unexpected associated type {:?} in `Field`", goal.predicate)
987+
};
988+
ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| {
989+
ecx.instantiate_normalizes_to_term(goal, ty.into());
990+
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
991+
})
992+
}
973993
}
974994

975995
impl<D, I> EvalCtxt<'_, D>

compiler/rustc_next_trait_solver/src/solve/trait_goals.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,22 @@ where
846846
}
847847
})
848848
}
849+
850+
fn consider_builtin_field_candidate(
851+
ecx: &mut EvalCtxt<'_, D>,
852+
goal: Goal<I, Self>,
853+
) -> Result<Candidate<I>, NoSolution> {
854+
if goal.predicate.polarity != ty::PredicatePolarity::Positive {
855+
return Err(NoSolution);
856+
}
857+
858+
match goal.predicate.self_ty().kind() {
859+
ty::Field(..) => ecx
860+
.probe_builtin_trait_candidate(BuiltinImplSource::Misc)
861+
.enter(|ecx| ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)),
862+
_ => Err(NoSolution),
863+
}
864+
}
849865
}
850866

851867
/// Small helper function to change the `def_id` of a trait predicate - this is not normally

compiler/rustc_type_ir/src/lang_items.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ pub enum SolverLangItem {
99
CoroutineReturn,
1010
CoroutineYield,
1111
DynMetadata,
12+
FieldBase,
13+
FieldType,
1214
FutureOutput,
1315
Metadata,
1416
// tidy-alphabetical-end
Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
error[E0322]: explicit impls for the `UnalignedField` trait are not permitted
2-
--> $DIR/deny-manual-impl.rs:11:1
3-
|
4-
LL | unsafe impl UnalignedField for MyStruct {
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl of `UnalignedField` not allowed
6-
71
error[E0322]: explicit impls for the `Field` trait are not permitted
8-
--> $DIR/deny-manual-impl.rs:18:1
2+
--> $DIR/deny-manual-impl.rs:11:1
93
|
10-
LL | unsafe impl Field for field_of!(MyStruct, 0) {}
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl of `Field` not allowed
4+
LL | unsafe impl Field for MyStruct {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl of `Field` not allowed
126

13-
error: aborting due to 2 previous errors
7+
error: aborting due to 1 previous error
148

159
For more information about this error, try `rustc --explain E0322`.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0322]: explicit impls for the `Field` trait are not permitted
2+
--> $DIR/deny-manual-impl.rs:11:1
3+
|
4+
LL | unsafe impl Field for MyStruct {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl of `Field` not allowed
6+
7+
error: aborting due to 1 previous error
8+
9+
For more information about this error, try `rustc --explain E0322`.

0 commit comments

Comments
 (0)