Skip to content

Commit 96d9d8a

Browse files
committed
Auto merge of rust-lang#131320 - matthiaskrgr:rollup-tom15b3, r=matthiaskrgr
Rollup of 5 pull requests Successful merges: - rust-lang#129392 (Do not consider match/let/ref of place that evaluates to `!` to diverge, disallow coercions from them too) - rust-lang#131279 (update "build/host" symlink comment) - rust-lang#131312 (On function and method calls in patterns, link to the book) - rust-lang#131315 (bootstrap: add `std_features` config) - rust-lang#131316 (Fix typo in primitive_docs.rs) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 68301a6 + dd09e9c commit 96d9d8a

39 files changed

+850
-111
lines changed

compiler/rustc_hir_typeck/src/coercion.rs

+35-11
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ struct Coerce<'a, 'tcx> {
8282
/// See #47489 and #48598
8383
/// See docs on the "AllowTwoPhase" type for a more detailed discussion
8484
allow_two_phase: AllowTwoPhase,
85+
/// Whether we allow `NeverToAny` coercions. This is unsound if we're
86+
/// coercing a place expression without it counting as a read in the MIR.
87+
/// This is a side-effect of HIR not really having a great distinction
88+
/// between places and values.
89+
coerce_never: bool,
8590
}
8691

8792
impl<'a, 'tcx> Deref for Coerce<'a, 'tcx> {
@@ -125,8 +130,9 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
125130
fcx: &'f FnCtxt<'f, 'tcx>,
126131
cause: ObligationCause<'tcx>,
127132
allow_two_phase: AllowTwoPhase,
133+
coerce_never: bool,
128134
) -> Self {
129-
Coerce { fcx, cause, allow_two_phase, use_lub: false }
135+
Coerce { fcx, cause, allow_two_phase, use_lub: false, coerce_never }
130136
}
131137

132138
fn unify(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> InferResult<'tcx, Ty<'tcx>> {
@@ -177,7 +183,12 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
177183

178184
// Coercing from `!` to any type is allowed:
179185
if a.is_never() {
180-
return success(simple(Adjust::NeverToAny)(b), b, vec![]);
186+
if self.coerce_never {
187+
return success(simple(Adjust::NeverToAny)(b), b, vec![]);
188+
} else {
189+
// Otherwise the only coercion we can do is unification.
190+
return self.unify_and(a, b, identity);
191+
}
181192
}
182193

183194
// Coercing *from* an unresolved inference variable means that
@@ -1038,7 +1049,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10381049
/// The expressions *must not* have any preexisting adjustments.
10391050
pub(crate) fn coerce(
10401051
&self,
1041-
expr: &hir::Expr<'_>,
1052+
expr: &'tcx hir::Expr<'tcx>,
10421053
expr_ty: Ty<'tcx>,
10431054
mut target: Ty<'tcx>,
10441055
allow_two_phase: AllowTwoPhase,
@@ -1055,7 +1066,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10551066

10561067
let cause =
10571068
cause.unwrap_or_else(|| self.cause(expr.span, ObligationCauseCode::ExprAssignable));
1058-
let coerce = Coerce::new(self, cause, allow_two_phase);
1069+
let coerce = Coerce::new(
1070+
self,
1071+
cause,
1072+
allow_two_phase,
1073+
self.expr_guaranteed_to_constitute_read_for_never(expr),
1074+
);
10591075
let ok = self.commit_if_ok(|_| coerce.coerce(source, target))?;
10601076

10611077
let (adjustments, _) = self.register_infer_ok_obligations(ok);
@@ -1077,8 +1093,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10771093
debug!("coercion::can_with_predicates({:?} -> {:?})", source, target);
10781094

10791095
let cause = self.cause(DUMMY_SP, ObligationCauseCode::ExprAssignable);
1080-
// We don't ever need two-phase here since we throw out the result of the coercion
1081-
let coerce = Coerce::new(self, cause, AllowTwoPhase::No);
1096+
// We don't ever need two-phase here since we throw out the result of the coercion.
1097+
// We also just always set `coerce_never` to true, since this is a heuristic.
1098+
let coerce = Coerce::new(self, cause, AllowTwoPhase::No, true);
10821099
self.probe(|_| {
10831100
let Ok(ok) = coerce.coerce(source, target) else {
10841101
return false;
@@ -1090,12 +1107,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10901107
}
10911108

10921109
/// Given a type and a target type, this function will calculate and return
1093-
/// how many dereference steps needed to achieve `expr_ty <: target`. If
1110+
/// how many dereference steps needed to coerce `expr_ty` to `target`. If
10941111
/// it's not possible, return `None`.
1095-
pub(crate) fn deref_steps(&self, expr_ty: Ty<'tcx>, target: Ty<'tcx>) -> Option<usize> {
1112+
pub(crate) fn deref_steps_for_suggestion(
1113+
&self,
1114+
expr_ty: Ty<'tcx>,
1115+
target: Ty<'tcx>,
1116+
) -> Option<usize> {
10961117
let cause = self.cause(DUMMY_SP, ObligationCauseCode::ExprAssignable);
1097-
// We don't ever need two-phase here since we throw out the result of the coercion
1098-
let coerce = Coerce::new(self, cause, AllowTwoPhase::No);
1118+
// We don't ever need two-phase here since we throw out the result of the coercion.
1119+
let coerce = Coerce::new(self, cause, AllowTwoPhase::No, true);
10991120
coerce
11001121
.autoderef(DUMMY_SP, expr_ty)
11011122
.find_map(|(ty, steps)| self.probe(|_| coerce.unify(ty, target)).ok().map(|_| steps))
@@ -1252,7 +1273,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12521273
// probably aren't processing function arguments here and even if we were,
12531274
// they're going to get autorefed again anyway and we can apply 2-phase borrows
12541275
// at that time.
1255-
let mut coerce = Coerce::new(self, cause.clone(), AllowTwoPhase::No);
1276+
//
1277+
// NOTE: we set `coerce_never` to `true` here because coercion LUBs only
1278+
// operate on values and not places, so a never coercion is valid.
1279+
let mut coerce = Coerce::new(self, cause.clone(), AllowTwoPhase::No, true);
12561280
coerce.use_lub = true;
12571281

12581282
// First try to coerce the new expression to the type of the previous ones,

compiler/rustc_hir_typeck/src/expr.rs

+188-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// ignore-tidy-filelength
2+
// FIXME: we should move the field error reporting code somewhere else.
3+
14
//! Type checking expressions.
25
//!
36
//! See [`rustc_hir_analysis::check`] for more context on type checking in general.
@@ -62,7 +65,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
6265

6366
// While we don't allow *arbitrary* coercions here, we *do* allow
6467
// coercions from ! to `expected`.
65-
if ty.is_never() {
68+
if ty.is_never() && self.expr_guaranteed_to_constitute_read_for_never(expr) {
6669
if let Some(_) = self.typeck_results.borrow().adjustments().get(expr.hir_id) {
6770
let reported = self.dcx().span_delayed_bug(
6871
expr.span,
@@ -238,8 +241,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
238241
_ => self.warn_if_unreachable(expr.hir_id, expr.span, "expression"),
239242
}
240243

241-
// Any expression that produces a value of type `!` must have diverged
242-
if ty.is_never() {
244+
// Any expression that produces a value of type `!` must have diverged,
245+
// unless it's a place expression that isn't being read from, in which case
246+
// diverging would be unsound since we may never actually read the `!`.
247+
// e.g. `let _ = *never_ptr;` with `never_ptr: *const !`.
248+
if ty.is_never() && self.expr_guaranteed_to_constitute_read_for_never(expr) {
243249
self.diverges.set(self.diverges.get() | Diverges::always(expr.span));
244250
}
245251

@@ -257,6 +263,185 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
257263
ty
258264
}
259265

266+
/// Whether this expression constitutes a read of value of the type that
267+
/// it evaluates to.
268+
///
269+
/// This is used to determine if we should consider the block to diverge
270+
/// if the expression evaluates to `!`, and if we should insert a `NeverToAny`
271+
/// coercion for values of type `!`.
272+
///
273+
/// This function generally returns `false` if the expression is a place
274+
/// expression and the *parent* expression is the scrutinee of a match or
275+
/// the pointee of an `&` addr-of expression, since both of those parent
276+
/// expressions take a *place* and not a value.
277+
pub(super) fn expr_guaranteed_to_constitute_read_for_never(
278+
&self,
279+
expr: &'tcx hir::Expr<'tcx>,
280+
) -> bool {
281+
// We only care about place exprs. Anything else returns an immediate
282+
// which would constitute a read. We don't care about distinguishing
283+
// "syntactic" place exprs since if the base of a field projection is
284+
// not a place then it would've been UB to read from it anyways since
285+
// that constitutes a read.
286+
if !expr.is_syntactic_place_expr() {
287+
return true;
288+
}
289+
290+
let parent_node = self.tcx.parent_hir_node(expr.hir_id);
291+
match parent_node {
292+
hir::Node::Expr(parent_expr) => {
293+
match parent_expr.kind {
294+
// Addr-of, field projections, and LHS of assignment don't constitute reads.
295+
// Assignment does call `drop_in_place`, though, but its safety
296+
// requirements are not the same.
297+
ExprKind::AddrOf(..) | hir::ExprKind::Field(..) => false,
298+
ExprKind::Assign(lhs, _, _) => {
299+
// Only the LHS does not constitute a read
300+
expr.hir_id != lhs.hir_id
301+
}
302+
303+
// See note on `PatKind::Or` below for why this is `all`.
304+
ExprKind::Match(scrutinee, arms, _) => {
305+
assert_eq!(scrutinee.hir_id, expr.hir_id);
306+
arms.iter()
307+
.all(|arm| self.pat_guaranteed_to_constitute_read_for_never(arm.pat))
308+
}
309+
ExprKind::Let(hir::LetExpr { init, pat, .. }) => {
310+
assert_eq!(init.hir_id, expr.hir_id);
311+
self.pat_guaranteed_to_constitute_read_for_never(*pat)
312+
}
313+
314+
// Any expression child of these expressions constitute reads.
315+
ExprKind::Array(_)
316+
| ExprKind::Call(_, _)
317+
| ExprKind::MethodCall(_, _, _, _)
318+
| ExprKind::Tup(_)
319+
| ExprKind::Binary(_, _, _)
320+
| ExprKind::Unary(_, _)
321+
| ExprKind::Cast(_, _)
322+
| ExprKind::Type(_, _)
323+
| ExprKind::DropTemps(_)
324+
| ExprKind::If(_, _, _)
325+
| ExprKind::Closure(_)
326+
| ExprKind::Block(_, _)
327+
| ExprKind::AssignOp(_, _, _)
328+
| ExprKind::Index(_, _, _)
329+
| ExprKind::Break(_, _)
330+
| ExprKind::Ret(_)
331+
| ExprKind::Become(_)
332+
| ExprKind::InlineAsm(_)
333+
| ExprKind::Struct(_, _, _)
334+
| ExprKind::Repeat(_, _)
335+
| ExprKind::Yield(_, _) => true,
336+
337+
// These expressions have no (direct) sub-exprs.
338+
ExprKind::ConstBlock(_)
339+
| ExprKind::Loop(_, _, _, _)
340+
| ExprKind::Lit(_)
341+
| ExprKind::Path(_)
342+
| ExprKind::Continue(_)
343+
| ExprKind::OffsetOf(_, _)
344+
| ExprKind::Err(_) => unreachable!("no sub-expr expected for {:?}", expr.kind),
345+
}
346+
}
347+
348+
// If we have a subpattern that performs a read, we want to consider this
349+
// to diverge for compatibility to support something like `let x: () = *never_ptr;`.
350+
hir::Node::LetStmt(hir::LetStmt { init: Some(target), pat, .. }) => {
351+
assert_eq!(target.hir_id, expr.hir_id);
352+
self.pat_guaranteed_to_constitute_read_for_never(*pat)
353+
}
354+
355+
// These nodes (if they have a sub-expr) do constitute a read.
356+
hir::Node::Block(_)
357+
| hir::Node::Arm(_)
358+
| hir::Node::ExprField(_)
359+
| hir::Node::AnonConst(_)
360+
| hir::Node::ConstBlock(_)
361+
| hir::Node::ConstArg(_)
362+
| hir::Node::Stmt(_)
363+
| hir::Node::Item(hir::Item {
364+
kind: hir::ItemKind::Const(..) | hir::ItemKind::Static(..),
365+
..
366+
})
367+
| hir::Node::TraitItem(hir::TraitItem {
368+
kind: hir::TraitItemKind::Const(..), ..
369+
})
370+
| hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(..), .. }) => true,
371+
372+
// These nodes do not have direct sub-exprs.
373+
hir::Node::Param(_)
374+
| hir::Node::Item(_)
375+
| hir::Node::ForeignItem(_)
376+
| hir::Node::TraitItem(_)
377+
| hir::Node::ImplItem(_)
378+
| hir::Node::Variant(_)
379+
| hir::Node::Field(_)
380+
| hir::Node::PathSegment(_)
381+
| hir::Node::Ty(_)
382+
| hir::Node::AssocItemConstraint(_)
383+
| hir::Node::TraitRef(_)
384+
| hir::Node::Pat(_)
385+
| hir::Node::PatField(_)
386+
| hir::Node::LetStmt(_)
387+
| hir::Node::Synthetic
388+
| hir::Node::Err(_)
389+
| hir::Node::Ctor(_)
390+
| hir::Node::Lifetime(_)
391+
| hir::Node::GenericParam(_)
392+
| hir::Node::Crate(_)
393+
| hir::Node::Infer(_)
394+
| hir::Node::WhereBoundPredicate(_)
395+
| hir::Node::ArrayLenInfer(_)
396+
| hir::Node::PreciseCapturingNonLifetimeArg(_)
397+
| hir::Node::OpaqueTy(_) => {
398+
unreachable!("no sub-expr expected for {parent_node:?}")
399+
}
400+
}
401+
}
402+
403+
/// Whether this pattern constitutes a read of value of the scrutinee that
404+
/// it is matching against. This is used to determine whether we should
405+
/// perform `NeverToAny` coercions.
406+
///
407+
/// See above for the nuances of what happens when this returns true.
408+
pub(super) fn pat_guaranteed_to_constitute_read_for_never(&self, pat: &hir::Pat<'_>) -> bool {
409+
match pat.kind {
410+
// Does not constitute a read.
411+
hir::PatKind::Wild => false,
412+
413+
// This is unnecessarily restrictive when the pattern that doesn't
414+
// constitute a read is unreachable.
415+
//
416+
// For example `match *never_ptr { value => {}, _ => {} }` or
417+
// `match *never_ptr { _ if false => {}, value => {} }`.
418+
//
419+
// It is however fine to be restrictive here; only returning `true`
420+
// can lead to unsoundness.
421+
hir::PatKind::Or(subpats) => {
422+
subpats.iter().all(|pat| self.pat_guaranteed_to_constitute_read_for_never(pat))
423+
}
424+
425+
// Does constitute a read, since it is equivalent to a discriminant read.
426+
hir::PatKind::Never => true,
427+
428+
// All of these constitute a read, or match on something that isn't `!`,
429+
// which would require a `NeverToAny` coercion.
430+
hir::PatKind::Binding(_, _, _, _)
431+
| hir::PatKind::Struct(_, _, _)
432+
| hir::PatKind::TupleStruct(_, _, _)
433+
| hir::PatKind::Path(_)
434+
| hir::PatKind::Tuple(_, _)
435+
| hir::PatKind::Box(_)
436+
| hir::PatKind::Ref(_, _)
437+
| hir::PatKind::Deref(_)
438+
| hir::PatKind::Lit(_)
439+
| hir::PatKind::Range(_, _, _)
440+
| hir::PatKind::Slice(_, _, _)
441+
| hir::PatKind::Err(_) => true,
442+
}
443+
}
444+
260445
#[instrument(skip(self, expr), level = "debug")]
261446
fn check_expr_kind(
262447
&self,

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2608,7 +2608,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
26082608
}
26092609

26102610
if let hir::ExprKind::Unary(hir::UnOp::Deref, inner) = expr.kind
2611-
&& let Some(1) = self.deref_steps(expected, checked_ty)
2611+
&& let Some(1) = self.deref_steps_for_suggestion(expected, checked_ty)
26122612
{
26132613
// We have `*&T`, check if what was expected was `&T`.
26142614
// If so, we may want to suggest removing a `*`.
@@ -2738,7 +2738,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
27382738
}
27392739
}
27402740
(_, &ty::RawPtr(ty_b, mutbl_b), &ty::Ref(_, ty_a, mutbl_a)) => {
2741-
if let Some(steps) = self.deref_steps(ty_a, ty_b)
2741+
if let Some(steps) = self.deref_steps_for_suggestion(ty_a, ty_b)
27422742
// Only suggest valid if dereferencing needed.
27432743
&& steps > 0
27442744
// The pointer type implements `Copy` trait so the suggestion is always valid.
@@ -2782,7 +2782,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
27822782
}
27832783
}
27842784
_ if sp == expr.span => {
2785-
if let Some(mut steps) = self.deref_steps(checked_ty, expected) {
2785+
if let Some(mut steps) = self.deref_steps_for_suggestion(checked_ty, expected) {
27862786
let mut expr = expr.peel_blocks();
27872787
let mut prefix_span = expr.span.shrink_to_lo();
27882788
let mut remove = String::new();

compiler/rustc_parse/messages.ftl

+2-1
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,8 @@ parse_unexpected_expr_in_pat =
812812
*[false] a pattern
813813
}, found an expression
814814
815-
.label = arbitrary expressions are not allowed in patterns
815+
.label = not a pattern
816+
.note = arbitrary expressions are not allowed in patterns: <https://doc.rust-lang.org/book/ch18-00-patterns.html>
816817
817818
parse_unexpected_expr_in_pat_const_sugg = consider extracting the expression into a `const`
818819

compiler/rustc_parse/src/errors.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2608,6 +2608,7 @@ pub(crate) struct ExpectedCommaAfterPatternField {
26082608

26092609
#[derive(Diagnostic)]
26102610
#[diag(parse_unexpected_expr_in_pat)]
2611+
#[note]
26112612
pub(crate) struct UnexpectedExpressionInPattern {
26122613
/// The unexpected expr's span.
26132614
#[primary_span]

compiler/rustc_resolve/src/late/diagnostics.rs

+13
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
443443

444444
self.suggest_bare_struct_literal(&mut err);
445445
self.suggest_changing_type_to_const_param(&mut err, res, source, span);
446+
self.explain_functions_in_pattern(&mut err, res, source);
446447

447448
if self.suggest_pattern_match_with_let(&mut err, source, span) {
448449
// Fallback label.
@@ -1166,6 +1167,18 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
11661167
}
11671168
}
11681169

1170+
fn explain_functions_in_pattern(
1171+
&mut self,
1172+
err: &mut Diag<'_>,
1173+
res: Option<Res>,
1174+
source: PathSource<'_>,
1175+
) {
1176+
let PathSource::TupleStruct(_, _) = source else { return };
1177+
let Some(Res::Def(DefKind::Fn, _)) = res else { return };
1178+
err.primary_message("expected a pattern, found a function call");
1179+
err.note("function calls are not allowed in patterns: <https://doc.rust-lang.org/book/ch18-00-patterns.html>");
1180+
}
1181+
11691182
fn suggest_changing_type_to_const_param(
11701183
&mut self,
11711184
err: &mut Diag<'_>,

0 commit comments

Comments
 (0)