Handle context for const patterns correctly · rust-lang/rust@7832ebb (original) (raw)

File tree

2 files changed

lines changed

2 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -144,11 +144,17 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
144 144 let hir_context = self.tcx.local_def_id_to_hir_id(def);
145 145 let safety_context = mem::replace(&mut self.safety_context, SafetyContext::Safe);
146 146 let mut inner_visitor = UnsafetyVisitor {
147 +tcx: self.tcx,
147 148 thir: inner_thir,
148 149 hir_context,
149 150 safety_context,
151 +body_target_features: self.body_target_features,
152 +assignment_info: self.assignment_info,
153 +in_union_destructure: false,
154 +param_env: self.param_env,
155 +inside_adt: false,
150 156 warnings: self.warnings,
151 -..*self
157 +suggest_unsafe_block: self.suggest_unsafe_block,
152 158 };
153 159 inner_visitor.visit_expr(&inner_thir[expr]);
154 160 // Unsafe blocks can be used in the inner body, make sure to take it into account
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
1 +// Check that ref mut patterns within a const pattern don't get considered
2 +// unsafe because they're within a pattern for a layout constrained stuct.
3 +// check-pass
4 +
5 +#![allow(incomplete_features)]
6 +#![feature(rustc_attrs)]
7 +#![feature(inline_const_pat)]
8 +
9 +#[rustc_layout_scalar_valid_range_start(3)]
10 +struct Gt2(i32);
11 +
12 +fn main() {
13 +match unsafe { Gt2(5) } {
14 +Gt2(
15 +const {
16 + |
17 +ref mut y => (),
18 +};
19 +4
20 +},
21 +) => (),
22 + _ => (),
23 +}
24 +}