Point at return type when it influences non-first match
arm
When encountering code like ```rust fn foo() -> i32 { match 0 { 1 => return 0, 2 => "", _ => 1, } } ``` Point at the return type and not at the prior arm, as that arm has type `!` which isn't influencing the arm corresponding to arm `2`. Fix #78124.
This commit is contained in:
parent
4cea2bc339
commit
55f8c66a60
12 changed files with 129 additions and 21 deletions
|
@ -743,6 +743,36 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
|||
ObligationCauseCode::Pattern { origin_expr: false, span: Some(span), .. } => {
|
||||
err.span_label(span, "expected due to this");
|
||||
}
|
||||
ObligationCauseCode::BlockTailExpression(
|
||||
_,
|
||||
scrut_hir_id,
|
||||
hir::MatchSource::TryDesugar,
|
||||
) => {
|
||||
if let Some(ty::error::ExpectedFound { expected, .. }) = exp_found {
|
||||
let scrut_expr = self.tcx.hir().expect_expr(scrut_hir_id);
|
||||
let scrut_ty = if let hir::ExprKind::Call(_, args) = &scrut_expr.kind {
|
||||
let arg_expr = args.first().expect("try desugaring call w/out arg");
|
||||
self.typeck_results.as_ref().and_then(|typeck_results| {
|
||||
typeck_results.expr_ty_opt(arg_expr)
|
||||
})
|
||||
} else {
|
||||
bug!("try desugaring w/out call expr as scrutinee");
|
||||
};
|
||||
|
||||
match scrut_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 `?`",
|
||||
"",
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
},
|
||||
ObligationCauseCode::MatchExpressionArm(box MatchExpressionArmCause {
|
||||
arm_block_id,
|
||||
arm_span,
|
||||
|
@ -1973,7 +2003,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
|||
trace: &TypeTrace<'tcx>,
|
||||
terr: TypeError<'tcx>,
|
||||
) -> Vec<TypeErrorAdditionalDiags> {
|
||||
use crate::traits::ObligationCauseCode::MatchExpressionArm;
|
||||
use crate::traits::ObligationCauseCode::{BlockTailExpression, MatchExpressionArm};
|
||||
let mut suggestions = Vec::new();
|
||||
let span = trace.cause.span();
|
||||
let values = self.resolve_vars_if_possible(trace.values);
|
||||
|
@ -1991,11 +2021,17 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
|||
// specify a byte literal
|
||||
(ty::Uint(ty::UintTy::U8), ty::Char) => {
|
||||
if let Ok(code) = self.tcx.sess().source_map().span_to_snippet(span)
|
||||
&& let Some(code) = code.strip_prefix('\'').and_then(|s| s.strip_suffix('\''))
|
||||
&& !code.starts_with("\\u") // forbid all Unicode escapes
|
||||
&& code.chars().next().is_some_and(|c| c.is_ascii()) // forbids literal Unicode characters beyond ASCII
|
||||
&& let Some(code) =
|
||||
code.strip_prefix('\'').and_then(|s| s.strip_suffix('\''))
|
||||
// forbid all Unicode escapes
|
||||
&& !code.starts_with("\\u")
|
||||
// forbids literal Unicode characters beyond ASCII
|
||||
&& code.chars().next().is_some_and(|c| c.is_ascii())
|
||||
{
|
||||
suggestions.push(TypeErrorAdditionalDiags::MeantByteLiteral { span, code: escape_literal(code) })
|
||||
suggestions.push(TypeErrorAdditionalDiags::MeantByteLiteral {
|
||||
span,
|
||||
code: escape_literal(code),
|
||||
})
|
||||
}
|
||||
}
|
||||
// If a character was expected and the found expression is a string literal
|
||||
|
@ -2006,7 +2042,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
|||
&& let Some(code) = code.strip_prefix('"').and_then(|s| s.strip_suffix('"'))
|
||||
&& code.chars().count() == 1
|
||||
{
|
||||
suggestions.push(TypeErrorAdditionalDiags::MeantCharLiteral { span, code: escape_literal(code) })
|
||||
suggestions.push(TypeErrorAdditionalDiags::MeantCharLiteral {
|
||||
span,
|
||||
code: escape_literal(code),
|
||||
})
|
||||
}
|
||||
}
|
||||
// If a string was expected and the found expression is a character literal,
|
||||
|
@ -2016,7 +2055,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
|||
if let Some(code) =
|
||||
code.strip_prefix('\'').and_then(|s| s.strip_suffix('\''))
|
||||
{
|
||||
suggestions.push(TypeErrorAdditionalDiags::MeantStrLiteral { span, code: escape_literal(code) })
|
||||
suggestions.push(TypeErrorAdditionalDiags::MeantStrLiteral {
|
||||
span,
|
||||
code: escape_literal(code),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2025,17 +2067,24 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
|||
(ty::Bool, ty::Tuple(list)) => if list.len() == 0 {
|
||||
suggestions.extend(self.suggest_let_for_letchains(&trace.cause, span));
|
||||
}
|
||||
(ty::Array(_, _), ty::Array(_, _)) => suggestions.extend(self.suggest_specify_actual_length(terr, trace, span)),
|
||||
(ty::Array(_, _), ty::Array(_, _)) => {
|
||||
suggestions.extend(self.suggest_specify_actual_length(terr, trace, span))
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let code = trace.cause.code();
|
||||
if let &MatchExpressionArm(box MatchExpressionArmCause { source, .. }) = code
|
||||
&& let hir::MatchSource::TryDesugar = source
|
||||
&& let Some((expected_ty, found_ty, _, _)) = self.values_str(trace.values)
|
||||
{
|
||||
suggestions.push(TypeErrorAdditionalDiags::TryCannotConvert { found: found_ty.content(), expected: expected_ty.content() });
|
||||
}
|
||||
if let &(MatchExpressionArm(box MatchExpressionArmCause { source, .. })
|
||||
| BlockTailExpression(.., source)
|
||||
) = code
|
||||
&& let hir::MatchSource::TryDesugar = source
|
||||
&& let Some((expected_ty, found_ty, _, _)) = self.values_str(trace.values)
|
||||
{
|
||||
suggestions.push(TypeErrorAdditionalDiags::TryCannotConvert {
|
||||
found: found_ty.content(),
|
||||
expected: expected_ty.content(),
|
||||
});
|
||||
}
|
||||
suggestions
|
||||
}
|
||||
|
||||
|
@ -2905,6 +2954,9 @@ impl<'tcx> ObligationCauseExt<'tcx> for ObligationCause<'tcx> {
|
|||
CompareImplItemObligation { kind: ty::AssocKind::Const, .. } => {
|
||||
ObligationCauseFailureCode::ConstCompat { span, subdiags }
|
||||
}
|
||||
BlockTailExpression(.., hir::MatchSource::TryDesugar) => {
|
||||
ObligationCauseFailureCode::TryCompat { span, subdiags }
|
||||
}
|
||||
MatchExpressionArm(box MatchExpressionArmCause { source, .. }) => match source {
|
||||
hir::MatchSource::TryDesugar => {
|
||||
ObligationCauseFailureCode::TryCompat { span, subdiags }
|
||||
|
|
|
@ -146,7 +146,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
|
|||
|
||||
if let SubregionOrigin::Subtype(box TypeTrace { cause, .. }) = sub_origin {
|
||||
if let ObligationCauseCode::ReturnValue(hir_id)
|
||||
| ObligationCauseCode::BlockTailExpression(hir_id) = cause.code()
|
||||
| ObligationCauseCode::BlockTailExpression(hir_id, ..) = cause.code()
|
||||
{
|
||||
let parent_id = tcx.hir().get_parent_item(*hir_id);
|
||||
if let Some(fn_decl) = tcx.hir().fn_decl_by_hir_id(parent_id.into()) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue