Skip to content

Commit 77b09c2

Browse files
committed
Use fn ptr signature instead of {closure@..} in infer error
When suggesting a type on inference error, do not use `{closure@..}`. Instead, replace with an appropriate `fn` ptr. On the error message, use `short_ty_string` and write long types to disk. ``` error[E0284]: type annotations needed for `Select<{[email protected]:2782:13}, _, Expression<'_>, _>` --> crates/lang/src/parser.rs:41:13 | 41 | let lit = select! { | ^^^ 42 | Token::Int(i) = e => Expression::new(Expr::Lit(ast::Lit::Int(i.parse().unwrap())), e.span()), | ---- type must be known at this point | = note: the full type name has been written to '/home/gh-estebank/iowo/target/debug/deps/lang-e2d6e25819442273.long-type-4587393693885174369.txt' = note: cannot satisfy `<_ as chumsky::input::Input<'_>>::Span == SimpleSpan` help: consider giving `lit` an explicit type, where the type for type parameter `I` is specified | 41 | let lit: Select<for<'a, 'b> fn(tokens::Token<'_>, &'a mut MapExtra<'_, 'b, _, _>) -> Option<Expression<'_>>, _, Expression<'_>, _> = select! { | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ``` instead of ``` error[E0284]: type annotations needed for `Select<{closure@/home/gh-estebank/.cargo/registry/src/index.crates.io-6f17d22bba15001f/chumsky-1.0.0-alpha.6/src/lib.rs:2782:13: 2782:28}, _, Expression<'_>, _>` --> crates/lang/src/parser.rs:41:13 | 41 | let lit = select! { | ^^^ 42 | Token::Int(i) = e => Expression::new(Expr::Lit(ast::Lit::Int(i.parse().unwrap())), e.span()), | ---- type must be known at this point | = note: cannot satisfy `<_ as chumsky::input::Input<'_>>::Span == SimpleSpan` help: consider giving `lit` an explicit type, where the type for type parameter `I` is specified | 41 | let lit: Select<{closure@/home/gh-estebank/.cargo/registry/src/index.crates.io-6f17d22bba15001f/chumsky-1.0.0-alpha.6/src/lib.rs:2782:13: 2782:28}, _, Expression<'_>, _> = select! { | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ``` Fix rust-lang#123630.
1 parent a983dd8 commit 77b09c2

29 files changed

+96
-42
lines changed

compiler/rustc_infer/messages.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ infer_fps_items_are_distinct = fn items are distinct from fn pointers
144144
infer_fps_remove_ref = consider removing the reference
145145
infer_fps_use_ref = consider using a reference
146146
infer_fulfill_req_lifetime = the type `{$ty}` does not fulfill the required lifetime
147+
148+
infer_full_type_written = the full type name has been written to '{$path}'
149+
147150
infer_implicit_static_lifetime_note = this has an implicit `'static` lifetime requirement
148151
infer_implicit_static_lifetime_suggestion = consider relaxing the implicit `'static` requirement
149152
infer_label_bad = {$bad_kind ->

compiler/rustc_infer/src/errors/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ use crate::infer::error_reporting::{
1818
ObligationCauseAsDiagArg,
1919
};
2020

21+
use std::path::PathBuf;
22+
2123
pub mod note_and_explain;
2224

2325
#[derive(Diagnostic)]
@@ -47,6 +49,9 @@ pub struct AnnotationRequired<'a> {
4749
pub infer_subdiags: Vec<SourceKindSubdiag<'a>>,
4850
#[subdiagnostic]
4951
pub multi_suggestions: Vec<SourceKindMultiSuggestion<'a>>,
52+
#[note(infer_full_type_written)]
53+
pub was_written: Option<()>,
54+
pub path: PathBuf,
5055
}
5156

5257
// Copy of `AnnotationRequired` for E0283
@@ -65,6 +70,9 @@ pub struct AmbiguousImpl<'a> {
6570
pub infer_subdiags: Vec<SourceKindSubdiag<'a>>,
6671
#[subdiagnostic]
6772
pub multi_suggestions: Vec<SourceKindMultiSuggestion<'a>>,
73+
#[note(infer_full_type_written)]
74+
pub was_written: Option<()>,
75+
pub path: PathBuf,
6876
}
6977

7078
// Copy of `AnnotationRequired` for E0284
@@ -83,6 +91,9 @@ pub struct AmbiguousReturn<'a> {
8391
pub infer_subdiags: Vec<SourceKindSubdiag<'a>>,
8492
#[subdiagnostic]
8593
pub multi_suggestions: Vec<SourceKindMultiSuggestion<'a>>,
94+
#[note(infer_full_type_written)]
95+
pub was_written: Option<()>,
96+
pub path: PathBuf,
8697
}
8798

8899
// Used when a better one isn't available

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

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ use rustc_middle::infer::unify_key::{
1818
};
1919
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow};
2020
use rustc_middle::ty::print::{FmtPrinter, PrettyPrinter, Print, Printer};
21-
use rustc_middle::ty::{self, InferConst};
22-
use rustc_middle::ty::{GenericArg, GenericArgKind, GenericArgsRef};
23-
use rustc_middle::ty::{IsSuggestable, Ty, TyCtxt, TypeckResults};
21+
use rustc_middle::ty::{
22+
self, GenericArg, GenericArgKind, GenericArgsRef, InferConst, IsSuggestable, Ty, TyCtxt,
23+
TypeFoldable, TypeFolder, TypeSuperFoldable, TypeckResults,
24+
};
2425
use rustc_span::symbol::{kw, sym, Ident};
25-
use rustc_span::{BytePos, Span};
26+
use rustc_span::{BytePos, Span, DUMMY_SP};
2627
use std::borrow::Cow;
2728
use std::iter;
29+
use std::path::PathBuf;
2830

2931
pub enum TypeAnnotationNeeded {
3032
/// ```compile_fail,E0282
@@ -153,6 +155,29 @@ impl UnderspecifiedArgKind {
153155
}
154156
}
155157

158+
struct ClosureEraser<'tcx> {
159+
tcx: TyCtxt<'tcx>,
160+
}
161+
162+
impl<'tcx> TypeFolder<TyCtxt<'tcx>> for ClosureEraser<'tcx> {
163+
fn interner(&self) -> TyCtxt<'tcx> {
164+
self.tcx
165+
}
166+
167+
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
168+
match ty.kind() {
169+
ty::Closure(_, args) => {
170+
let closure_sig = args.as_closure().sig();
171+
Ty::new_fn_ptr(
172+
self.tcx,
173+
self.tcx.signature_unclosure(closure_sig, hir::Unsafety::Normal),
174+
)
175+
}
176+
_ => ty.super_fold_with(self),
177+
}
178+
}
179+
}
180+
156181
fn fmt_printer<'a, 'tcx>(infcx: &'a InferCtxt<'tcx>, ns: Namespace) -> FmtPrinter<'a, 'tcx> {
157182
let mut printer = FmtPrinter::new(infcx.tcx, ns);
158183
let ty_getter = move |ty_vid| {
@@ -209,6 +234,8 @@ fn ty_to_string<'tcx>(
209234
) -> String {
210235
let mut printer = fmt_printer(infcx, Namespace::TypeNS);
211236
let ty = infcx.resolve_vars_if_possible(ty);
237+
let ty = ty.fold_with(&mut ClosureEraser { tcx: infcx.tcx });
238+
212239
match (ty.kind(), called_method_def_id) {
213240
// We don't want the regular output for `fn`s because it includes its path in
214241
// invalid pseudo-syntax, we want the `fn`-pointer output instead.
@@ -387,6 +414,8 @@ impl<'tcx> InferCtxt<'tcx> {
387414
infer_subdiags,
388415
multi_suggestions,
389416
bad_label,
417+
was_written: None,
418+
path: Default::default(),
390419
}),
391420
TypeAnnotationNeeded::E0283 => self.dcx().create_err(AmbiguousImpl {
392421
span,
@@ -396,6 +425,8 @@ impl<'tcx> InferCtxt<'tcx> {
396425
infer_subdiags,
397426
multi_suggestions,
398427
bad_label,
428+
was_written: None,
429+
path: Default::default(),
399430
}),
400431
TypeAnnotationNeeded::E0284 => self.dcx().create_err(AmbiguousReturn {
401432
span,
@@ -405,6 +436,8 @@ impl<'tcx> InferCtxt<'tcx> {
405436
infer_subdiags,
406437
multi_suggestions,
407438
bad_label,
439+
was_written: None,
440+
path: Default::default(),
408441
}),
409442
}
410443
}
@@ -442,7 +475,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
442475
return self.bad_inference_failure_err(failure_span, arg_data, error_code);
443476
};
444477

445-
let (source_kind, name) = kind.ty_localized_msg(self);
478+
let (source_kind, name, path) = kind.ty_localized_msg(self);
446479
let failure_span = if should_label_span && !failure_span.overlaps(span) {
447480
Some(failure_span)
448481
} else {
@@ -518,15 +551,15 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
518551
GenericArgKind::Lifetime(_) => bug!("unexpected lifetime"),
519552
GenericArgKind::Type(_) => self
520553
.next_ty_var(TypeVariableOrigin {
521-
span: rustc_span::DUMMY_SP,
554+
span: DUMMY_SP,
522555
kind: TypeVariableOriginKind::MiscVariable,
523556
})
524557
.into(),
525558
GenericArgKind::Const(arg) => self
526559
.next_const_var(
527560
arg.ty(),
528561
ConstVariableOrigin {
529-
span: rustc_span::DUMMY_SP,
562+
span: DUMMY_SP,
530563
kind: ConstVariableOriginKind::MiscVariable,
531564
},
532565
)
@@ -547,7 +580,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
547580
}
548581
InferSourceKind::FullyQualifiedMethodCall { receiver, successor, args, def_id } => {
549582
let placeholder = Some(self.next_ty_var(TypeVariableOrigin {
550-
span: rustc_span::DUMMY_SP,
583+
span: DUMMY_SP,
551584
kind: TypeVariableOriginKind::MiscVariable,
552585
}));
553586
if let Some(args) = args.make_suggestable(self.infcx.tcx, true, placeholder) {
@@ -584,7 +617,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
584617
}
585618
InferSourceKind::ClosureReturn { ty, data, should_wrap_expr } => {
586619
let placeholder = Some(self.next_ty_var(TypeVariableOrigin {
587-
span: rustc_span::DUMMY_SP,
620+
span: DUMMY_SP,
588621
kind: TypeVariableOriginKind::MiscVariable,
589622
}));
590623
if let Some(ty) = ty.make_suggestable(self.infcx.tcx, true, placeholder) {
@@ -606,6 +639,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
606639
infer_subdiags,
607640
multi_suggestions,
608641
bad_label: None,
642+
was_written: path.as_ref().map(|_| ()),
643+
path: path.unwrap_or_default(),
609644
}),
610645
TypeAnnotationNeeded::E0283 => self.dcx().create_err(AmbiguousImpl {
611646
span,
@@ -615,6 +650,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
615650
infer_subdiags,
616651
multi_suggestions,
617652
bad_label: None,
653+
was_written: path.as_ref().map(|_| ()),
654+
path: path.unwrap_or_default(),
618655
}),
619656
TypeAnnotationNeeded::E0284 => self.dcx().create_err(AmbiguousReturn {
620657
span,
@@ -624,6 +661,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
624661
infer_subdiags,
625662
multi_suggestions,
626663
bad_label: None,
664+
was_written: path.as_ref().map(|_| ()),
665+
path: path.unwrap_or_default(),
627666
}),
628667
}
629668
}
@@ -688,22 +727,23 @@ impl<'tcx> InferSource<'tcx> {
688727
}
689728

690729
impl<'tcx> InferSourceKind<'tcx> {
691-
fn ty_localized_msg(&self, infcx: &InferCtxt<'tcx>) -> (&'static str, String) {
730+
fn ty_localized_msg(&self, infcx: &InferCtxt<'tcx>) -> (&'static str, String, Option<PathBuf>) {
731+
let mut path = None;
692732
match *self {
693733
InferSourceKind::LetBinding { ty, .. }
694734
| InferSourceKind::ClosureArg { ty, .. }
695735
| InferSourceKind::ClosureReturn { ty, .. } => {
696736
if ty.is_closure() {
697-
("closure", closure_as_fn_str(infcx, ty))
737+
("closure", closure_as_fn_str(infcx, ty), path)
698738
} else if !ty.is_ty_or_numeric_infer() {
699-
("normal", ty_to_string(infcx, ty, None))
739+
("normal", infcx.tcx.short_ty_string(ty, &mut path), path)
700740
} else {
701-
("other", String::new())
741+
("other", String::new(), path)
702742
}
703743
}
704744
// FIXME: We should be able to add some additional info here.
705745
InferSourceKind::GenericArg { .. }
706-
| InferSourceKind::FullyQualifiedMethodCall { .. } => ("other", String::new()),
746+
| InferSourceKind::FullyQualifiedMethodCall { .. } => ("other", String::new(), path),
707747
}
708748
}
709749
}

tests/ui/array-slice-vec/vector-no-ann.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0282]: type annotations needed for `Vec<T>`
1+
error[E0282]: type annotations needed for `Vec<_>`
22
--> $DIR/vector-no-ann.rs:2:9
33
|
44
LL | let _foo = Vec::new();

tests/ui/const-generics/defaults/doesnt_infer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ impl<const N: u32> Foo<N> {
99
fn main() {
1010
let foo = Foo::<1>::foo();
1111
let foo = Foo::foo();
12-
//~^ error: type annotations needed for `Foo<N>`
12+
//~^ ERROR type annotations needed for `Foo<_>`
1313
}

tests/ui/const-generics/defaults/doesnt_infer.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0282]: type annotations needed for `Foo<N>`
1+
error[E0282]: type annotations needed for `Foo<_>`
22
--> $DIR/doesnt_infer.rs:11:9
33
|
44
LL | let foo = Foo::foo();

tests/ui/const-generics/generic_arg_infer/issue-91614.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0283]: type annotations needed for `Mask<_, N>`
1+
error[E0283]: type annotations needed for `Mask<_, _>`
22
--> $DIR/issue-91614.rs:6:9
33
|
44
LL | let y = Mask::<_, _>::splat(false);

tests/ui/const-generics/generic_const_exprs/issue-62504.full.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ help: try adding a `where` bound
1818
LL | pub const fn new() -> Self where [(); Self::SIZE]: {
1919
| +++++++++++++++++++++++
2020

21-
error[E0282]: type annotations needed for `ArrayHolder<X>`
21+
error[E0282]: type annotations needed for `ArrayHolder<_>`
2222
--> $DIR/issue-62504.rs:26:9
2323
|
2424
LL | let mut array = ArrayHolder::new();

tests/ui/const-generics/generic_const_exprs/issue-62504.min.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ note: tuple struct defined here
2222
LL | struct ArrayHolder<const X: usize>([u32; X]);
2323
| ^^^^^^^^^^^
2424

25-
error[E0282]: type annotations needed for `ArrayHolder<X>`
25+
error[E0282]: type annotations needed for `ArrayHolder<_>`
2626
--> $DIR/issue-62504.rs:26:9
2727
|
2828
LL | let mut array = ArrayHolder::new();

tests/ui/generic-const-items/inference-failure.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0282]: type annotations needed for `Option<T>`
1+
error[E0282]: type annotations needed for `Option<_>`
22
--> $DIR/inference-failure.rs:8:9
33
|
44
LL | let _ = NONE;

0 commit comments

Comments
 (0)