Skip to content

Commit 94bc9c7

Browse files
committed
Auto merge of #114811 - estebank:impl-ambiguity, r=wesleywiser
Show more information when multiple `impl`s apply - When there are `impl`s without type params, show only those (to avoid showing overly generic `impl`s). ``` error[E0283]: type annotations needed --> $DIR/multiple-impl-apply.rs:34:9 | LL | let y = x.into(); | ^ ---- type must be known at this point | note: multiple `impl`s satisfying `_: From<Baz>` found --> $DIR/multiple-impl-apply.rs:14:1 | LL | impl From<Baz> for Bar { | ^^^^^^^^^^^^^^^^^^^^^^ ... LL | impl From<Baz> for Foo { | ^^^^^^^^^^^^^^^^^^^^^^ = note: required for `Baz` to implement `Into<_>` help: consider giving `y` an explicit type | LL | let y: /* Type */ = x.into(); | ++++++++++++ ``` - Lower the importance of `T: Sized`, `T: WellFormed` and coercion errors, to prioritize more relevant errors. The pre-existing deduplication logic deals with hiding redundant errors better that way, and we show errors with more metadata that is useful to the user. - Show `<SelfTy as Trait>::assoc_fn` suggestion in more cases. ``` error[E0790]: cannot call associated function on trait without specifying the corresponding `impl` type --> $DIR/cross-return-site-inference.rs:38:16 | LL | return Err(From::from("foo")); | ^^^^^^^^^^ cannot call associated function of trait | help: use a fully-qualified path to a specific available implementation | LL | return Err(</* self type */ as From>::from("foo")); | +++++++++++++++++++ + ``` Fix #88284.
2 parents 31be8cc + 4483ac2 commit 94bc9c7

File tree

85 files changed

+3993
-3825
lines changed

Some content is hidden

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

85 files changed

+3993
-3825
lines changed

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ fn check_opaque_type_well_formed<'tcx>(
368368
if errors.is_empty() {
369369
Ok(definition_ty)
370370
} else {
371-
Err(infcx.err_ctxt().report_fulfillment_errors(&errors))
371+
Err(infcx.err_ctxt().report_fulfillment_errors(errors))
372372
}
373373
}
374374

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
743743

744744
let errors = ocx.select_all_or_error();
745745
if !errors.is_empty() {
746-
infcx.err_ctxt().report_fulfillment_errors(&errors);
746+
infcx.err_ctxt().report_fulfillment_errors(errors);
747747
}
748748

749749
// Attempting to call a trait method?

compiler/rustc_error_codes/src/error_codes/E0282.md

+14-11
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,32 @@ The compiler could not infer a type and asked for a type annotation.
33
Erroneous code example:
44

55
```compile_fail,E0282
6-
let x = "hello".chars().rev().collect();
6+
let x = Vec::new();
77
```
88

99
This error indicates that type inference did not result in one unique possible
1010
type, and extra information is required. In most cases this can be provided
1111
by adding a type annotation. Sometimes you need to specify a generic type
1212
parameter manually.
1313

14-
A common example is the `collect` method on `Iterator`. It has a generic type
15-
parameter with a `FromIterator` bound, which for a `char` iterator is
16-
implemented by `Vec` and `String` among others. Consider the following snippet
17-
that reverses the characters of a string:
14+
In the example above, type `Vec` has a type parameter `T`. When calling
15+
`Vec::new`, barring any other later usage of the variable `x` that allows the
16+
compiler to infer what type `T` is, the compiler needs to be told what it is.
1817

19-
In the first code example, the compiler cannot infer what the type of `x` should
20-
be: `Vec<char>` and `String` are both suitable candidates. To specify which type
21-
to use, you can use a type annotation on `x`:
18+
The type can be specified on the variable:
2219

2320
```
24-
let x: Vec<char> = "hello".chars().rev().collect();
21+
let x: Vec<i32> = Vec::new();
2522
```
2623

27-
It is not necessary to annotate the full type. Once the ambiguity is resolved,
28-
the compiler can infer the rest:
24+
The type can also be specified in the path of the expression:
25+
26+
```
27+
let x = Vec::<i32>::new();
28+
```
29+
30+
In cases with more complex types, it is not necessary to annotate the full
31+
type. Once the ambiguity is resolved, the compiler can infer the rest:
2932

3033
```
3134
let x: Vec<_> = "hello".chars().rev().collect();

compiler/rustc_error_codes/src/error_codes/E0283.md

+45-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,51 @@
1-
An implementation cannot be chosen unambiguously because of lack of information.
1+
The compiler could not infer a type and asked for a type annotation.
22

33
Erroneous code example:
44

5+
```compile_fail,E0283
6+
let x = "hello".chars().rev().collect();
7+
```
8+
9+
This error indicates that type inference did not result in one unique possible
10+
type, and extra information is required. In most cases this can be provided
11+
by adding a type annotation. Sometimes you need to specify a generic type
12+
parameter manually.
13+
14+
A common example is the `collect` method on `Iterator`. It has a generic type
15+
parameter with a `FromIterator` bound, which for a `char` iterator is
16+
implemented by `Vec` and `String` among others. Consider the following snippet
17+
that reverses the characters of a string:
18+
19+
In the first code example, the compiler cannot infer what the type of `x` should
20+
be: `Vec<char>` and `String` are both suitable candidates. To specify which type
21+
to use, you can use a type annotation on `x`:
22+
23+
```
24+
let x: Vec<char> = "hello".chars().rev().collect();
25+
```
26+
27+
It is not necessary to annotate the full type. Once the ambiguity is resolved,
28+
the compiler can infer the rest:
29+
30+
```
31+
let x: Vec<_> = "hello".chars().rev().collect();
32+
```
33+
34+
Another way to provide the compiler with enough information, is to specify the
35+
generic type parameter:
36+
37+
```
38+
let x = "hello".chars().rev().collect::<Vec<char>>();
39+
```
40+
41+
Again, you need not specify the full type if the compiler can infer it:
42+
43+
```
44+
let x = "hello".chars().rev().collect::<Vec<_>>();
45+
```
46+
47+
We can see a self-contained example below:
48+
549
```compile_fail,E0283
650
struct Foo;
751

compiler/rustc_hir_analysis/src/check/check.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ fn check_opaque_meets_bounds<'tcx>(
327327
// version.
328328
let errors = ocx.select_all_or_error();
329329
if !errors.is_empty() {
330-
let guar = infcx.err_ctxt().report_fulfillment_errors(&errors);
330+
let guar = infcx.err_ctxt().report_fulfillment_errors(errors);
331331
return Err(guar);
332332
}
333333
match origin {
@@ -1512,6 +1512,6 @@ pub(super) fn check_generator_obligations(tcx: TyCtxt<'_>, def_id: LocalDefId) {
15121512
let errors = fulfillment_cx.select_all_or_error(&infcx);
15131513
debug!(?errors);
15141514
if !errors.is_empty() {
1515-
infcx.err_ctxt().report_fulfillment_errors(&errors);
1515+
infcx.err_ctxt().report_fulfillment_errors(errors);
15161516
}
15171517
}

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ fn compare_method_predicate_entailment<'tcx>(
323323
// FIXME(-Ztrait-solver=next): Not needed when the hack below is removed.
324324
let errors = ocx.select_where_possible();
325325
if !errors.is_empty() {
326-
let reported = infcx.err_ctxt().report_fulfillment_errors(&errors);
326+
let reported = infcx.err_ctxt().report_fulfillment_errors(errors);
327327
return Err(reported);
328328
}
329329

@@ -394,7 +394,7 @@ fn compare_method_predicate_entailment<'tcx>(
394394
});
395395
}
396396
CheckImpliedWfMode::Skip => {
397-
let reported = infcx.err_ctxt().report_fulfillment_errors(&errors);
397+
let reported = infcx.err_ctxt().report_fulfillment_errors(errors);
398398
return Err(reported);
399399
}
400400
}
@@ -874,7 +874,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
874874
// RPITs.
875875
let errors = ocx.select_all_or_error();
876876
if !errors.is_empty() {
877-
let reported = infcx.err_ctxt().report_fulfillment_errors(&errors);
877+
let reported = infcx.err_ctxt().report_fulfillment_errors(errors);
878878
return Err(reported);
879879
}
880880

@@ -2050,7 +2050,7 @@ fn compare_const_predicate_entailment<'tcx>(
20502050
// version.
20512051
let errors = ocx.select_all_or_error();
20522052
if !errors.is_empty() {
2053-
return Err(infcx.err_ctxt().report_fulfillment_errors(&errors));
2053+
return Err(infcx.err_ctxt().report_fulfillment_errors(errors));
20542054
}
20552055

20562056
let outlives_env = OutlivesEnvironment::new(param_env);
@@ -2143,7 +2143,7 @@ fn compare_type_predicate_entailment<'tcx>(
21432143
// version.
21442144
let errors = ocx.select_all_or_error();
21452145
if !errors.is_empty() {
2146-
let reported = infcx.err_ctxt().report_fulfillment_errors(&errors);
2146+
let reported = infcx.err_ctxt().report_fulfillment_errors(errors);
21472147
return Err(reported);
21482148
}
21492149

@@ -2358,7 +2358,7 @@ pub(super) fn check_type_bounds<'tcx>(
23582358
// version.
23592359
let errors = ocx.select_all_or_error();
23602360
if !errors.is_empty() {
2361-
let reported = infcx.err_ctxt().report_fulfillment_errors(&errors);
2361+
let reported = infcx.err_ctxt().report_fulfillment_errors(errors);
23622362
return Err(reported);
23632363
}
23642364

compiler/rustc_hir_analysis/src/check/entry.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
158158
ocx.register_bound(cause, param_env, norm_return_ty, term_did);
159159
let errors = ocx.select_all_or_error();
160160
if !errors.is_empty() {
161-
infcx.err_ctxt().report_fulfillment_errors(&errors);
161+
infcx.err_ctxt().report_fulfillment_errors(errors);
162162
error = true;
163163
}
164164
// now we can take the return type of the given main function

compiler/rustc_hir_analysis/src/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ pub fn check_function_signature<'tcx>(
588588
Ok(()) => {
589589
let errors = ocx.select_all_or_error();
590590
if !errors.is_empty() {
591-
infcx.err_ctxt().report_fulfillment_errors(&errors);
591+
infcx.err_ctxt().report_fulfillment_errors(errors);
592592
return;
593593
}
594594
}

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub(super) fn enter_wf_checking_ctxt<'tcx, F>(
116116

117117
let errors = wfcx.select_all_or_error();
118118
if !errors.is_empty() {
119-
infcx.err_ctxt().report_fulfillment_errors(&errors);
119+
infcx.err_ctxt().report_fulfillment_errors(errors);
120120
return;
121121
}
122122

compiler/rustc_hir_analysis/src/coherence/builtin.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ fn visit_implementation_of_dispatch_from_dyn(tcx: TyCtxt<'_>, impl_did: LocalDef
261261
}
262262
let errors = ocx.select_all_or_error();
263263
if !errors.is_empty() {
264-
infcx.err_ctxt().report_fulfillment_errors(&errors);
264+
infcx.err_ctxt().report_fulfillment_errors(errors);
265265
}
266266

267267
// Finally, resolve all regions.
@@ -470,7 +470,7 @@ pub fn coerce_unsized_info<'tcx>(tcx: TyCtxt<'tcx>, impl_did: LocalDefId) -> Coe
470470
ocx.register_obligation(obligation);
471471
let errors = ocx.select_all_or_error();
472472
if !errors.is_empty() {
473-
infcx.err_ctxt().report_fulfillment_errors(&errors);
473+
infcx.err_ctxt().report_fulfillment_errors(errors);
474474
}
475475

476476
// Finally, resolve all regions.

compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ fn get_impl_args(
196196

197197
let errors = ocx.select_all_or_error();
198198
if !errors.is_empty() {
199-
let guar = ocx.infcx.err_ctxt().report_fulfillment_errors(&errors);
199+
let guar = ocx.infcx.err_ctxt().report_fulfillment_errors(errors);
200200
return Err(guar);
201201
}
202202

compiler/rustc_hir_typeck/src/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2958,7 +2958,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
29582958
// There should be at least one error reported. If not, we
29592959
// will still delay a span bug in `report_fulfillment_errors`.
29602960
Ok::<_, NoSolution>((
2961-
self.err_ctxt().report_fulfillment_errors(&errors),
2961+
self.err_ctxt().report_fulfillment_errors(errors),
29622962
impl_trait_ref.args.type_at(1),
29632963
element_ty,
29642964
))

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
564564

565565
if !errors.is_empty() {
566566
self.adjust_fulfillment_errors_for_expr_obligation(&mut errors);
567-
self.err_ctxt().report_fulfillment_errors(&errors);
567+
self.err_ctxt().report_fulfillment_errors(errors);
568568
}
569569
}
570570

@@ -577,7 +577,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
577577
if !result.is_empty() {
578578
mutate_fulfillment_errors(&mut result);
579579
self.adjust_fulfillment_errors_for_expr_obligation(&mut result);
580-
self.err_ctxt().report_fulfillment_errors(&result);
580+
self.err_ctxt().report_fulfillment_errors(result);
581581
}
582582
}
583583

@@ -1477,7 +1477,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14771477
{
14781478
Ok(normalized_ty) => normalized_ty,
14791479
Err(errors) => {
1480-
let guar = self.err_ctxt().report_fulfillment_errors(&errors);
1480+
let guar = self.err_ctxt().report_fulfillment_errors(errors);
14811481
return Ty::new_error(self.tcx,guar);
14821482
}
14831483
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use std::iter;
2727

2828
pub enum TypeAnnotationNeeded {
2929
/// ```compile_fail,E0282
30-
/// let x = "hello".chars().rev().collect();
30+
/// let x;
3131
/// ```
3232
E0282,
3333
/// An implementation cannot be chosen unambiguously because of lack of information.

compiler/rustc_passes/src/check_attr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2373,7 +2373,7 @@ impl CheckAttrVisitor<'_> {
23732373

23742374
let errors = ocx.select_all_or_error();
23752375
if !errors.is_empty() {
2376-
infcx.err_ctxt().report_fulfillment_errors(&errors);
2376+
infcx.err_ctxt().report_fulfillment_errors(errors);
23772377
self.abort.set(true);
23782378
}
23792379
}

compiler/rustc_passes/src/layout_test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub fn ensure_wf<'tcx>(
5757
ocx.register_obligation(obligation);
5858
let errors = ocx.select_all_or_error();
5959
if !errors.is_empty() {
60-
infcx.err_ctxt().report_fulfillment_errors(&errors);
60+
infcx.err_ctxt().report_fulfillment_errors(errors);
6161
false
6262
} else {
6363
// looks WF!

compiler/rustc_trait_selection/src/traits/engine.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ impl<'a, 'tcx> ObligationCtxt<'a, 'tcx> {
218218
def_id: LocalDefId,
219219
) -> Result<FxIndexSet<Ty<'tcx>>, ErrorGuaranteed> {
220220
self.assumed_wf_types(param_env, def_id)
221-
.map_err(|errors| self.infcx.err_ctxt().report_fulfillment_errors(&errors))
221+
.map_err(|errors| self.infcx.err_ctxt().report_fulfillment_errors(errors))
222222
}
223223

224224
pub fn assumed_wf_types(

0 commit comments

Comments
 (0)