Skip to content

Commit 0bdb00d

Browse files
committed
Auto merge of rust-lang#114864 - matthiaskrgr:rollup-uw47qco, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - rust-lang#114588 (Improve docs for impl Default for ExitStatus) - rust-lang#114619 (Fix pthread_attr_union layout on Wasi) - rust-lang#114644 (Point out expectation even if we have `TypeError::RegionsInsufficientlyPolymorphic`) - rust-lang#114668 (Deny `FnDef` in patterns) - rust-lang#114819 (Point at return type when it influences non-first `match` arm) - rust-lang#114826 (Fix typos) - rust-lang#114837 (add missing feature(error_in_core)) - rust-lang#114853 (Migrate GUI colors test to original CSS color format) r? `@ghost` `@rustbot` modify labels: rollup
2 parents c57393e + 79bc72a commit 0bdb00d

File tree

58 files changed

+262
-116
lines changed

Some content is hidden

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

58 files changed

+262
-116
lines changed

Diff for: compiler/rustc_ast_lowering/src/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1648,7 +1648,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
16481648
hir::ExprKind::Match(
16491649
scrutinee,
16501650
arena_vec![self; break_arm, continue_arm],
1651-
hir::MatchSource::TryDesugar,
1651+
hir::MatchSource::TryDesugar(scrutinee.hir_id),
16521652
)
16531653
}
16541654

Diff for: compiler/rustc_hir/src/hir.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2148,7 +2148,7 @@ pub enum MatchSource {
21482148
/// A desugared `for _ in _ { .. }` loop.
21492149
ForLoopDesugar,
21502150
/// A desugared `?` operator.
2151-
TryDesugar,
2151+
TryDesugar(HirId),
21522152
/// A desugared `<expr>.await`.
21532153
AwaitDesugar,
21542154
/// A desugared `format_args!()`.
@@ -2162,7 +2162,7 @@ impl MatchSource {
21622162
match self {
21632163
Normal => "match",
21642164
ForLoopDesugar => "for",
2165-
TryDesugar => "?",
2165+
TryDesugar(_) => "?",
21662166
AwaitDesugar => ".await",
21672167
FormatArgs => "format_args!()",
21682168
}

Diff for: compiler/rustc_hir_typeck/src/_match.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
107107
let (span, code) = match prior_arm {
108108
// The reason for the first arm to fail is not that the match arms diverge,
109109
// but rather that there's a prior obligation that doesn't hold.
110-
None => (arm_span, ObligationCauseCode::BlockTailExpression(arm.body.hir_id)),
110+
None => {
111+
(arm_span, ObligationCauseCode::BlockTailExpression(arm.body.hir_id, match_src))
112+
}
111113
Some((prior_arm_block_id, prior_arm_ty, prior_arm_span)) => (
112114
expr.span,
113115
ObligationCauseCode::MatchExpressionArm(Box::new(MatchExpressionArmCause {
@@ -120,7 +122,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
120122
scrut_span: scrut.span,
121123
source: match_src,
122124
prior_arms: other_arms.clone(),
123-
scrut_hir_id: scrut.hir_id,
124125
opt_suggest_box_span,
125126
})),
126127
),
@@ -145,7 +146,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
145146
other_arms.remove(0);
146147
}
147148

148-
prior_arm = Some((arm_block_id, arm_ty, arm_span));
149+
if !arm_ty.is_never() {
150+
// When a match arm has type `!`, then it doesn't influence the expected type for
151+
// the following arm. If all of the prior arms are `!`, then the influence comes
152+
// from elsewhere and we shouldn't point to any previous arm.
153+
prior_arm = Some((arm_block_id, arm_ty, arm_span));
154+
}
149155
}
150156

151157
// If all of the arms in the `match` diverge,

Diff for: compiler/rustc_hir_typeck/src/coercion.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -1603,7 +1603,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
16031603
);
16041604
err.span_label(cause.span, "return type is not `()`");
16051605
}
1606-
ObligationCauseCode::BlockTailExpression(blk_id) => {
1606+
ObligationCauseCode::BlockTailExpression(blk_id, ..) => {
16071607
let parent_id = fcx.tcx.hir().parent_id(blk_id);
16081608
err = self.report_return_mismatched_types(
16091609
cause,
@@ -1650,10 +1650,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
16501650

16511651
augment_error(&mut err);
16521652

1653-
let is_insufficiently_polymorphic =
1654-
matches!(coercion_error, TypeError::RegionsInsufficientlyPolymorphic(..));
1655-
1656-
if !is_insufficiently_polymorphic && let Some(expr) = expression {
1653+
if let Some(expr) = expression {
16571654
fcx.emit_coerce_suggestions(
16581655
&mut err,
16591656
expr,
@@ -1751,7 +1748,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
17511748
) && !in_external_macro(fcx.tcx.sess, cond_expr.span)
17521749
&& !matches!(
17531750
cond_expr.kind,
1754-
hir::ExprKind::Match(.., hir::MatchSource::TryDesugar)
1751+
hir::ExprKind::Match(.., hir::MatchSource::TryDesugar(_))
17551752
)
17561753
{
17571754
err.span_label(cond_expr.span, "expected this to be `()`");

Diff for: compiler/rustc_hir_typeck/src/demand.rs

+8-16
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
8484

8585
self.annotate_expected_due_to_let_ty(err, expr, error);
8686

87+
// FIXME(#73154): For now, we do leak check when coercing function
88+
// pointers in typeck, instead of only during borrowck. This can lead
89+
// to these `RegionsInsufficientlyPolymorphic` errors that aren't helpful.
90+
if matches!(error, Some(TypeError::RegionsInsufficientlyPolymorphic(..))) {
91+
return;
92+
}
93+
8794
if self.is_destruct_assignment_desugaring(expr) {
8895
return;
8996
}
@@ -263,22 +270,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
263270
let expr_ty = self.resolve_vars_if_possible(checked_ty);
264271
let mut err = self.err_ctxt().report_mismatched_types(&cause, expected, expr_ty, e);
265272

266-
let is_insufficiently_polymorphic =
267-
matches!(e, TypeError::RegionsInsufficientlyPolymorphic(..));
268-
269-
// FIXME(#73154): For now, we do leak check when coercing function
270-
// pointers in typeck, instead of only during borrowck. This can lead
271-
// to these `RegionsInsufficientlyPolymorphic` errors that aren't helpful.
272-
if !is_insufficiently_polymorphic {
273-
self.emit_coerce_suggestions(
274-
&mut err,
275-
expr,
276-
expr_ty,
277-
expected,
278-
expected_ty_expr,
279-
Some(e),
280-
);
281-
}
273+
self.emit_coerce_suggestions(&mut err, expr, expr_ty, expected, expected_ty_expr, Some(e));
282274

283275
(expected, Some(err))
284276
}

Diff for: compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1580,7 +1580,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15801580
let coerce = ctxt.coerce.as_mut().unwrap();
15811581
if let Some((tail_expr, tail_expr_ty)) = tail_expr_ty {
15821582
let span = self.get_expr_coercion_span(tail_expr);
1583-
let cause = self.cause(span, ObligationCauseCode::BlockTailExpression(blk.hir_id));
1583+
let cause = self.cause(
1584+
span,
1585+
ObligationCauseCode::BlockTailExpression(blk.hir_id, hir::MatchSource::Normal),
1586+
);
15841587
let ty_for_diagnostic = coerce.merged_ty();
15851588
// We use coerce_inner here because we want to augment the error
15861589
// suggesting to wrap the block in square brackets if it might've

Diff for: compiler/rustc_infer/src/infer/error_reporting/mod.rs

+73-18
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,35 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
743743
ObligationCauseCode::Pattern { origin_expr: false, span: Some(span), .. } => {
744744
err.span_label(span, "expected due to this");
745745
}
746+
ObligationCauseCode::BlockTailExpression(
747+
_,
748+
hir::MatchSource::TryDesugar(scrut_hir_id),
749+
) => {
750+
if let Some(ty::error::ExpectedFound { expected, .. }) = exp_found {
751+
let scrut_expr = self.tcx.hir().expect_expr(scrut_hir_id);
752+
let scrut_ty = if let hir::ExprKind::Call(_, args) = &scrut_expr.kind {
753+
let arg_expr = args.first().expect("try desugaring call w/out arg");
754+
self.typeck_results.as_ref().and_then(|typeck_results| {
755+
typeck_results.expr_ty_opt(arg_expr)
756+
})
757+
} else {
758+
bug!("try desugaring w/out call expr as scrutinee");
759+
};
760+
761+
match scrut_ty {
762+
Some(ty) if expected == ty => {
763+
let source_map = self.tcx.sess.source_map();
764+
err.span_suggestion(
765+
source_map.end_point(cause.span()),
766+
"try removing this `?`",
767+
"",
768+
Applicability::MachineApplicable,
769+
);
770+
}
771+
_ => {}
772+
}
773+
}
774+
},
746775
ObligationCauseCode::MatchExpressionArm(box MatchExpressionArmCause {
747776
arm_block_id,
748777
arm_span,
@@ -752,12 +781,11 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
752781
prior_arm_ty,
753782
source,
754783
ref prior_arms,
755-
scrut_hir_id,
756784
opt_suggest_box_span,
757785
scrut_span,
758786
..
759787
}) => match source {
760-
hir::MatchSource::TryDesugar => {
788+
hir::MatchSource::TryDesugar(scrut_hir_id) => {
761789
if let Some(ty::error::ExpectedFound { expected, .. }) = exp_found {
762790
let scrut_expr = self.tcx.hir().expect_expr(scrut_hir_id);
763791
let scrut_ty = if let hir::ExprKind::Call(_, args) = &scrut_expr.kind {
@@ -1927,7 +1955,12 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
19271955
true
19281956
};
19291957

1930-
if should_suggest_fixes {
1958+
// FIXME(#73154): For now, we do leak check when coercing function
1959+
// pointers in typeck, instead of only during borrowck. This can lead
1960+
// to these `RegionsInsufficientlyPolymorphic` errors that aren't helpful.
1961+
if should_suggest_fixes
1962+
&& !matches!(terr, TypeError::RegionsInsufficientlyPolymorphic(..))
1963+
{
19311964
self.suggest_tuple_pattern(cause, &exp_found, diag);
19321965
self.suggest_accessing_field_where_appropriate(cause, &exp_found, diag);
19331966
self.suggest_await_on_expect_found(cause, span, &exp_found, diag);
@@ -1973,7 +2006,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
19732006
trace: &TypeTrace<'tcx>,
19742007
terr: TypeError<'tcx>,
19752008
) -> Vec<TypeErrorAdditionalDiags> {
1976-
use crate::traits::ObligationCauseCode::MatchExpressionArm;
2009+
use crate::traits::ObligationCauseCode::{BlockTailExpression, MatchExpressionArm};
19772010
let mut suggestions = Vec::new();
19782011
let span = trace.cause.span();
19792012
let values = self.resolve_vars_if_possible(trace.values);
@@ -1991,11 +2024,17 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
19912024
// specify a byte literal
19922025
(ty::Uint(ty::UintTy::U8), ty::Char) => {
19932026
if let Ok(code) = self.tcx.sess().source_map().span_to_snippet(span)
1994-
&& let Some(code) = code.strip_prefix('\'').and_then(|s| s.strip_suffix('\''))
1995-
&& !code.starts_with("\\u") // forbid all Unicode escapes
1996-
&& code.chars().next().is_some_and(|c| c.is_ascii()) // forbids literal Unicode characters beyond ASCII
2027+
&& let Some(code) =
2028+
code.strip_prefix('\'').and_then(|s| s.strip_suffix('\''))
2029+
// forbid all Unicode escapes
2030+
&& !code.starts_with("\\u")
2031+
// forbids literal Unicode characters beyond ASCII
2032+
&& code.chars().next().is_some_and(|c| c.is_ascii())
19972033
{
1998-
suggestions.push(TypeErrorAdditionalDiags::MeantByteLiteral { span, code: escape_literal(code) })
2034+
suggestions.push(TypeErrorAdditionalDiags::MeantByteLiteral {
2035+
span,
2036+
code: escape_literal(code),
2037+
})
19992038
}
20002039
}
20012040
// If a character was expected and the found expression is a string literal
@@ -2006,7 +2045,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
20062045
&& let Some(code) = code.strip_prefix('"').and_then(|s| s.strip_suffix('"'))
20072046
&& code.chars().count() == 1
20082047
{
2009-
suggestions.push(TypeErrorAdditionalDiags::MeantCharLiteral { span, code: escape_literal(code) })
2048+
suggestions.push(TypeErrorAdditionalDiags::MeantCharLiteral {
2049+
span,
2050+
code: escape_literal(code),
2051+
})
20102052
}
20112053
}
20122054
// If a string was expected and the found expression is a character literal,
@@ -2016,7 +2058,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
20162058
if let Some(code) =
20172059
code.strip_prefix('\'').and_then(|s| s.strip_suffix('\''))
20182060
{
2019-
suggestions.push(TypeErrorAdditionalDiags::MeantStrLiteral { span, code: escape_literal(code) })
2061+
suggestions.push(TypeErrorAdditionalDiags::MeantStrLiteral {
2062+
span,
2063+
code: escape_literal(code),
2064+
})
20202065
}
20212066
}
20222067
}
@@ -2025,17 +2070,24 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
20252070
(ty::Bool, ty::Tuple(list)) => if list.len() == 0 {
20262071
suggestions.extend(self.suggest_let_for_letchains(&trace.cause, span));
20272072
}
2028-
(ty::Array(_, _), ty::Array(_, _)) => suggestions.extend(self.suggest_specify_actual_length(terr, trace, span)),
2073+
(ty::Array(_, _), ty::Array(_, _)) => {
2074+
suggestions.extend(self.suggest_specify_actual_length(terr, trace, span))
2075+
}
20292076
_ => {}
20302077
}
20312078
}
20322079
let code = trace.cause.code();
2033-
if let &MatchExpressionArm(box MatchExpressionArmCause { source, .. }) = code
2034-
&& let hir::MatchSource::TryDesugar = source
2035-
&& let Some((expected_ty, found_ty, _, _)) = self.values_str(trace.values)
2036-
{
2037-
suggestions.push(TypeErrorAdditionalDiags::TryCannotConvert { found: found_ty.content(), expected: expected_ty.content() });
2038-
}
2080+
if let &(MatchExpressionArm(box MatchExpressionArmCause { source, .. })
2081+
| BlockTailExpression(.., source)
2082+
) = code
2083+
&& let hir::MatchSource::TryDesugar(_) = source
2084+
&& let Some((expected_ty, found_ty, _, _)) = self.values_str(trace.values)
2085+
{
2086+
suggestions.push(TypeErrorAdditionalDiags::TryCannotConvert {
2087+
found: found_ty.content(),
2088+
expected: expected_ty.content(),
2089+
});
2090+
}
20392091
suggestions
20402092
}
20412093

@@ -2905,8 +2957,11 @@ impl<'tcx> ObligationCauseExt<'tcx> for ObligationCause<'tcx> {
29052957
CompareImplItemObligation { kind: ty::AssocKind::Const, .. } => {
29062958
ObligationCauseFailureCode::ConstCompat { span, subdiags }
29072959
}
2960+
BlockTailExpression(.., hir::MatchSource::TryDesugar(_)) => {
2961+
ObligationCauseFailureCode::TryCompat { span, subdiags }
2962+
}
29082963
MatchExpressionArm(box MatchExpressionArmCause { source, .. }) => match source {
2909-
hir::MatchSource::TryDesugar => {
2964+
hir::MatchSource::TryDesugar(_) => {
29102965
ObligationCauseFailureCode::TryCompat { span, subdiags }
29112966
}
29122967
_ => ObligationCauseFailureCode::MatchCompat { span, subdiags },

Diff for: compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
146146

147147
if let SubregionOrigin::Subtype(box TypeTrace { cause, .. }) = sub_origin {
148148
if let ObligationCauseCode::ReturnValue(hir_id)
149-
| ObligationCauseCode::BlockTailExpression(hir_id) = cause.code()
149+
| ObligationCauseCode::BlockTailExpression(hir_id, ..) = cause.code()
150150
{
151151
let parent_id = tcx.hir().get_parent_item(*hir_id);
152152
if let Some(fn_decl) = tcx.hir().fn_decl_by_hir_id(parent_id.into()) {

Diff for: compiler/rustc_middle/src/thir.rs

+1
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ pub enum ExprKind<'tcx> {
346346
/// A `match` expression.
347347
Match {
348348
scrutinee: ExprId,
349+
scrutinee_hir_id: hir::HirId,
349350
arms: Box<[ArmId]>,
350351
},
351352
/// A block.

Diff for: compiler/rustc_middle/src/thir/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Exp
7070
visitor.visit_expr(&visitor.thir()[expr]);
7171
}
7272
Loop { body } => visitor.visit_expr(&visitor.thir()[body]),
73-
Match { scrutinee, ref arms } => {
73+
Match { scrutinee, ref arms, .. } => {
7474
visitor.visit_expr(&visitor.thir()[scrutinee]);
7575
for &arm in &**arms {
7676
visitor.visit_arm(&visitor.thir()[arm]);

Diff for: compiler/rustc_middle/src/traits/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ pub enum ObligationCauseCode<'tcx> {
402402
OpaqueReturnType(Option<(Ty<'tcx>, Span)>),
403403

404404
/// Block implicit return
405-
BlockTailExpression(hir::HirId),
405+
BlockTailExpression(hir::HirId, hir::MatchSource),
406406

407407
/// #[feature(trivial_bounds)] is not enabled
408408
TrivialBound,
@@ -543,7 +543,6 @@ pub struct MatchExpressionArmCause<'tcx> {
543543
pub scrut_span: Span,
544544
pub source: hir::MatchSource,
545545
pub prior_arms: Vec<Span>,
546-
pub scrut_hir_id: hir::HirId,
547546
pub opt_suggest_box_span: Option<Span>,
548547
}
549548

Diff for: compiler/rustc_mir_build/src/build/custom/parse/instruction.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> {
6565
let target = self.parse_block(args[1])?;
6666
self.parse_call(args[2], destination, target)
6767
},
68-
ExprKind::Match { scrutinee, arms } => {
68+
ExprKind::Match { scrutinee, arms, .. } => {
6969
let discr = self.parse_operand(*scrutinee)?;
7070
self.parse_match(arms, expr.span).map(|t| TerminatorKind::SwitchInt { discr, targets: t })
7171
},

Diff for: compiler/rustc_mir_build/src/build/expr/into.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
4747
ExprKind::Block { block: ast_block } => {
4848
this.ast_block(destination, block, ast_block, source_info)
4949
}
50-
ExprKind::Match { scrutinee, ref arms } => {
50+
ExprKind::Match { scrutinee, ref arms, .. } => {
5151
this.match_expr(destination, expr_span, block, &this.thir[scrutinee], arms)
5252
}
5353
ExprKind::If { cond, then, else_opt, if_then_scope } => {

Diff for: compiler/rustc_mir_build/src/thir/cx/expr.rs

+1
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,7 @@ impl<'tcx> Cx<'tcx> {
732732
},
733733
hir::ExprKind::Match(ref discr, ref arms, _) => ExprKind::Match {
734734
scrutinee: self.mirror_expr(discr),
735+
scrutinee_hir_id: discr.hir_id,
735736
arms: arms.iter().map(|a| self.convert_arm(a)).collect(),
736737
},
737738
hir::ExprKind::Loop(ref body, ..) => {

Diff for: compiler/rustc_mir_build/src/thir/pattern/check_match.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,12 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for MatchVisitor<'a, '_, 'tcx> {
135135
});
136136
return;
137137
}
138-
ExprKind::Match { scrutinee, box ref arms } => {
138+
ExprKind::Match { scrutinee, scrutinee_hir_id, box ref arms } => {
139139
let source = match ex.span.desugaring_kind() {
140140
Some(DesugaringKind::ForLoop) => hir::MatchSource::ForLoopDesugar,
141-
Some(DesugaringKind::QuestionMark) => hir::MatchSource::TryDesugar,
141+
Some(DesugaringKind::QuestionMark) => {
142+
hir::MatchSource::TryDesugar(scrutinee_hir_id)
143+
}
142144
Some(DesugaringKind::Await) => hir::MatchSource::AwaitDesugar,
143145
_ => hir::MatchSource::Normal,
144146
};
@@ -277,7 +279,7 @@ impl<'p, 'tcx> MatchVisitor<'_, 'p, 'tcx> {
277279
| hir::MatchSource::FormatArgs => report_arm_reachability(&cx, &report),
278280
// Unreachable patterns in try and await expressions occur when one of
279281
// the arms are an uninhabited type. Which is OK.
280-
hir::MatchSource::AwaitDesugar | hir::MatchSource::TryDesugar => {}
282+
hir::MatchSource::AwaitDesugar | hir::MatchSource::TryDesugar(_) => {}
281283
}
282284

283285
// Check if the match is exhaustive.

0 commit comments

Comments
 (0)