Skip to content

Commit 075b3ce

Browse files
Attempt to normalize FnDef signature in InferCtxt::cmp
1 parent e69cad4 commit 075b3ce

File tree

6 files changed

+96
-2
lines changed

6 files changed

+96
-2
lines changed

compiler/rustc_infer/src/infer/at.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
7878
err_count_on_creation: self.err_count_on_creation,
7979
in_snapshot: self.in_snapshot.clone(),
8080
universe: self.universe.clone(),
81+
normalize_fn_sig_for_diagnostic: self
82+
.normalize_fn_sig_for_diagnostic
83+
.as_ref()
84+
.map(|f| f.clone()),
8185
}
8286
}
8387
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,12 +961,23 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
961961
}
962962
}
963963

964+
fn normalize_fn_sig_for_diagnostic(&self, sig: ty::PolyFnSig<'tcx>) -> ty::PolyFnSig<'tcx> {
965+
if let Some(normalize) = &self.normalize_fn_sig_for_diagnostic {
966+
normalize(self, sig)
967+
} else {
968+
sig
969+
}
970+
}
971+
964972
/// Given two `fn` signatures highlight only sub-parts that are different.
965973
fn cmp_fn_sig(
966974
&self,
967975
sig1: &ty::PolyFnSig<'tcx>,
968976
sig2: &ty::PolyFnSig<'tcx>,
969977
) -> (DiagnosticStyledString, DiagnosticStyledString) {
978+
let sig1 = &self.normalize_fn_sig_for_diagnostic(*sig1);
979+
let sig2 = &self.normalize_fn_sig_for_diagnostic(*sig2);
980+
970981
let get_lifetimes = |sig| {
971982
use rustc_hir::def::Namespace;
972983
let (_, sig, reg) = ty::print::FmtPrinter::new(self.tcx, Namespace::TypeNS)

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,9 @@ pub struct InferCtxt<'a, 'tcx> {
337337
/// when we enter into a higher-ranked (`for<..>`) type or trait
338338
/// bound.
339339
universe: Cell<ty::UniverseIndex>,
340+
341+
normalize_fn_sig_for_diagnostic:
342+
Option<Lrc<dyn Fn(&InferCtxt<'_, 'tcx>, ty::PolyFnSig<'tcx>) -> ty::PolyFnSig<'tcx>>>,
340343
}
341344

342345
/// See the `error_reporting` module for more details.
@@ -540,6 +543,8 @@ pub struct InferCtxtBuilder<'tcx> {
540543
defining_use_anchor: DefiningAnchor,
541544
considering_regions: bool,
542545
fresh_typeck_results: Option<RefCell<ty::TypeckResults<'tcx>>>,
546+
normalize_fn_sig_for_diagnostic:
547+
Option<Lrc<dyn Fn(&InferCtxt<'_, 'tcx>, ty::PolyFnSig<'tcx>) -> ty::PolyFnSig<'tcx>>>,
543548
}
544549

545550
pub trait TyCtxtInferExt<'tcx> {
@@ -553,6 +558,7 @@ impl<'tcx> TyCtxtInferExt<'tcx> for TyCtxt<'tcx> {
553558
defining_use_anchor: DefiningAnchor::Error,
554559
considering_regions: true,
555560
fresh_typeck_results: None,
561+
normalize_fn_sig_for_diagnostic: None,
556562
}
557563
}
558564
}
@@ -582,6 +588,14 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
582588
self
583589
}
584590

591+
pub fn with_normalize_fn_sig_for_diagnostic(
592+
mut self,
593+
fun: Lrc<dyn Fn(&InferCtxt<'_, 'tcx>, ty::PolyFnSig<'tcx>) -> ty::PolyFnSig<'tcx>>,
594+
) -> Self {
595+
self.normalize_fn_sig_for_diagnostic = Some(fun);
596+
self
597+
}
598+
585599
/// Given a canonical value `C` as a starting point, create an
586600
/// inference context that contains each of the bound values
587601
/// within instantiated as a fresh variable. The `f` closure is
@@ -611,6 +625,7 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
611625
defining_use_anchor,
612626
considering_regions,
613627
ref fresh_typeck_results,
628+
ref normalize_fn_sig_for_diagnostic,
614629
} = *self;
615630
let in_progress_typeck_results = fresh_typeck_results.as_ref();
616631
f(InferCtxt {
@@ -629,6 +644,9 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
629644
in_snapshot: Cell::new(false),
630645
skip_leak_check: Cell::new(false),
631646
universe: Cell::new(ty::UniverseIndex::ROOT),
647+
normalize_fn_sig_for_diagnostic: normalize_fn_sig_for_diagnostic
648+
.as_ref()
649+
.map(|f| f.clone()),
632650
})
633651
}
634652
}

compiler/rustc_typeck/src/check/inherited.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
use super::callee::DeferredCallResolution;
22

33
use rustc_data_structures::fx::FxHashSet;
4+
use rustc_data_structures::sync::Lrc;
45
use rustc_hir as hir;
56
use rustc_hir::def_id::LocalDefId;
67
use rustc_hir::HirIdMap;
78
use rustc_infer::infer;
89
use rustc_infer::infer::{InferCtxt, InferOk, TyCtxtInferExt};
10+
use rustc_infer::traits::TraitEngineExt as _;
911
use rustc_middle::ty::fold::TypeFoldable;
1012
use rustc_middle::ty::visit::TypeVisitable;
1113
use rustc_middle::ty::{self, Ty, TyCtxt};
1214
use rustc_span::def_id::LocalDefIdMap;
1315
use rustc_span::{self, Span};
1416
use rustc_trait_selection::infer::InferCtxtExt as _;
15-
use rustc_trait_selection::traits::{self, ObligationCause, TraitEngine, TraitEngineExt};
17+
use rustc_trait_selection::traits::{
18+
self, FulfillmentContext, ObligationCause, TraitEngine, TraitEngineExt as _,
19+
};
1620

1721
use std::cell::RefCell;
1822
use std::ops::Deref;
@@ -84,7 +88,29 @@ impl<'tcx> Inherited<'_, 'tcx> {
8488
infcx: tcx
8589
.infer_ctxt()
8690
.ignoring_regions()
87-
.with_fresh_in_progress_typeck_results(hir_owner),
91+
.with_fresh_in_progress_typeck_results(hir_owner)
92+
.with_normalize_fn_sig_for_diagnostic(Lrc::new(move |infcx, fn_sig| {
93+
if fn_sig.has_escaping_bound_vars() {
94+
return fn_sig;
95+
}
96+
infcx.probe(|_| {
97+
let traits::Normalized { value: normalized_fn_sig, obligations } =
98+
traits::normalize(
99+
&mut traits::SelectionContext::new(infcx),
100+
// FIXME(compiler-errors): This is probably not the right param-env...
101+
infcx.tcx.param_env(def_id),
102+
ObligationCause::dummy(),
103+
fn_sig,
104+
);
105+
let mut fulfillment_ctxt = FulfillmentContext::new_in_snapshot();
106+
fulfillment_ctxt.register_predicate_obligations(infcx, obligations);
107+
if fulfillment_ctxt.select_all_or_error(infcx).is_empty() {
108+
infcx.resolve_vars_if_possible(normalized_fn_sig)
109+
} else {
110+
fn_sig
111+
}
112+
})
113+
})),
88114
def_id,
89115
}
90116
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
trait Foo {
2+
type Bar;
3+
}
4+
5+
impl<T> Foo for T {
6+
type Bar = i32;
7+
}
8+
9+
fn foo<T>(_: <T as Foo>::Bar, _: &'static <T as Foo>::Bar) {}
10+
11+
fn needs_i32_ref_fn(_: fn(&'static i32, i32)) {}
12+
13+
fn main() {
14+
needs_i32_ref_fn(foo::<()>);
15+
//~^ ERROR mismatched types
16+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/normalize-fn-sig.rs:14:22
3+
|
4+
LL | needs_i32_ref_fn(foo::<()>);
5+
| ---------------- ^^^^^^^^^ expected `&i32`, found `i32`
6+
| |
7+
| arguments to this function are incorrect
8+
|
9+
= note: expected fn pointer `fn(&'static i32, i32)`
10+
found fn item `fn(i32, &'static i32) {foo::<()>}`
11+
note: function defined here
12+
--> $DIR/normalize-fn-sig.rs:11:4
13+
|
14+
LL | fn needs_i32_ref_fn(_: fn(&'static i32, i32)) {}
15+
| ^^^^^^^^^^^^^^^^ ------------------------
16+
17+
error: aborting due to previous error
18+
19+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)