Skip to content

Commit 802a4ed

Browse files
committed
Auto merge of rust-lang#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 rust-lang#88284.
2 parents 7b4d9e1 + b3eec8b commit 802a4ed

File tree

82 files changed

+3903
-3735
lines changed

Some content is hidden

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

82 files changed

+3903
-3735
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
@@ -742,7 +742,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
742742

743743
let errors = ocx.select_all_or_error();
744744
if !errors.is_empty() {
745-
infcx.err_ctxt().report_fulfillment_errors(&errors);
745+
infcx.err_ctxt().report_fulfillment_errors(errors);
746746
}
747747

748748
// 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
@@ -322,7 +322,7 @@ fn compare_method_predicate_entailment<'tcx>(
322322
// FIXME(-Ztrait-solver=next): Not needed when the hack below is removed.
323323
let errors = ocx.select_where_possible();
324324
if !errors.is_empty() {
325-
let reported = infcx.err_ctxt().report_fulfillment_errors(&errors);
325+
let reported = infcx.err_ctxt().report_fulfillment_errors(errors);
326326
return Err(reported);
327327
}
328328

@@ -393,7 +393,7 @@ fn compare_method_predicate_entailment<'tcx>(
393393
});
394394
}
395395
CheckImpliedWfMode::Skip => {
396-
let reported = infcx.err_ctxt().report_fulfillment_errors(&errors);
396+
let reported = infcx.err_ctxt().report_fulfillment_errors(errors);
397397
return Err(reported);
398398
}
399399
}
@@ -873,7 +873,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
873873
// RPITs.
874874
let errors = ocx.select_all_or_error();
875875
if !errors.is_empty() {
876-
let reported = infcx.err_ctxt().report_fulfillment_errors(&errors);
876+
let reported = infcx.err_ctxt().report_fulfillment_errors(errors);
877877
return Err(reported);
878878
}
879879

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

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

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

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
@@ -577,7 +577,7 @@ pub fn check_function_signature<'tcx>(
577577
Ok(()) => {
578578
let errors = ocx.select_all_or_error();
579579
if !errors.is_empty() {
580-
infcx.err_ctxt().report_fulfillment_errors(&errors);
580+
infcx.err_ctxt().report_fulfillment_errors(errors);
581581
return;
582582
}
583583
}

compiler/rustc_hir_analysis/src/check/wfcheck.rs

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

114114
let errors = wfcx.select_all_or_error();
115115
if !errors.is_empty() {
116-
infcx.err_ctxt().report_fulfillment_errors(&errors);
116+
infcx.err_ctxt().report_fulfillment_errors(errors);
117117
return;
118118
}
119119

compiler/rustc_hir_analysis/src/coherence/builtin.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ fn visit_implementation_of_dispatch_from_dyn(tcx: TyCtxt<'_>, impl_did: LocalDef
279279
}
280280
let errors = ocx.select_all_or_error();
281281
if !errors.is_empty() {
282-
infcx.err_ctxt().report_fulfillment_errors(&errors);
282+
infcx.err_ctxt().report_fulfillment_errors(errors);
283283
}
284284

285285
// Finally, resolve all regions.
@@ -519,7 +519,7 @@ pub fn coerce_unsized_info<'tcx>(tcx: TyCtxt<'tcx>, impl_did: LocalDefId) -> Coe
519519
ocx.register_obligation(obligation);
520520
let errors = ocx.select_all_or_error();
521521
if !errors.is_empty() {
522-
infcx.err_ctxt().report_fulfillment_errors(&errors);
522+
infcx.err_ctxt().report_fulfillment_errors(errors);
523523
}
524524

525525
// 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
@@ -3010,7 +3010,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
30103010
// There should be at least one error reported. If not, we
30113011
// will still delay a span bug in `report_fulfillment_errors`.
30123012
Ok::<_, NoSolution>((
3013-
self.err_ctxt().report_fulfillment_errors(&errors),
3013+
self.err_ctxt().report_fulfillment_errors(errors),
30143014
impl_trait_ref.args.type_at(1),
30153015
element_ty,
30163016
))

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
@@ -203,7 +203,7 @@ impl<'a, 'tcx> ObligationCtxt<'a, 'tcx> {
203203
def_id: LocalDefId,
204204
) -> Result<FxIndexSet<Ty<'tcx>>, ErrorGuaranteed> {
205205
self.assumed_wf_types(param_env, def_id)
206-
.map_err(|errors| self.infcx.err_ctxt().report_fulfillment_errors(&errors))
206+
.map_err(|errors| self.infcx.err_ctxt().report_fulfillment_errors(errors))
207207
}
208208

209209
pub fn assumed_wf_types(

0 commit comments

Comments
 (0)