Skip to content

Commit 6f3eb1c

Browse files
committed
Auto merge of #122454 - matthiaskrgr:rollup-xbmufdc, r=matthiaskrgr
Rollup of 11 pull requests Successful merges: - #122422 (compiletest: Allow `only-unix` in test headers) - #122424 (fix: typos) - #122425 (Increase timeout for new bors bot) - #122426 (Fix StableMIR `WrappingRange::is_full` computation) - #122429 (Add Exploit Mitigations PG to triagebot.toml) - #122430 (Generate link to `Local` in `hir::Let` documentation) - #122434 (pattern analysis: rename a few types) - #122437 (pattern analysis: remove `MaybeInfiniteInt::JustAfterMax`) - #122438 (Safe Transmute: Require that source referent is smaller than destination) - #122442 (extend docs of -Zprint-mono-items) - #122449 (Delay a bug for stranded opaques) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 5ac0b2d + c52ce4e commit 6f3eb1c

File tree

31 files changed

+374
-174
lines changed

31 files changed

+374
-174
lines changed

compiler/rustc_hir/src/hir.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1253,11 +1253,11 @@ pub struct Arm<'hir> {
12531253
pub body: &'hir Expr<'hir>,
12541254
}
12551255

1256-
/// Represents a `let <pat>[: <ty>] = <expr>` expression (not a Local), occurring in an `if-let` or
1257-
/// `let-else`, evaluating to a boolean. Typically the pattern is refutable.
1256+
/// Represents a `let <pat>[: <ty>] = <expr>` expression (not a [`Local`]), occurring in an `if-let`
1257+
/// or `let-else`, evaluating to a boolean. Typically the pattern is refutable.
12581258
///
1259-
/// In an if-let, imagine it as `if (let <pat> = <expr>) { ... }`; in a let-else, it is part of the
1260-
/// desugaring to if-let. Only let-else supports the type annotation at present.
1259+
/// In an `if let`, imagine it as `if (let <pat> = <expr>) { ... }`; in a let-else, it is part of
1260+
/// the desugaring to if-let. Only let-else supports the type annotation at present.
12611261
#[derive(Debug, Clone, Copy, HashStable_Generic)]
12621262
pub struct Let<'hir> {
12631263
pub span: Span,

compiler/rustc_hir_analysis/src/check/check.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -381,11 +381,17 @@ fn check_opaque_meets_bounds<'tcx>(
381381
match ocx.eq(&misc_cause, param_env, opaque_ty, hidden_ty) {
382382
Ok(()) => {}
383383
Err(ty_err) => {
384+
// Some types may be left "stranded" if they can't be reached
385+
// from an astconv'd bound but they're mentioned in the HIR. This
386+
// will happen, e.g., when a nested opaque is inside of a non-
387+
// existent associated type, like `impl Trait<Missing = impl Trait>`.
388+
// See <tests/ui/impl-trait/stranded-opaque.rs>.
384389
let ty_err = ty_err.to_string(tcx);
385-
tcx.dcx().span_bug(
390+
let guar = tcx.dcx().span_delayed_bug(
386391
span,
387392
format!("could not unify `{hidden_ty}` with revealed type:\n{ty_err}"),
388393
);
394+
return Err(guar);
389395
}
390396
}
391397

compiler/rustc_mir_build/src/errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_errors::{
66
};
77
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
88
use rustc_middle::ty::{self, Ty};
9-
use rustc_pattern_analysis::{errors::Uncovered, rustc::RustcMatchCheckCtxt};
9+
use rustc_pattern_analysis::{errors::Uncovered, rustc::RustcPatCtxt};
1010
use rustc_span::symbol::Symbol;
1111
use rustc_span::Span;
1212

@@ -455,7 +455,7 @@ pub enum UnusedUnsafeEnclosing {
455455
}
456456

457457
pub(crate) struct NonExhaustivePatternsTypeNotEmpty<'p, 'tcx, 'm> {
458-
pub cx: &'m RustcMatchCheckCtxt<'p, 'tcx>,
458+
pub cx: &'m RustcPatCtxt<'p, 'tcx>,
459459
pub expr_span: Span,
460460
pub span: Span,
461461
pub ty: Ty<'tcx>,

compiler/rustc_mir_build/src/thir/pattern/check_match.rs

+13-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_pattern_analysis::errors::Uncovered;
22
use rustc_pattern_analysis::rustc::{
3-
Constructor, DeconstructedPat, MatchArm, RustcMatchCheckCtxt as MatchCheckCtxt, Usefulness,
4-
UsefulnessReport, WitnessPat,
3+
Constructor, DeconstructedPat, MatchArm, RustcPatCtxt as PatCtxt, Usefulness, UsefulnessReport,
4+
WitnessPat,
55
};
66

77
use crate::errors::*;
@@ -276,7 +276,7 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
276276

277277
fn lower_pattern(
278278
&mut self,
279-
cx: &MatchCheckCtxt<'p, 'tcx>,
279+
cx: &PatCtxt<'p, 'tcx>,
280280
pat: &'p Pat<'tcx>,
281281
) -> Result<&'p DeconstructedPat<'p, 'tcx>, ErrorGuaranteed> {
282282
if let Err(err) = pat.pat_error_reported() {
@@ -375,7 +375,7 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
375375
whole_match_span: Option<Span>,
376376
scrutinee: Option<&Expr<'tcx>>,
377377
scrut_span: Span,
378-
) -> MatchCheckCtxt<'p, 'tcx> {
378+
) -> PatCtxt<'p, 'tcx> {
379379
let refutable = match refutability {
380380
Irrefutable => false,
381381
Refutable => true,
@@ -384,7 +384,7 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
384384
// require validity.
385385
let known_valid_scrutinee =
386386
scrutinee.map(|scrut| self.is_known_valid_scrutinee(scrut)).unwrap_or(true);
387-
MatchCheckCtxt {
387+
PatCtxt {
388388
tcx: self.tcx,
389389
typeck_results: self.typeck_results,
390390
param_env: self.param_env,
@@ -400,7 +400,7 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
400400

401401
fn analyze_patterns(
402402
&mut self,
403-
cx: &MatchCheckCtxt<'p, 'tcx>,
403+
cx: &PatCtxt<'p, 'tcx>,
404404
arms: &[MatchArm<'p, 'tcx>],
405405
scrut_ty: Ty<'tcx>,
406406
) -> Result<UsefulnessReport<'p, 'tcx>, ErrorGuaranteed> {
@@ -584,7 +584,7 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
584584
pat: &'p Pat<'tcx>,
585585
refutability: RefutableFlag,
586586
scrut: Option<&Expr<'tcx>>,
587-
) -> Result<(MatchCheckCtxt<'p, 'tcx>, UsefulnessReport<'p, 'tcx>), ErrorGuaranteed> {
587+
) -> Result<(PatCtxt<'p, 'tcx>, UsefulnessReport<'p, 'tcx>), ErrorGuaranteed> {
588588
let cx = self.new_cx(refutability, None, scrut, pat.span);
589589
let pat = self.lower_pattern(&cx, pat)?;
590590
let arms = [MatchArm { pat, arm_data: self.lint_level, has_guard: false }];
@@ -849,7 +849,7 @@ fn check_for_bindings_named_same_as_variants(
849849

850850
/// Check that never patterns are only used on inhabited types.
851851
fn check_never_pattern<'tcx>(
852-
cx: &MatchCheckCtxt<'_, 'tcx>,
852+
cx: &PatCtxt<'_, 'tcx>,
853853
pat: &Pat<'tcx>,
854854
) -> Result<(), ErrorGuaranteed> {
855855
if let PatKind::Never = pat.kind {
@@ -884,7 +884,7 @@ fn report_irrefutable_let_patterns(
884884

885885
/// Report unreachable arms, if any.
886886
fn report_unreachable_pattern<'p, 'tcx>(
887-
cx: &MatchCheckCtxt<'p, 'tcx>,
887+
cx: &PatCtxt<'p, 'tcx>,
888888
hir_id: HirId,
889889
span: Span,
890890
catchall: Option<Span>,
@@ -898,10 +898,7 @@ fn report_unreachable_pattern<'p, 'tcx>(
898898
}
899899

900900
/// Report unreachable arms, if any.
901-
fn report_arm_reachability<'p, 'tcx>(
902-
cx: &MatchCheckCtxt<'p, 'tcx>,
903-
report: &UsefulnessReport<'p, 'tcx>,
904-
) {
901+
fn report_arm_reachability<'p, 'tcx>(cx: &PatCtxt<'p, 'tcx>, report: &UsefulnessReport<'p, 'tcx>) {
905902
let mut catchall = None;
906903
for (arm, is_useful) in report.arm_usefulness.iter() {
907904
if matches!(is_useful, Usefulness::Redundant) {
@@ -926,7 +923,7 @@ fn pat_is_catchall(pat: &DeconstructedPat<'_, '_>) -> bool {
926923

927924
/// Report that a match is not exhaustive.
928925
fn report_non_exhaustive_match<'p, 'tcx>(
929-
cx: &MatchCheckCtxt<'p, 'tcx>,
926+
cx: &PatCtxt<'p, 'tcx>,
930927
thir: &Thir<'tcx>,
931928
scrut_ty: Ty<'tcx>,
932929
sp: Span,
@@ -1126,7 +1123,7 @@ fn report_non_exhaustive_match<'p, 'tcx>(
11261123
}
11271124

11281125
fn joined_uncovered_patterns<'p, 'tcx>(
1129-
cx: &MatchCheckCtxt<'p, 'tcx>,
1126+
cx: &PatCtxt<'p, 'tcx>,
11301127
witnesses: &[WitnessPat<'p, 'tcx>],
11311128
) -> String {
11321129
const LIMIT: usize = 3;
@@ -1147,7 +1144,7 @@ fn joined_uncovered_patterns<'p, 'tcx>(
11471144
}
11481145

11491146
fn collect_non_exhaustive_tys<'tcx>(
1150-
cx: &MatchCheckCtxt<'_, 'tcx>,
1147+
cx: &PatCtxt<'_, 'tcx>,
11511148
pat: &WitnessPat<'_, 'tcx>,
11521149
non_exhaustive_tys: &mut FxIndexSet<Ty<'tcx>>,
11531150
) {

compiler/rustc_pattern_analysis/src/constructor.rs

+14-17
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
//! - That have no non-trivial intersection with any of the constructors in the column (i.e. they're
4141
//! each either disjoint with or covered by any given column constructor).
4242
//!
43-
//! We compute this in two steps: first [`TypeCx::ctors_for_ty`] determines the
43+
//! We compute this in two steps: first [`PatCx::ctors_for_ty`] determines the
4444
//! set of all possible constructors for the type. Then [`ConstructorSet::split`] looks at the
4545
//! column of constructors and splits the set into groups accordingly. The precise invariants of
4646
//! [`ConstructorSet::split`] is described in [`SplitConstructorSet`].
@@ -136,7 +136,7 @@
136136
//! the algorithm can't distinguish them from a nonempty constructor. The only known case where this
137137
//! could happen is the `[..]` pattern on `[!; N]` with `N > 0` so we must take care to not emit it.
138138
//!
139-
//! This is all handled by [`TypeCx::ctors_for_ty`] and
139+
//! This is all handled by [`PatCx::ctors_for_ty`] and
140140
//! [`ConstructorSet::split`]. The invariants of [`SplitConstructorSet`] are also of interest.
141141
//!
142142
//!
@@ -162,7 +162,7 @@ use self::MaybeInfiniteInt::*;
162162
use self::SliceKind::*;
163163

164164
use crate::index;
165-
use crate::TypeCx;
165+
use crate::PatCx;
166166

167167
/// Whether we have seen a constructor in the column or not.
168168
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
@@ -195,8 +195,6 @@ pub enum MaybeInfiniteInt {
195195
/// Encoded value. DO NOT CONSTRUCT BY HAND; use `new_finite_{int,uint}`.
196196
#[non_exhaustive]
197197
Finite(u128),
198-
/// The integer after `u128::MAX`. We need it to represent `x..=u128::MAX` as an exclusive range.
199-
JustAfterMax,
200198
PosInfinity,
201199
}
202200

@@ -232,18 +230,18 @@ impl MaybeInfiniteInt {
232230
pub fn minus_one(self) -> Option<Self> {
233231
match self {
234232
Finite(n) => n.checked_sub(1).map(Finite),
235-
JustAfterMax => Some(Finite(u128::MAX)),
236233
x => Some(x),
237234
}
238235
}
239-
/// Note: this will not turn a finite value into an infinite one or vice-versa.
236+
/// Note: this will turn `u128::MAX` into `PosInfinity`. This means `plus_one` and `minus_one`
237+
/// are not strictly inverses, but that poses no problem in our use of them.
238+
/// this will not turn a finite value into an infinite one or vice-versa.
240239
pub fn plus_one(self) -> Option<Self> {
241240
match self {
242241
Finite(n) => match n.checked_add(1) {
243242
Some(m) => Some(Finite(m)),
244-
None => Some(JustAfterMax),
243+
None => Some(PosInfinity),
245244
},
246-
JustAfterMax => None,
247245
x => Some(x),
248246
}
249247
}
@@ -277,8 +275,7 @@ impl IntRange {
277275
}
278276

279277
/// Construct a range with these boundaries.
280-
/// `lo` must not be `PosInfinity` or `JustAfterMax`. `hi` must not be `NegInfinity`.
281-
/// If `end` is `Included`, `hi` must also not be `JustAfterMax`.
278+
/// `lo` must not be `PosInfinity`. `hi` must not be `NegInfinity`.
282279
#[inline]
283280
pub fn from_range(lo: MaybeInfiniteInt, mut hi: MaybeInfiniteInt, end: RangeEnd) -> IntRange {
284281
if end == RangeEnd::Included {
@@ -651,7 +648,7 @@ impl OpaqueId {
651648
/// constructor. `Constructor::apply` reconstructs the pattern from a pair of `Constructor` and
652649
/// `Fields`.
653650
#[derive(Debug)]
654-
pub enum Constructor<Cx: TypeCx> {
651+
pub enum Constructor<Cx: PatCx> {
655652
/// Tuples and structs.
656653
Struct,
657654
/// Enum variants.
@@ -696,7 +693,7 @@ pub enum Constructor<Cx: TypeCx> {
696693
PrivateUninhabited,
697694
}
698695

699-
impl<Cx: TypeCx> Clone for Constructor<Cx> {
696+
impl<Cx: PatCx> Clone for Constructor<Cx> {
700697
fn clone(&self) -> Self {
701698
match self {
702699
Constructor::Struct => Constructor::Struct,
@@ -720,7 +717,7 @@ impl<Cx: TypeCx> Clone for Constructor<Cx> {
720717
}
721718
}
722719

723-
impl<Cx: TypeCx> Constructor<Cx> {
720+
impl<Cx: PatCx> Constructor<Cx> {
724721
pub(crate) fn is_non_exhaustive(&self) -> bool {
725722
matches!(self, NonExhaustive)
726723
}
@@ -838,7 +835,7 @@ pub enum VariantVisibility {
838835
/// In terms of division of responsibility, [`ConstructorSet::split`] handles all of the
839836
/// `exhaustive_patterns` feature.
840837
#[derive(Debug)]
841-
pub enum ConstructorSet<Cx: TypeCx> {
838+
pub enum ConstructorSet<Cx: PatCx> {
842839
/// The type is a tuple or struct. `empty` tracks whether the type is empty.
843840
Struct { empty: bool },
844841
/// This type has the following list of constructors. If `variants` is empty and
@@ -889,13 +886,13 @@ pub enum ConstructorSet<Cx: TypeCx> {
889886
/// of the `ConstructorSet` for the type, yet if we forgot to include them in `present` we would be
890887
/// ignoring any row with `Opaque`s in the algorithm. Hence the importance of point 4.
891888
#[derive(Debug)]
892-
pub struct SplitConstructorSet<Cx: TypeCx> {
889+
pub struct SplitConstructorSet<Cx: PatCx> {
893890
pub present: SmallVec<[Constructor<Cx>; 1]>,
894891
pub missing: Vec<Constructor<Cx>>,
895892
pub missing_empty: Vec<Constructor<Cx>>,
896893
}
897894

898-
impl<Cx: TypeCx> ConstructorSet<Cx> {
895+
impl<Cx: PatCx> ConstructorSet<Cx> {
899896
/// This analyzes a column of constructors to 1/ determine which constructors of the type (if
900897
/// any) are missing; 2/ split constructors to handle non-trivial intersections e.g. on ranges
901898
/// or slices. This can get subtle; see [`SplitConstructorSet`] for details of this operation

compiler/rustc_pattern_analysis/src/errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_middle::thir::Pat;
44
use rustc_middle::ty::Ty;
55
use rustc_span::Span;
66

7-
use crate::rustc::{RustcMatchCheckCtxt, WitnessPat};
7+
use crate::rustc::{RustcPatCtxt, WitnessPat};
88

99
#[derive(Subdiagnostic)]
1010
#[label(pattern_analysis_uncovered)]
@@ -21,7 +21,7 @@ pub struct Uncovered<'tcx> {
2121
impl<'tcx> Uncovered<'tcx> {
2222
pub fn new<'p>(
2323
span: Span,
24-
cx: &RustcMatchCheckCtxt<'p, 'tcx>,
24+
cx: &RustcPatCtxt<'p, 'tcx>,
2525
witnesses: Vec<WitnessPat<'p, 'tcx>>,
2626
) -> Self
2727
where

compiler/rustc_pattern_analysis/src/lib.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub struct PrivateUninhabitedField(pub bool);
8484
/// Context that provides type information about constructors.
8585
///
8686
/// Most of the crate is parameterized on a type that implements this trait.
87-
pub trait TypeCx: Sized + fmt::Debug {
87+
pub trait PatCx: Sized + fmt::Debug {
8888
/// The type of a pattern.
8989
type Ty: Clone + fmt::Debug;
9090
/// Errors that can abort analysis.
@@ -155,34 +155,34 @@ pub trait TypeCx: Sized + fmt::Debug {
155155

156156
/// The arm of a match expression.
157157
#[derive(Debug)]
158-
pub struct MatchArm<'p, Cx: TypeCx> {
158+
pub struct MatchArm<'p, Cx: PatCx> {
159159
pub pat: &'p DeconstructedPat<Cx>,
160160
pub has_guard: bool,
161161
pub arm_data: Cx::ArmData,
162162
}
163163

164-
impl<'p, Cx: TypeCx> Clone for MatchArm<'p, Cx> {
164+
impl<'p, Cx: PatCx> Clone for MatchArm<'p, Cx> {
165165
fn clone(&self) -> Self {
166166
Self { pat: self.pat, has_guard: self.has_guard, arm_data: self.arm_data }
167167
}
168168
}
169169

170-
impl<'p, Cx: TypeCx> Copy for MatchArm<'p, Cx> {}
170+
impl<'p, Cx: PatCx> Copy for MatchArm<'p, Cx> {}
171171

172172
/// The entrypoint for this crate. Computes whether a match is exhaustive and which of its arms are
173173
/// useful, and runs some lints.
174174
#[cfg(feature = "rustc")]
175175
pub fn analyze_match<'p, 'tcx>(
176-
tycx: &rustc::RustcMatchCheckCtxt<'p, 'tcx>,
176+
tycx: &rustc::RustcPatCtxt<'p, 'tcx>,
177177
arms: &[rustc::MatchArm<'p, 'tcx>],
178178
scrut_ty: Ty<'tcx>,
179179
pattern_complexity_limit: Option<usize>,
180180
) -> Result<rustc::UsefulnessReport<'p, 'tcx>, ErrorGuaranteed> {
181181
use lints::lint_nonexhaustive_missing_variants;
182-
use usefulness::{compute_match_usefulness, ValidityConstraint};
182+
use usefulness::{compute_match_usefulness, PlaceValidity};
183183

184184
let scrut_ty = tycx.reveal_opaque_ty(scrut_ty);
185-
let scrut_validity = ValidityConstraint::from_bool(tycx.known_valid_scrutinee);
185+
let scrut_validity = PlaceValidity::from_bool(tycx.known_valid_scrutinee);
186186
let report =
187187
compute_match_usefulness(tycx, arms, scrut_ty, scrut_validity, pattern_complexity_limit)?;
188188

compiler/rustc_pattern_analysis/src/lints.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ use rustc_span::ErrorGuaranteed;
44
use crate::constructor::Constructor;
55
use crate::errors::{NonExhaustiveOmittedPattern, NonExhaustiveOmittedPatternLintOnArm, Uncovered};
66
use crate::pat_column::PatternColumn;
7-
use crate::rustc::{RevealedTy, RustcMatchCheckCtxt, WitnessPat};
7+
use crate::rustc::{RevealedTy, RustcPatCtxt, WitnessPat};
88
use crate::MatchArm;
99

1010
/// Traverse the patterns to collect any variants of a non_exhaustive enum that fail to be mentioned
1111
/// in a given column.
1212
#[instrument(level = "debug", skip(cx), ret)]
1313
fn collect_nonexhaustive_missing_variants<'p, 'tcx>(
14-
cx: &RustcMatchCheckCtxt<'p, 'tcx>,
15-
column: &PatternColumn<'p, RustcMatchCheckCtxt<'p, 'tcx>>,
14+
cx: &RustcPatCtxt<'p, 'tcx>,
15+
column: &PatternColumn<'p, RustcPatCtxt<'p, 'tcx>>,
1616
) -> Result<Vec<WitnessPat<'p, 'tcx>>, ErrorGuaranteed> {
1717
let Some(&ty) = column.head_ty() else {
1818
return Ok(Vec::new());
@@ -57,9 +57,9 @@ fn collect_nonexhaustive_missing_variants<'p, 'tcx>(
5757
}
5858

5959
pub(crate) fn lint_nonexhaustive_missing_variants<'p, 'tcx>(
60-
rcx: &RustcMatchCheckCtxt<'p, 'tcx>,
61-
arms: &[MatchArm<'p, RustcMatchCheckCtxt<'p, 'tcx>>],
62-
pat_column: &PatternColumn<'p, RustcMatchCheckCtxt<'p, 'tcx>>,
60+
rcx: &RustcPatCtxt<'p, 'tcx>,
61+
arms: &[MatchArm<'p, RustcPatCtxt<'p, 'tcx>>],
62+
pat_column: &PatternColumn<'p, RustcPatCtxt<'p, 'tcx>>,
6363
scrut_ty: RevealedTy<'tcx>,
6464
) -> Result<(), ErrorGuaranteed> {
6565
if !matches!(

0 commit comments

Comments
 (0)