Skip to content

Commit e606861

Browse files
authored
Rollup merge of rust-lang#100261 - luqmana:suggestions-overflow, r=lcnr
Set tainted errors bit before emitting coerce suggestions. Fixes rust-lang#100246. rust-lang#89576 basically got 99% of the way there but the match typechecking code (which calls `coerce_inner`) also needed a similar fix.
2 parents be071ec + 75cc9cd commit e606861

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

compiler/rustc_typeck/src/check/coercion.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1479,6 +1479,10 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
14791479
}
14801480
}
14811481
Err(coercion_error) => {
1482+
// Mark that we've failed to coerce the types here to suppress
1483+
// any superfluous errors we might encounter while trying to
1484+
// emit or provide suggestions on how to fix the initial error.
1485+
fcx.set_tainted_by_errors();
14821486
let (expected, found) = if label_expression_as_expected {
14831487
// In the case where this is a "forced unit", like
14841488
// `break`, we want to call the `()` "expected"

src/test/ui/typeck/issue-100246.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#![recursion_limit = "5"] // To reduce noise
2+
3+
//expect incompatible type error when ambiguous traits are in scope
4+
//and not an overflow error on the span in the main function.
5+
6+
struct Ratio<T>(T);
7+
8+
pub trait Pow {
9+
fn pow(self) -> Self;
10+
}
11+
12+
impl<'a, T> Pow for &'a Ratio<T>
13+
where
14+
&'a T: Pow,
15+
{
16+
fn pow(self) -> Self {
17+
self
18+
}
19+
}
20+
21+
fn downcast<'a, W: ?Sized>() -> std::io::Result<&'a W> {
22+
todo!()
23+
}
24+
25+
struct Other;
26+
27+
fn main() -> std::io::Result<()> {
28+
let other: Other = downcast()?;//~ERROR 28:24: 28:35: `?` operator has incompatible types
29+
Ok(())
30+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0308]: `?` operator has incompatible types
2+
--> $DIR/issue-100246.rs:28:24
3+
|
4+
LL | let other: Other = downcast()?;
5+
| ^^^^^^^^^^^ expected struct `Other`, found reference
6+
|
7+
= note: `?` operator cannot convert from `&_` to `Other`
8+
= note: expected struct `Other`
9+
found reference `&_`
10+
11+
error: aborting due to previous error
12+
13+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)