Skip to content

Commit 4dd8813

Browse files
authored
Rollup merge of #126360 - compiler-errors:uplift-structural-traits, r=lcnr
Uplift `structural_traits.rs` into the new trait solver Self-explanatory. I will leave some comments inline regarding design decisions.
2 parents 977c5fd + c8e4206 commit 4dd8813

File tree

23 files changed

+824
-382
lines changed

23 files changed

+824
-382
lines changed

compiler/rustc_hir/src/lang_items.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,9 @@ language_item_table! {
244244
AsyncIterator, sym::async_iterator, async_iterator_trait, Target::Trait, GenericRequirement::Exact(0);
245245

246246
CoroutineState, sym::coroutine_state, coroutine_state, Target::Enum, GenericRequirement::None;
247-
Coroutine, sym::coroutine, coroutine_trait, Target::Trait, GenericRequirement::Minimum(1);
247+
Coroutine, sym::coroutine, coroutine_trait, Target::Trait, GenericRequirement::Exact(1);
248+
CoroutineReturn, sym::coroutine_return, coroutine_return, Target::AssocTy, GenericRequirement::Exact(1);
249+
CoroutineYield, sym::coroutine_yield, coroutine_yield, Target::AssocTy, GenericRequirement::Exact(1);
248250
CoroutineResume, sym::coroutine_resume, coroutine_resume, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
249251

250252
Unpin, sym::unpin, unpin_trait, Target::Trait, GenericRequirement::None;

compiler/rustc_infer/src/infer/at.rs

+67-14
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,50 @@ impl<'a, 'tcx> At<'a, 'tcx> {
225225
}
226226
}
227227

228+
/// Used in the new solver since we don't care about tracking an `ObligationCause`.
229+
pub fn relate_no_trace<T>(
230+
self,
231+
expected: T,
232+
variance: ty::Variance,
233+
actual: T,
234+
) -> Result<Vec<Goal<'tcx, ty::Predicate<'tcx>>>, NoSolution>
235+
where
236+
T: Relate<TyCtxt<'tcx>>,
237+
{
238+
let mut fields = CombineFields::new(
239+
self.infcx,
240+
TypeTrace::dummy(self.cause),
241+
self.param_env,
242+
DefineOpaqueTypes::Yes,
243+
);
244+
fields.sub().relate_with_variance(
245+
variance,
246+
ty::VarianceDiagInfo::default(),
247+
expected,
248+
actual,
249+
)?;
250+
Ok(fields.goals)
251+
}
252+
253+
/// Used in the new solver since we don't care about tracking an `ObligationCause`.
254+
pub fn eq_structurally_relating_aliases_no_trace<T>(
255+
self,
256+
expected: T,
257+
actual: T,
258+
) -> Result<Vec<Goal<'tcx, ty::Predicate<'tcx>>>, NoSolution>
259+
where
260+
T: Relate<TyCtxt<'tcx>>,
261+
{
262+
let mut fields = CombineFields::new(
263+
self.infcx,
264+
TypeTrace::dummy(self.cause),
265+
self.param_env,
266+
DefineOpaqueTypes::Yes,
267+
);
268+
fields.equate(StructurallyRelateAliases::Yes).relate(expected, actual)?;
269+
Ok(fields.goals)
270+
}
271+
228272
/// Computes the least-upper-bound, or mutual supertype, of two
229273
/// values. The order of the arguments doesn't matter, but since
230274
/// this can result in an error (e.g., if asked to compute LUB of
@@ -303,7 +347,7 @@ impl<'tcx> ToTrace<'tcx> for Ty<'tcx> {
303347
) -> TypeTrace<'tcx> {
304348
TypeTrace {
305349
cause: cause.clone(),
306-
values: Terms(ExpectedFound::new(a_is_expected, a.into(), b.into())),
350+
values: ValuePairs::Terms(ExpectedFound::new(a_is_expected, a.into(), b.into())),
307351
}
308352
}
309353
}
@@ -315,7 +359,10 @@ impl<'tcx> ToTrace<'tcx> for ty::Region<'tcx> {
315359
a: Self,
316360
b: Self,
317361
) -> TypeTrace<'tcx> {
318-
TypeTrace { cause: cause.clone(), values: Regions(ExpectedFound::new(a_is_expected, a, b)) }
362+
TypeTrace {
363+
cause: cause.clone(),
364+
values: ValuePairs::Regions(ExpectedFound::new(a_is_expected, a, b)),
365+
}
319366
}
320367
}
321368

@@ -328,7 +375,7 @@ impl<'tcx> ToTrace<'tcx> for Const<'tcx> {
328375
) -> TypeTrace<'tcx> {
329376
TypeTrace {
330377
cause: cause.clone(),
331-
values: Terms(ExpectedFound::new(a_is_expected, a.into(), b.into())),
378+
values: ValuePairs::Terms(ExpectedFound::new(a_is_expected, a.into(), b.into())),
332379
}
333380
}
334381
}
@@ -344,13 +391,13 @@ impl<'tcx> ToTrace<'tcx> for ty::GenericArg<'tcx> {
344391
cause: cause.clone(),
345392
values: match (a.unpack(), b.unpack()) {
346393
(GenericArgKind::Lifetime(a), GenericArgKind::Lifetime(b)) => {
347-
Regions(ExpectedFound::new(a_is_expected, a, b))
394+
ValuePairs::Regions(ExpectedFound::new(a_is_expected, a, b))
348395
}
349396
(GenericArgKind::Type(a), GenericArgKind::Type(b)) => {
350-
Terms(ExpectedFound::new(a_is_expected, a.into(), b.into()))
397+
ValuePairs::Terms(ExpectedFound::new(a_is_expected, a.into(), b.into()))
351398
}
352399
(GenericArgKind::Const(a), GenericArgKind::Const(b)) => {
353-
Terms(ExpectedFound::new(a_is_expected, a.into(), b.into()))
400+
ValuePairs::Terms(ExpectedFound::new(a_is_expected, a.into(), b.into()))
354401
}
355402

356403
(
@@ -379,7 +426,10 @@ impl<'tcx> ToTrace<'tcx> for ty::Term<'tcx> {
379426
a: Self,
380427
b: Self,
381428
) -> TypeTrace<'tcx> {
382-
TypeTrace { cause: cause.clone(), values: Terms(ExpectedFound::new(a_is_expected, a, b)) }
429+
TypeTrace {
430+
cause: cause.clone(),
431+
values: ValuePairs::Terms(ExpectedFound::new(a_is_expected, a, b)),
432+
}
383433
}
384434
}
385435

@@ -392,7 +442,7 @@ impl<'tcx> ToTrace<'tcx> for ty::TraitRef<'tcx> {
392442
) -> TypeTrace<'tcx> {
393443
TypeTrace {
394444
cause: cause.clone(),
395-
values: TraitRefs(ExpectedFound::new(a_is_expected, a, b)),
445+
values: ValuePairs::TraitRefs(ExpectedFound::new(a_is_expected, a, b)),
396446
}
397447
}
398448
}
@@ -406,7 +456,7 @@ impl<'tcx> ToTrace<'tcx> for ty::AliasTy<'tcx> {
406456
) -> TypeTrace<'tcx> {
407457
TypeTrace {
408458
cause: cause.clone(),
409-
values: Aliases(ExpectedFound::new(a_is_expected, a.into(), b.into())),
459+
values: ValuePairs::Aliases(ExpectedFound::new(a_is_expected, a.into(), b.into())),
410460
}
411461
}
412462
}
@@ -418,7 +468,10 @@ impl<'tcx> ToTrace<'tcx> for ty::AliasTerm<'tcx> {
418468
a: Self,
419469
b: Self,
420470
) -> TypeTrace<'tcx> {
421-
TypeTrace { cause: cause.clone(), values: Aliases(ExpectedFound::new(a_is_expected, a, b)) }
471+
TypeTrace {
472+
cause: cause.clone(),
473+
values: ValuePairs::Aliases(ExpectedFound::new(a_is_expected, a, b)),
474+
}
422475
}
423476
}
424477

@@ -431,7 +484,7 @@ impl<'tcx> ToTrace<'tcx> for ty::FnSig<'tcx> {
431484
) -> TypeTrace<'tcx> {
432485
TypeTrace {
433486
cause: cause.clone(),
434-
values: PolySigs(ExpectedFound::new(
487+
values: ValuePairs::PolySigs(ExpectedFound::new(
435488
a_is_expected,
436489
ty::Binder::dummy(a),
437490
ty::Binder::dummy(b),
@@ -449,7 +502,7 @@ impl<'tcx> ToTrace<'tcx> for ty::PolyFnSig<'tcx> {
449502
) -> TypeTrace<'tcx> {
450503
TypeTrace {
451504
cause: cause.clone(),
452-
values: PolySigs(ExpectedFound::new(a_is_expected, a, b)),
505+
values: ValuePairs::PolySigs(ExpectedFound::new(a_is_expected, a, b)),
453506
}
454507
}
455508
}
@@ -463,7 +516,7 @@ impl<'tcx> ToTrace<'tcx> for ty::PolyExistentialTraitRef<'tcx> {
463516
) -> TypeTrace<'tcx> {
464517
TypeTrace {
465518
cause: cause.clone(),
466-
values: ExistentialTraitRef(ExpectedFound::new(a_is_expected, a, b)),
519+
values: ValuePairs::ExistentialTraitRef(ExpectedFound::new(a_is_expected, a, b)),
467520
}
468521
}
469522
}
@@ -477,7 +530,7 @@ impl<'tcx> ToTrace<'tcx> for ty::PolyExistentialProjection<'tcx> {
477530
) -> TypeTrace<'tcx> {
478531
TypeTrace {
479532
cause: cause.clone(),
480-
values: ExistentialProjection(ExpectedFound::new(a_is_expected, a, b)),
533+
values: ValuePairs::ExistentialProjection(ExpectedFound::new(a_is_expected, a, b)),
481534
}
482535
}
483536
}

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -1707,6 +1707,9 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
17071707
ValuePairs::ExistentialProjection(_) => {
17081708
(false, Mismatch::Fixed("existential projection"))
17091709
}
1710+
ValuePairs::Dummy => {
1711+
bug!("do not expect to report a type error from a ValuePairs::Dummy")
1712+
}
17101713
};
17111714
let Some(vals) = self.values_str(values) else {
17121715
// Derived error. Cancel the emitter.
@@ -2250,12 +2253,12 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
22502253
values: ValuePairs<'tcx>,
22512254
) -> Option<(DiagStyledString, DiagStyledString, Option<PathBuf>)> {
22522255
match values {
2253-
infer::Regions(exp_found) => self.expected_found_str(exp_found),
2254-
infer::Terms(exp_found) => self.expected_found_str_term(exp_found),
2255-
infer::Aliases(exp_found) => self.expected_found_str(exp_found),
2256-
infer::ExistentialTraitRef(exp_found) => self.expected_found_str(exp_found),
2257-
infer::ExistentialProjection(exp_found) => self.expected_found_str(exp_found),
2258-
infer::TraitRefs(exp_found) => {
2256+
ValuePairs::Regions(exp_found) => self.expected_found_str(exp_found),
2257+
ValuePairs::Terms(exp_found) => self.expected_found_str_term(exp_found),
2258+
ValuePairs::Aliases(exp_found) => self.expected_found_str(exp_found),
2259+
ValuePairs::ExistentialTraitRef(exp_found) => self.expected_found_str(exp_found),
2260+
ValuePairs::ExistentialProjection(exp_found) => self.expected_found_str(exp_found),
2261+
ValuePairs::TraitRefs(exp_found) => {
22592262
let pretty_exp_found = ty::error::ExpectedFound {
22602263
expected: exp_found.expected.print_trait_sugared(),
22612264
found: exp_found.found.print_trait_sugared(),
@@ -2267,14 +2270,17 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
22672270
ret => ret,
22682271
}
22692272
}
2270-
infer::PolySigs(exp_found) => {
2273+
ValuePairs::PolySigs(exp_found) => {
22712274
let exp_found = self.resolve_vars_if_possible(exp_found);
22722275
if exp_found.references_error() {
22732276
return None;
22742277
}
22752278
let (exp, fnd) = self.cmp_fn_sig(&exp_found.expected, &exp_found.found);
22762279
Some((exp, fnd, None))
22772280
}
2281+
ValuePairs::Dummy => {
2282+
bug!("do not expect to report a type error from a ValuePairs::Dummy")
2283+
}
22782284
}
22792285
}
22802286

0 commit comments

Comments
 (0)