Skip to content

Commit d92a1bd

Browse files
authored
Rollup merge of #116073 - compiler-errors:poly-sigs, r=b-naber
Allow higher-ranked fn sigs in `ValuePairs` For better bookkeeping -- only affects diagnostic path. Allow reporting signature mismatches like "signature"s and not "fn pointer"s. Improves #115897 (comment)
2 parents 13e6f24 + 8cfd249 commit d92a1bd

File tree

11 files changed

+48
-27
lines changed

11 files changed

+48
-27
lines changed

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1134,7 +1134,10 @@ fn report_trait_method_mismatch<'tcx>(
11341134
&mut diag,
11351135
&cause,
11361136
trait_err_span.map(|sp| (sp, Cow::from("type in trait"))),
1137-
Some(infer::ValuePairs::Sigs(ExpectedFound { expected: trait_sig, found: impl_sig })),
1137+
Some(infer::ValuePairs::PolySigs(ExpectedFound {
1138+
expected: ty::Binder::dummy(trait_sig),
1139+
found: ty::Binder::dummy(impl_sig),
1140+
})),
11381141
terr,
11391142
false,
11401143
false,

compiler/rustc_hir_analysis/src/check/mod.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -573,10 +573,7 @@ pub fn check_function_signature<'tcx>(
573573
let norm_cause = ObligationCause::misc(cause.span, local_id);
574574
let actual_sig = ocx.normalize(&norm_cause, param_env, actual_sig);
575575

576-
let expected_ty = Ty::new_fn_ptr(tcx, expected_sig);
577-
let actual_ty = Ty::new_fn_ptr(tcx, actual_sig);
578-
579-
match ocx.eq(&cause, param_env, expected_ty, actual_ty) {
576+
match ocx.eq(&cause, param_env, expected_sig, actual_sig) {
580577
Ok(()) => {
581578
let errors = ocx.select_all_or_error();
582579
if !errors.is_empty() {
@@ -595,9 +592,9 @@ pub fn check_function_signature<'tcx>(
595592
&mut diag,
596593
&cause,
597594
None,
598-
Some(infer::ValuePairs::Sigs(ExpectedFound {
599-
expected: tcx.liberate_late_bound_regions(fn_id, expected_sig),
600-
found: tcx.liberate_late_bound_regions(fn_id, actual_sig),
595+
Some(infer::ValuePairs::PolySigs(ExpectedFound {
596+
expected: expected_sig,
597+
found: actual_sig,
601598
})),
602599
err,
603600
false,

compiler/rustc_infer/src/infer/at.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,28 @@ impl<'tcx> ToTrace<'tcx> for ty::FnSig<'tcx> {
478478
a: Self,
479479
b: Self,
480480
) -> TypeTrace<'tcx> {
481-
TypeTrace { cause: cause.clone(), values: Sigs(ExpectedFound::new(a_is_expected, a, b)) }
481+
TypeTrace {
482+
cause: cause.clone(),
483+
values: PolySigs(ExpectedFound::new(
484+
a_is_expected,
485+
ty::Binder::dummy(a),
486+
ty::Binder::dummy(b),
487+
)),
488+
}
489+
}
490+
}
491+
492+
impl<'tcx> ToTrace<'tcx> for ty::PolyFnSig<'tcx> {
493+
fn to_trace(
494+
cause: &ObligationCause<'tcx>,
495+
a_is_expected: bool,
496+
a: Self,
497+
b: Self,
498+
) -> TypeTrace<'tcx> {
499+
TypeTrace {
500+
cause: cause.clone(),
501+
values: PolySigs(ExpectedFound::new(a_is_expected, a, b)),
502+
}
482503
}
483504
}
484505

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

+3-6
Original file line numberDiff line numberDiff line change
@@ -1660,7 +1660,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
16601660
_ => (false, Mismatch::Fixed("type")),
16611661
}
16621662
}
1663-
ValuePairs::Sigs(infer::ExpectedFound { expected, found }) => {
1663+
ValuePairs::PolySigs(infer::ExpectedFound { expected, found }) => {
16641664
OpaqueTypesVisitor::visit_expected_found(self.tcx, expected, found, span)
16651665
.report(diag);
16661666
(false, Mismatch::Fixed("signature"))
@@ -2232,15 +2232,12 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
22322232
ret => ret,
22332233
}
22342234
}
2235-
infer::Sigs(exp_found) => {
2235+
infer::PolySigs(exp_found) => {
22362236
let exp_found = self.resolve_vars_if_possible(exp_found);
22372237
if exp_found.references_error() {
22382238
return None;
22392239
}
2240-
let (exp, fnd) = self.cmp_fn_sig(
2241-
&ty::Binder::dummy(exp_found.expected),
2242-
&ty::Binder::dummy(exp_found.found),
2243-
);
2240+
let (exp, fnd) = self.cmp_fn_sig(&exp_found.expected, &exp_found.found);
22442241
Some((exp, fnd, None, None))
22452242
}
22462243
}

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/trait_impl_difference.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
3535
&& let (Subtype(sup_trace), Subtype(sub_trace)) = (&sup_origin, &sub_origin)
3636
&& let CompareImplItemObligation { trait_item_def_id, .. } = sub_trace.cause.code()
3737
&& sub_trace.values == sup_trace.values
38-
&& let ValuePairs::Sigs(ExpectedFound { expected, found }) = sub_trace.values
38+
&& let ValuePairs::PolySigs(ExpectedFound { expected, found }) = sub_trace.values
3939
{
4040
// FIXME(compiler-errors): Don't like that this needs `Ty`s, but
4141
// all of the region highlighting machinery only deals with those.
4242
let guar = self.emit_err(
4343
var_origin.span(),
44-
Ty::new_fn_ptr(self.cx.tcx,ty::Binder::dummy(expected)),
45-
Ty::new_fn_ptr(self.cx.tcx,ty::Binder::dummy(found)),
44+
Ty::new_fn_ptr(self.cx.tcx, expected),
45+
Ty::new_fn_ptr(self.cx.tcx, found),
4646
*trait_item_def_id,
4747
);
4848
return Some(guar);

compiler/rustc_infer/src/infer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ pub enum ValuePairs<'tcx> {
383383
Aliases(ExpectedFound<ty::AliasTy<'tcx>>),
384384
TraitRefs(ExpectedFound<ty::TraitRef<'tcx>>),
385385
PolyTraitRefs(ExpectedFound<ty::PolyTraitRef<'tcx>>),
386-
Sigs(ExpectedFound<ty::FnSig<'tcx>>),
386+
PolySigs(ExpectedFound<ty::PolyFnSig<'tcx>>),
387387
ExistentialTraitRef(ExpectedFound<ty::PolyExistentialTraitRef<'tcx>>),
388388
ExistentialProjection(ExpectedFound<ty::PolyExistentialProjection<'tcx>>),
389389
}

compiler/rustc_passes/src/check_attr.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -2333,7 +2333,10 @@ impl CheckAttrVisitor<'_> {
23332333
&mut diag,
23342334
&cause,
23352335
None,
2336-
Some(ValuePairs::Sigs(ExpectedFound { expected: expected_sig, found: sig })),
2336+
Some(ValuePairs::PolySigs(ExpectedFound {
2337+
expected: ty::Binder::dummy(expected_sig),
2338+
found: ty::Binder::dummy(sig),
2339+
})),
23372340
terr,
23382341
false,
23392342
false,

tests/ui/panic-handler/panic-handler-bad-signature-1.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ error[E0308]: `#[panic_handler]` function has wrong type
44
LL | fn panic(info: PanicInfo) -> () {}
55
| ^^^^^^^^^ expected `&PanicInfo<'_>`, found `PanicInfo<'_>`
66
|
7-
= note: expected signature `fn(&PanicInfo<'_>) -> !`
8-
found signature `fn(PanicInfo<'_>)`
7+
= note: expected signature `for<'a, 'b> fn(&'a PanicInfo<'b>) -> !`
8+
found signature `for<'a> fn(PanicInfo<'a>)`
99

1010
error: aborting due to previous error
1111

tests/ui/panic-handler/panic-handler-bad-signature-2.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ error[E0308]: `#[panic_handler]` function has wrong type
44
LL | fn panic(info: &'static PanicInfo) -> !
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
66
|
7-
= note: expected fn pointer `for<'a, 'b> fn(&'a PanicInfo<'b>) -> _`
8-
found fn pointer `for<'a> fn(&'static PanicInfo<'a>) -> _`
7+
= note: expected signature `for<'a, 'b> fn(&'a PanicInfo<'b>) -> _`
8+
found signature `for<'a> fn(&'static PanicInfo<'a>) -> _`
99

1010
error: aborting due to previous error
1111

tests/ui/panic-handler/panic-handler-bad-signature-3.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0308]: `#[panic_handler]` function has wrong type
44
LL | fn panic() -> ! {
55
| ^^^^^^^^^^^^^^^ incorrect number of function parameters
66
|
7-
= note: expected signature `fn(&PanicInfo<'_>) -> _`
7+
= note: expected signature `for<'a, 'b> fn(&'a PanicInfo<'b>) -> _`
88
found signature `fn() -> _`
99

1010
error: aborting due to previous error

tests/ui/panic-handler/panic-handler-bad-signature-5.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ error[E0308]: `#[panic_handler]` function has wrong type
44
LL | fn panic(info: &PanicInfo<'static>) -> !
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
66
|
7-
= note: expected fn pointer `for<'a, 'b> fn(&'a PanicInfo<'b>) -> _`
8-
found fn pointer `for<'a> fn(&'a PanicInfo<'static>) -> _`
7+
= note: expected signature `for<'a, 'b> fn(&'a PanicInfo<'b>) -> _`
8+
found signature `for<'a> fn(&'a PanicInfo<'static>) -> _`
99

1010
error: aborting due to previous error
1111

0 commit comments

Comments
 (0)