Rollup merge of #59859 - davidtwco:issue-59756, r=cramertj
Suggest removing `?` to resolve type errors. Fixes #59756.
This commit is contained in:
commit
2bc127dcc7
8 changed files with 84 additions and 2 deletions
|
@ -604,13 +604,39 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
|
||||||
source,
|
source,
|
||||||
ref prior_arms,
|
ref prior_arms,
|
||||||
last_ty,
|
last_ty,
|
||||||
|
discrim_hir_id,
|
||||||
..
|
..
|
||||||
} => match source {
|
} => match source {
|
||||||
hir::MatchSource::IfLetDesugar { .. } => {
|
hir::MatchSource::IfLetDesugar { .. } => {
|
||||||
let msg = "`if let` arms have incompatible types";
|
let msg = "`if let` arms have incompatible types";
|
||||||
err.span_label(cause.span, msg);
|
err.span_label(cause.span, msg);
|
||||||
}
|
}
|
||||||
hir::MatchSource::TryDesugar => {}
|
hir::MatchSource::TryDesugar => {
|
||||||
|
if let Some(ty::error::ExpectedFound { expected, .. }) = exp_found {
|
||||||
|
let discrim_expr = self.tcx.hir().expect_expr_by_hir_id(discrim_hir_id);
|
||||||
|
let discrim_ty = if let hir::ExprKind::Call(_, args) = &discrim_expr.node {
|
||||||
|
let arg_expr = args.first().expect("try desugaring call w/out arg");
|
||||||
|
self.in_progress_tables.and_then(|tables| {
|
||||||
|
tables.borrow().expr_ty_opt(arg_expr)
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
bug!("try desugaring w/out call expr as discriminant");
|
||||||
|
};
|
||||||
|
|
||||||
|
match discrim_ty {
|
||||||
|
Some(ty) if expected == ty => {
|
||||||
|
let source_map = self.tcx.sess.source_map();
|
||||||
|
err.span_suggestion(
|
||||||
|
source_map.end_point(cause.span),
|
||||||
|
"try removing this `?`",
|
||||||
|
"".to_string(),
|
||||||
|
Applicability::MachineApplicable,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
_ => {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
_ => {
|
_ => {
|
||||||
let msg = "`match` arms have incompatible types";
|
let msg = "`match` arms have incompatible types";
|
||||||
err.span_label(cause.span, msg);
|
err.span_label(cause.span, msg);
|
||||||
|
|
|
@ -226,6 +226,7 @@ pub enum ObligationCauseCode<'tcx> {
|
||||||
source: hir::MatchSource,
|
source: hir::MatchSource,
|
||||||
prior_arms: Vec<Span>,
|
prior_arms: Vec<Span>,
|
||||||
last_ty: Ty<'tcx>,
|
last_ty: Ty<'tcx>,
|
||||||
|
discrim_hir_id: hir::HirId,
|
||||||
},
|
},
|
||||||
|
|
||||||
/// Computing common supertype in the pattern guard for the arms of a match expression
|
/// Computing common supertype in the pattern guard for the arms of a match expression
|
||||||
|
|
|
@ -519,6 +519,7 @@ impl<'a, 'tcx> Lift<'tcx> for traits::ObligationCauseCode<'a> {
|
||||||
source,
|
source,
|
||||||
ref prior_arms,
|
ref prior_arms,
|
||||||
last_ty,
|
last_ty,
|
||||||
|
discrim_hir_id,
|
||||||
} => {
|
} => {
|
||||||
tcx.lift(&last_ty).map(|last_ty| {
|
tcx.lift(&last_ty).map(|last_ty| {
|
||||||
super::MatchExpressionArm {
|
super::MatchExpressionArm {
|
||||||
|
@ -526,6 +527,7 @@ impl<'a, 'tcx> Lift<'tcx> for traits::ObligationCauseCode<'a> {
|
||||||
source,
|
source,
|
||||||
prior_arms: prior_arms.clone(),
|
prior_arms: prior_arms.clone(),
|
||||||
last_ty,
|
last_ty,
|
||||||
|
discrim_hir_id,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -732,6 +732,7 @@ https://doc.rust-lang.org/reference/types.html#trait-objects");
|
||||||
source: match_src,
|
source: match_src,
|
||||||
prior_arms: other_arms.clone(),
|
prior_arms: other_arms.clone(),
|
||||||
last_ty: prior_arm_ty.unwrap(),
|
last_ty: prior_arm_ty.unwrap(),
|
||||||
|
discrim_hir_id: discrim.hir_id,
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
coercion.coerce(self, &cause, &arm.body, arm_ty);
|
coercion.coerce(self, &cause, &arm.body, arm_ty);
|
||||||
|
|
17
src/test/ui/issue-59756.fixed
Normal file
17
src/test/ui/issue-59756.fixed
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
// run-rustfix
|
||||||
|
|
||||||
|
#![allow(warnings)]
|
||||||
|
|
||||||
|
struct A;
|
||||||
|
struct B;
|
||||||
|
|
||||||
|
fn foo() -> Result<A, B> {
|
||||||
|
Ok(A)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bar() -> Result<A, B> {
|
||||||
|
foo()
|
||||||
|
//~^ ERROR try expression alternatives have incompatible types [E0308]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {}
|
17
src/test/ui/issue-59756.rs
Normal file
17
src/test/ui/issue-59756.rs
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
// run-rustfix
|
||||||
|
|
||||||
|
#![allow(warnings)]
|
||||||
|
|
||||||
|
struct A;
|
||||||
|
struct B;
|
||||||
|
|
||||||
|
fn foo() -> Result<A, B> {
|
||||||
|
Ok(A)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bar() -> Result<A, B> {
|
||||||
|
foo()?
|
||||||
|
//~^ ERROR try expression alternatives have incompatible types [E0308]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {}
|
15
src/test/ui/issue-59756.stderr
Normal file
15
src/test/ui/issue-59756.stderr
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
error[E0308]: try expression alternatives have incompatible types
|
||||||
|
--> $DIR/issue-59756.rs:13:5
|
||||||
|
|
|
||||||
|
LL | foo()?
|
||||||
|
| ^^^^^-
|
||||||
|
| | |
|
||||||
|
| | help: try removing this `?`
|
||||||
|
| expected enum `std::result::Result`, found struct `A`
|
||||||
|
|
|
||||||
|
= note: expected type `std::result::Result<A, B>`
|
||||||
|
found type `A`
|
||||||
|
|
||||||
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0308`.
|
|
@ -2,7 +2,10 @@ error[E0308]: try expression alternatives have incompatible types
|
||||||
--> $DIR/issue-51632-try-desugar-incompatible-types.rs:8:5
|
--> $DIR/issue-51632-try-desugar-incompatible-types.rs:8:5
|
||||||
|
|
|
|
||||||
LL | missing_discourses()?
|
LL | missing_discourses()?
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^ expected enum `std::result::Result`, found isize
|
| ^^^^^^^^^^^^^^^^^^^^-
|
||||||
|
| | |
|
||||||
|
| | help: try removing this `?`
|
||||||
|
| expected enum `std::result::Result`, found isize
|
||||||
|
|
|
|
||||||
= note: expected type `std::result::Result<isize, ()>`
|
= note: expected type `std::result::Result<isize, ()>`
|
||||||
found type `isize`
|
found type `isize`
|
||||||
|
|
Loading…
Reference in a new issue