Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit e40d5e8

Browse files
committed
Auto merge of rust-lang#89862 - lcnr:path-generics-diagnostics, r=estebank
rewrite error handling for unresolved inference vars Pretty much completely rewrites `fn emit_inference_failure_err`. This new setup should hopefully be easier to extend and is already a lot better when looking for generic arguments. Because this is a rewrite there are still some parts which are lacking, these are tracked in rust-lang#94483 and will be fixed in later PRs. r? `@estebank` `@petrochenkov`
2 parents 72f7e31 + b343a46 commit e40d5e8

File tree

124 files changed

+2132
-1481
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

124 files changed

+2132
-1481
lines changed

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

Lines changed: 829 additions & 886 deletions
Large diffs are not rendered by default.

compiler/rustc_infer/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#![feature(let_else)]
2323
#![feature(min_specialization)]
2424
#![feature(never_type)]
25+
#![feature(try_blocks)]
2526
#![recursion_limit = "512"] // For rustdoc
2627

2728
#[macro_use]

compiler/rustc_middle/src/hir/map/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,11 @@ impl<'hir> Map<'hir> {
983983
Node::AnonConst(constant) => self.body(constant.body).value.span,
984984
Node::Expr(expr) => expr.span,
985985
Node::Stmt(stmt) => stmt.span,
986-
Node::PathSegment(seg) => seg.ident.span,
986+
Node::PathSegment(seg) => {
987+
let ident_span = seg.ident.span;
988+
ident_span
989+
.with_hi(seg.args.map_or_else(|| ident_span.hi(), |args| args.span_ext.hi()))
990+
}
987991
Node::Ty(ty) => ty.span,
988992
Node::TraitRef(tr) => tr.path.span,
989993
Node::Binding(pat) => pat.span,

compiler/rustc_middle/src/ty/generics.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,29 @@ impl GenericParamDef {
6363
bug!("cannot convert a non-lifetime parameter def to an early bound region")
6464
}
6565
}
66+
67+
pub fn has_default(&self) -> bool {
68+
match self.kind {
69+
GenericParamDefKind::Type { has_default, .. }
70+
| GenericParamDefKind::Const { has_default } => has_default,
71+
GenericParamDefKind::Lifetime => false,
72+
}
73+
}
74+
75+
pub fn default_value<'tcx>(
76+
&self,
77+
tcx: TyCtxt<'tcx>,
78+
) -> Option<EarlyBinder<ty::GenericArg<'tcx>>> {
79+
match self.kind {
80+
GenericParamDefKind::Type { has_default, .. } if has_default => {
81+
Some(EarlyBinder(tcx.type_of(self.def_id).into()))
82+
}
83+
GenericParamDefKind::Const { has_default } if has_default => {
84+
Some(EarlyBinder(tcx.const_param_default(self.def_id).into()))
85+
}
86+
_ => None,
87+
}
88+
}
6689
}
6790

6891
#[derive(Default)]
@@ -204,6 +227,12 @@ impl<'tcx> Generics {
204227
matches!(param.kind, ty::GenericParamDefKind::Type { synthetic: true, .. })
205228
})
206229
}
230+
231+
/// Returns the substs corresponding to the generic parameters of this item, excluding `Self`.
232+
pub fn own_substs(&'tcx self, substs: SubstsRef<'tcx>) -> &'tcx [ty::GenericArg<'tcx>] {
233+
let own = &substs[self.parent_count..][..self.params.len()];
234+
if self.has_self && self.parent.is_none() { &own[1..] } else { &own }
235+
}
207236
}
208237

209238
/// Bounds on generics.

src/test/ui/array-slice-vec/infer_array_len.stderr

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ error[E0282]: type annotations needed
22
--> $DIR/infer_array_len.rs:19:9
33
|
44
LL | let [_, _] = a.into();
5-
| ^^^^^^ consider giving this pattern a type
5+
| ^^^^^^
66
|
77
= note: type must be known at this point
8+
help: consider giving this pattern a type
9+
|
10+
LL | let [_, _]: _ = a.into();
11+
| +++
812

913
error: aborting due to previous error
1014

src/test/ui/array-slice-vec/vector-no-ann.stderr

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
error[E0282]: type annotations needed for `Vec<T>`
2-
--> $DIR/vector-no-ann.rs:2:16
2+
--> $DIR/vector-no-ann.rs:2:9
33
|
44
LL | let _foo = Vec::new();
5-
| ---- ^^^^^^^^ cannot infer type for type parameter `T`
6-
| |
7-
| consider giving `_foo` the explicit type `Vec<T>`, where the type parameter `T` is specified
5+
| ^^^^
6+
|
7+
help: consider giving `_foo` an explicit type, where the type for type parameter `T` is specified
8+
|
9+
LL | let _foo: Vec<T> = Vec::new();
10+
| ++++++++
811

912
error: aborting due to previous error
1013

src/test/ui/closure-expected-type/expect-two-infer-vars-supply-ty-with-bound-region.stderr

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ error[E0282]: type annotations needed
22
--> $DIR/expect-two-infer-vars-supply-ty-with-bound-region.rs:8:27
33
|
44
LL | with_closure(|x: u32, y| {});
5-
| ^ consider giving this closure parameter a type
5+
| ^
6+
|
7+
help: consider giving this closure parameter an explicit type
8+
|
9+
LL | with_closure(|x: u32, y: B| {});
10+
| +++
611

712
error: aborting due to previous error
813

src/test/ui/closures/issue-52437.stderr

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ error[E0282]: type annotations needed
88
--> $DIR/issue-52437.rs:2:30
99
|
1010
LL | [(); &(&'static: loop { |x| {}; }) as *const _ as usize]
11-
| ^ consider giving this closure parameter a type
11+
| ^
12+
|
13+
help: consider giving this closure parameter an explicit type
14+
|
15+
LL | [(); &(&'static: loop { |x: _| {}; }) as *const _ as usize]
16+
| +++
1217

1318
error[E0308]: mismatched types
1419
--> $DIR/issue-52437.rs:2:5

src/test/ui/const-generics/defaults/doesnt_infer.stderr

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
error[E0282]: type annotations needed for `Foo<N>`
2-
--> $DIR/doesnt_infer.rs:11:15
2+
--> $DIR/doesnt_infer.rs:11:9
33
|
44
LL | let foo = Foo::foo();
5-
| --- ^^^^^^^^ cannot infer the value of const parameter `N`
6-
| |
7-
| consider giving `foo` the explicit type `Foo<N>`, where the const parameter `N` is specified
5+
| ^^^
6+
|
7+
help: consider giving `foo` an explicit type, where the the value of const parameter `N` is specified
8+
|
9+
LL | let foo: Foo<N> = Foo::foo();
10+
| ++++++++
811

912
error: aborting due to previous error
1013

src/test/ui/const-generics/generic_arg_infer/issue-91614.stderr

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
error[E0283]: type annotations needed for `Mask<_, LANES>`
2-
--> $DIR/issue-91614.rs:6:13
2+
--> $DIR/issue-91614.rs:6:9
33
|
44
LL | let y = Mask::<_, _>::splat(false);
5-
| - ^^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `T`
6-
| |
7-
| consider giving `y` the explicit type `Mask<_, LANES>`, where the type parameter `T` is specified
5+
| ^
86
|
97
= note: cannot satisfy `_: MaskElement`
108
note: required by a bound in `Mask::<T, LANES>::splat`
119
--> $SRC_DIR/core/src/../../portable-simd/crates/core_simd/src/masks.rs:LL:COL
1210
|
1311
LL | T: MaskElement,
1412
| ^^^^^^^^^^^ required by this bound in `Mask::<T, LANES>::splat`
13+
help: consider giving `y` an explicit type, where the type for type parameter `T` is specified
14+
|
15+
LL | let y: Mask<_, LANES> = Mask::<_, _>::splat(false);
16+
| ++++++++++++++++
1517

1618
error: aborting due to previous error
1719

0 commit comments

Comments
 (0)