Skip to content

Commit d4e0ddf

Browse files
authored
Rollup merge of #95751 - compiler-errors:ambig-int, r=jackh726
Don't report numeric inference ambiguity when we have previous errors Fixes #95648
2 parents 525438b + 39bff4b commit d4e0ddf

File tree

5 files changed

+48
-35
lines changed

5 files changed

+48
-35
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+19-3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use rustc_span::symbol::{kw, sym};
3636
use rustc_span::{ExpnKind, Span, DUMMY_SP};
3737
use std::fmt;
3838
use std::iter;
39+
use std::ops::ControlFlow;
3940

4041
use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
4142
use crate::traits::query::normalize::AtExt as _;
@@ -2226,9 +2227,10 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
22262227
post.dedup();
22272228

22282229
if self.is_tainted_by_errors()
2229-
&& crate_names.len() == 1
2230-
&& ["`core`", "`alloc`", "`std`"].contains(&crate_names[0].as_str())
2231-
&& spans.len() == 0
2230+
&& (crate_names.len() == 1
2231+
&& spans.len() == 0
2232+
&& ["`core`", "`alloc`", "`std`"].contains(&crate_names[0].as_str())
2233+
|| predicate.visit_with(&mut HasNumericInferVisitor).is_break())
22322234
{
22332235
// Avoid complaining about other inference issues for expressions like
22342236
// `42 >> 1`, where the types are still `{integer}`, but we want to
@@ -2666,3 +2668,17 @@ impl ArgKind {
26662668
}
26672669
}
26682670
}
2671+
2672+
struct HasNumericInferVisitor;
2673+
2674+
impl<'tcx> ty::TypeVisitor<'tcx> for HasNumericInferVisitor {
2675+
type BreakTy = ();
2676+
2677+
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
2678+
if matches!(ty.kind(), ty::Infer(ty::FloatVar(_) | ty::IntVar(_))) {
2679+
ControlFlow::Break(())
2680+
} else {
2681+
ControlFlow::CONTINUE
2682+
}
2683+
}
2684+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
trait Fallback {
2+
fn foo(&self) {}
3+
}
4+
5+
impl Fallback for i32 {}
6+
7+
impl Fallback for u64 {}
8+
9+
impl Fallback for usize {}
10+
11+
fn main() {
12+
missing();
13+
//~^ ERROR cannot find function `missing` in this scope
14+
0.foo();
15+
// But then we shouldn't report an inference ambiguity here...
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0425]: cannot find function `missing` in this scope
2+
--> $DIR/no-fallback-multiple-impls.rs:12:5
3+
|
4+
LL | missing();
5+
| ^^^^^^^ not found in this scope
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0425`.

src/test/ui/traits/test-2.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ impl bar for i32 { fn dup(&self) -> i32 { *self } fn blah<X>(&self) {} }
66
impl bar for u32 { fn dup(&self) -> u32 { *self } fn blah<X>(&self) {} }
77

88
fn main() {
9-
10.dup::<i32>(); //~ ERROR type annotations needed
9+
10.dup::<i32>();
1010
//~^ ERROR this associated function takes 0 generic arguments but 1
11-
10.blah::<i32, i32>(); //~ ERROR type annotations needed
11+
10.blah::<i32, i32>();
1212
//~^ ERROR this associated function takes 1 generic argument but 2
1313
(Box::new(10) as Box<dyn bar>).dup();
1414
//~^ ERROR E0038

src/test/ui/traits/test-2.stderr

+2-30
Original file line numberDiff line numberDiff line change
@@ -79,35 +79,7 @@ LL | trait bar { fn dup(&self) -> Self; fn blah<X>(&self); }
7979
= note: required because of the requirements on the impl of `CoerceUnsized<Box<dyn bar>>` for `Box<{integer}>`
8080
= note: required by cast to type `Box<dyn bar>`
8181

82-
error[E0283]: type annotations needed
83-
--> $DIR/test-2.rs:9:8
84-
|
85-
LL | 10.dup::<i32>();
86-
| ^^^ cannot infer type for type `{integer}`
87-
|
88-
note: multiple `impl`s satisfying `{integer}: bar` found
89-
--> $DIR/test-2.rs:5:1
90-
|
91-
LL | impl bar for i32 { fn dup(&self) -> i32 { *self } fn blah<X>(&self) {} }
92-
| ^^^^^^^^^^^^^^^^
93-
LL | impl bar for u32 { fn dup(&self) -> u32 { *self } fn blah<X>(&self) {} }
94-
| ^^^^^^^^^^^^^^^^
95-
96-
error[E0283]: type annotations needed
97-
--> $DIR/test-2.rs:11:8
98-
|
99-
LL | 10.blah::<i32, i32>();
100-
| ^^^^ cannot infer type for type `{integer}`
101-
|
102-
note: multiple `impl`s satisfying `{integer}: bar` found
103-
--> $DIR/test-2.rs:5:1
104-
|
105-
LL | impl bar for i32 { fn dup(&self) -> i32 { *self } fn blah<X>(&self) {} }
106-
| ^^^^^^^^^^^^^^^^
107-
LL | impl bar for u32 { fn dup(&self) -> u32 { *self } fn blah<X>(&self) {} }
108-
| ^^^^^^^^^^^^^^^^
109-
110-
error: aborting due to 7 previous errors
82+
error: aborting due to 5 previous errors
11183

112-
Some errors have detailed explanations: E0038, E0107, E0283.
84+
Some errors have detailed explanations: E0038, E0107.
11385
For more information about an error, try `rustc --explain E0038`.

0 commit comments

Comments
 (0)