Skip to content

Rollup of 10 pull requests #120242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
c0a9f72
Undeprecate and use lint `unstable_features`
fmease Dec 5, 2023
bfe04e0
Fix deallocation with wrong allocator in (A)Rc::from_box_in
zachs18 Jan 9, 2024
3b325bc
Refactor uses of `objc_msgSend` to no longer have clashing definitions
madsmtm Nov 14, 2023
341f0a1
revert temporary patch #108288
onur-ozkan Jan 17, 2024
8a461aa
distribute actual stage of the compiled compiler
onur-ozkan Jan 18, 2024
12ebc3d
Add tests
Nadrieril Jan 18, 2024
753680a
Consistently set `MatchVisitor.error` on error
Nadrieril Jan 18, 2024
0a9bb97
Consistently warn unreachable subpatterns
Nadrieril Jan 18, 2024
f26f52c
Validate AggregateKind types in MIR
compiler-errors Jan 19, 2024
fc75a4e
Allow any expression blocks in `thread_local!`
nvzqz Oct 3, 2023
f52b88e
Revert example change from PR 116392
dtolnay Jan 21, 2024
c43344e
Add test of thread_local containing multiline const block
dtolnay Jan 21, 2024
b58a8a9
`maybe_lint_impl_trait`: separate `is_downgradable` from `is_object_s…
trevyn Jan 20, 2024
981e8b4
Check that a token can begin a nonterminal kind before parsing it as …
compiler-errors Jan 22, 2024
b93ae21
Do not eagerly recover malformed AST in rustfmt
compiler-errors Jan 22, 2024
5297433
Actually, just use nonterminal_may_begin_with
compiler-errors Jan 22, 2024
9454b51
Make generic const type mismatches not hide trait impls from the trai…
oli-obk Jan 17, 2024
e355b27
Rollup merge of #117910 - madsmtm:msg-send-no-clashing, r=thomcc
matthiaskrgr Jan 22, 2024
a54c295
Rollup merge of #118639 - fmease:deny-features-in-stable-rustc-crates…
matthiaskrgr Jan 22, 2024
97bcf0d
Rollup merge of #119801 - zachs18:zachs18-patch-1, r=steffahn,Nilstrieb
matthiaskrgr Jan 22, 2024
5f8988b
Rollup merge of #120058 - onur-ozkan:compiler-assemble, r=Mark-Simula…
matthiaskrgr Jan 22, 2024
d942357
Rollup merge of #120059 - oli-obk:const_arg_type_mismatch, r=compiler…
matthiaskrgr Jan 22, 2024
f194a84
Rollup merge of #120097 - Nadrieril:consistent_unreachable_subpats, r…
matthiaskrgr Jan 22, 2024
a12e2ff
Rollup merge of #120137 - compiler-errors:validate-aggregates, r=nnet…
matthiaskrgr Jan 22, 2024
6e4933f
Rollup merge of #120164 - trevyn:is_downgradable, r=compiler-errors
matthiaskrgr Jan 22, 2024
d3761de
Rollup merge of #120181 - dtolnay:tlconst, r=thomcc
matthiaskrgr Jan 22, 2024
0b0f0be
Rollup merge of #120218 - compiler-errors:parse_macro_arg, r=calebcar…
matthiaskrgr Jan 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Consistently warn unreachable subpatterns
  • Loading branch information
Nadrieril committed Jan 18, 2024
commit 0a9bb9722907cbbd75a643fad1af3278517805fc
68 changes: 37 additions & 31 deletions compiler/rustc_mir_build/src/thir/pattern/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,26 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
arms: &[MatchArm<'p, 'tcx>],
scrut_ty: Ty<'tcx>,
) -> Result<UsefulnessReport<'p, 'tcx>, ErrorGuaranteed> {
rustc_pattern_analysis::analyze_match(&cx, &arms, scrut_ty).map_err(|err| {
self.error = Err(err);
err
})
let report =
rustc_pattern_analysis::analyze_match(&cx, &arms, scrut_ty).map_err(|err| {
self.error = Err(err);
err
})?;

// Warn unreachable subpatterns.
for (arm, is_useful) in report.arm_usefulness.iter() {
if let Usefulness::Useful(redundant_subpats) = is_useful
&& !redundant_subpats.is_empty()
{
let mut redundant_subpats = redundant_subpats.clone();
// Emit lints in the order in which they occur in the file.
redundant_subpats.sort_unstable_by_key(|pat| pat.data().unwrap().span);
for pat in redundant_subpats {
report_unreachable_pattern(cx, arm.arm_data, pat.data().unwrap().span, None)
}
}
}
Ok(report)
}

#[instrument(level = "trace", skip(self))]
Expand Down Expand Up @@ -567,7 +583,6 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
) -> Result<RefutableFlag, ErrorGuaranteed> {
let (cx, report) = self.analyze_binding(pat, Refutable, scrut)?;
// Report if the pattern is unreachable, which can only occur when the type is uninhabited.
// This also reports unreachable sub-patterns.
report_arm_reachability(&cx, &report);
// If the list of witnesses is empty, the match is exhaustive, i.e. the `if let` pattern is
// irrefutable.
Expand Down Expand Up @@ -837,39 +852,30 @@ fn report_irrefutable_let_patterns(
}
}

/// Report unreachable arms, if any.
fn report_unreachable_pattern<'p, 'tcx>(
cx: &MatchCheckCtxt<'p, 'tcx>,
hir_id: HirId,
span: Span,
catchall: Option<Span>,
) {
cx.tcx.emit_spanned_lint(
UNREACHABLE_PATTERNS,
hir_id,
span,
UnreachablePattern { span: if catchall.is_some() { Some(span) } else { None }, catchall },
);
}

/// Report unreachable arms, if any.
fn report_arm_reachability<'p, 'tcx>(
cx: &MatchCheckCtxt<'p, 'tcx>,
report: &UsefulnessReport<'p, 'tcx>,
) {
let report_unreachable_pattern = |span, hir_id, catchall: Option<Span>| {
cx.tcx.emit_spanned_lint(
UNREACHABLE_PATTERNS,
hir_id,
span,
UnreachablePattern {
span: if catchall.is_some() { Some(span) } else { None },
catchall,
},
);
};

let mut catchall = None;
for (arm, is_useful) in report.arm_usefulness.iter() {
match is_useful {
Usefulness::Redundant => {
report_unreachable_pattern(arm.pat.data().unwrap().span, arm.arm_data, catchall)
}
Usefulness::Useful(redundant_subpats) if redundant_subpats.is_empty() => {}
// The arm is reachable, but contains redundant subpatterns (from or-patterns).
Usefulness::Useful(redundant_subpats) => {
let mut redundant_subpats = redundant_subpats.clone();
// Emit lints in the order in which they occur in the file.
redundant_subpats.sort_unstable_by_key(|pat| pat.data().unwrap().span);
for pat in redundant_subpats {
report_unreachable_pattern(pat.data().unwrap().span, arm.arm_data, None);
}
}
if matches!(is_useful, Usefulness::Redundant) {
report_unreachable_pattern(cx, arm.arm_data, arm.pat.data().unwrap().span, catchall)
}
if !arm.has_guard && catchall.is_none() && pat_is_catchall(arm.pat) {
catchall = Some(arm.pat.data().unwrap().span);
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/or-patterns/exhaustiveness-unreachable-pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,14 @@ fn main() {
}

fn unreachable_in_param((_ | (_, _)): (bool, bool)) {}
//~^ ERROR unreachable

fn unreachable_in_binding() {
let bool_pair = (true, true);
let bool_option = Some(true);

let (_ | (_, _)) = bool_pair;
//~^ ERROR unreachable
for (_ | (_, _)) in [bool_pair] {}
//~^ ERROR unreachable

Expand Down
20 changes: 16 additions & 4 deletions tests/ui/or-patterns/exhaustiveness-unreachable-pattern.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -184,29 +184,41 @@ error: unreachable pattern
LL | | (y, x) => {}
| ^^^^^^

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:164:30
|
LL | fn unreachable_in_param((_ | (_, _)): (bool, bool)) {}
| ^^^^^^

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:171:14
|
LL | let (_ | (_, _)) = bool_pair;
| ^^^^^^

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:173:14
|
LL | for (_ | (_, _)) in [bool_pair] {}
| ^^^^^^

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:174:20
--> $DIR/exhaustiveness-unreachable-pattern.rs:176:20
|
LL | let (Some(_) | Some(true)) = bool_option else { return };
| ^^^^^^^^^^

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:176:22
--> $DIR/exhaustiveness-unreachable-pattern.rs:178:22
|
LL | if let Some(_) | Some(true) = bool_option {}
| ^^^^^^^^^^

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:178:25
--> $DIR/exhaustiveness-unreachable-pattern.rs:180:25
|
LL | while let Some(_) | Some(true) = bool_option {}
| ^^^^^^^^^^

error: aborting due to 33 previous errors
error: aborting due to 35 previous errors

20 changes: 16 additions & 4 deletions tests/ui/rfcs/rfc-0000-never_patterns/unreachable.exh_pats.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,34 @@ LL | #![deny(unreachable_patterns)]
| ^^^^^^^^^^^^^^^^^^^^

error: unreachable pattern
--> $DIR/unreachable.rs:21:12
--> $DIR/unreachable.rs:20:19
|
LL | let (Ok(_x) | Err(!)) = res_void;
| ^^^^^^

error: unreachable pattern
--> $DIR/unreachable.rs:22:12
|
LL | if let Err(!) = res_void {}
| ^^^^^^

error: unreachable pattern
--> $DIR/unreachable.rs:23:24
--> $DIR/unreachable.rs:24:24
|
LL | if let (Ok(true) | Err(!)) = res_void {}
| ^^^^^^

error: unreachable pattern
--> $DIR/unreachable.rs:25:23
--> $DIR/unreachable.rs:26:23
|
LL | for (Ok(mut _x) | Err(!)) in [res_void] {}
| ^^^^^^

error: aborting due to 4 previous errors
error: unreachable pattern
--> $DIR/unreachable.rs:30:18
|
LL | fn foo((Ok(_x) | Err(!)): Result<bool, Void>) {}
| ^^^^^^

error: aborting due to 6 previous errors

2 changes: 2 additions & 0 deletions tests/ui/rfcs/rfc-0000-never_patterns/unreachable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ fn main() {
//[exh_pats]~^ ERROR unreachable
}
let (Ok(_x) | Err(!)) = res_void;
//[exh_pats]~^ ERROR unreachable
if let Err(!) = res_void {}
//[exh_pats]~^ ERROR unreachable
if let (Ok(true) | Err(!)) = res_void {}
Expand All @@ -27,3 +28,4 @@ fn main() {
}

fn foo((Ok(_x) | Err(!)): Result<bool, Void>) {}
//[exh_pats]~^ ERROR unreachable
1 change: 1 addition & 0 deletions tests/ui/unsafe/union_destructure.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// run-pass
#![allow(unreachable_patterns)]

#[derive(Copy, Clone)]
#[allow(dead_code)]
Expand Down