Skip to content

Commit 985fb4c

Browse files
Add helper for switching safety contexts
1 parent d5ea294 commit 985fb4c

File tree

3 files changed

+74
-51
lines changed

3 files changed

+74
-51
lines changed

compiler/rustc_mir_build/src/check_unsafety.rs

+38-23
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,36 @@ struct UnsafetyVisitor<'tcx> {
2121
}
2222

2323
impl<'tcx> UnsafetyVisitor<'tcx> {
24+
fn in_safety_context<R>(
25+
&mut self,
26+
safety_context: SafetyContext,
27+
f: impl FnOnce(&mut Self) -> R,
28+
) {
29+
if let (
30+
SafetyContext::UnsafeBlock { span: enclosing_span, .. },
31+
SafetyContext::UnsafeBlock { span: block_span, hir_id, .. },
32+
) = (self.safety_context, safety_context)
33+
{
34+
self.warn_unused_unsafe(
35+
hir_id,
36+
block_span,
37+
Some(self.tcx.sess.source_map().guess_head_span(enclosing_span)),
38+
);
39+
f(self);
40+
} else {
41+
let prev_context = self.safety_context;
42+
self.safety_context = safety_context;
43+
44+
f(self);
45+
46+
if let SafetyContext::UnsafeBlock { used: false, span, hir_id } = self.safety_context {
47+
self.warn_unused_unsafe(hir_id, span, self.body_unsafety.unsafe_fn_sig_span());
48+
}
49+
self.safety_context = prev_context;
50+
return;
51+
}
52+
}
53+
2454
fn requires_unsafe(&mut self, span: Span, kind: UnsafeOpKind) {
2555
let (description, note) = kind.description_and_note();
2656
let unsafe_op_in_unsafe_fn_allowed = self.unsafe_op_in_unsafe_fn_allowed();
@@ -100,40 +130,25 @@ impl<'tcx> UnsafetyVisitor<'tcx> {
100130
impl<'thir, 'tcx> Visitor<'thir, 'tcx> for UnsafetyVisitor<'tcx> {
101131
fn visit_block(&mut self, block: &Block<'thir, 'tcx>) {
102132
if let BlockSafety::ExplicitUnsafe(hir_id) = block.safety_mode {
103-
if let SafetyContext::UnsafeBlock { span: enclosing_span, .. } = self.safety_context {
104-
self.warn_unused_unsafe(
105-
hir_id,
106-
block.span,
107-
Some(self.tcx.sess.source_map().guess_head_span(enclosing_span)),
108-
);
109-
} else {
110-
let prev_context = self.safety_context;
111-
self.safety_context =
112-
SafetyContext::UnsafeBlock { span: block.span, hir_id, used: false };
113-
visit::walk_block(self, block);
114-
if let SafetyContext::UnsafeBlock { used: false, span, hir_id } =
115-
self.safety_context
116-
{
117-
self.warn_unused_unsafe(hir_id, span, self.body_unsafety.unsafe_fn_sig_span());
118-
}
119-
self.safety_context = prev_context;
120-
return;
121-
}
133+
self.in_safety_context(
134+
SafetyContext::UnsafeBlock { span: block.span, hir_id, used: false },
135+
|this| visit::walk_block(this, block),
136+
);
137+
} else {
138+
visit::walk_block(self, block);
122139
}
123-
124-
visit::walk_block(self, block);
125140
}
126141

127142
fn visit_expr(&mut self, expr: &'thir Expr<'thir, 'tcx>) {
128143
match expr.kind {
129-
ExprKind::Scope { value, lint_level: LintLevel::Explicit(hir_id), .. } => {
144+
ExprKind::Scope { value, lint_level: LintLevel::Explicit(hir_id), region_scope: _ } => {
130145
let prev_id = self.hir_context;
131146
self.hir_context = hir_id;
132147
self.visit_expr(value);
133148
self.hir_context = prev_id;
134149
return;
135150
}
136-
ExprKind::Call { fun, .. } => {
151+
ExprKind::Call { fun, ty: _, args: _, from_hir_call: _, fn_span: _ } => {
137152
if fun.ty.fn_sig(self.tcx).unsafety() == hir::Unsafety::Unsafe {
138153
self.requires_unsafe(expr.span, CallToUnsafeFunction);
139154
}

compiler/rustc_mir_build/src/thir/visit.rs

+34-26
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub fn walk_expr<'thir, 'tcx, V: Visitor<'thir, 'tcx>>(
2626
) {
2727
use ExprKind::*;
2828
match expr.kind {
29-
Scope { value, .. } => visitor.visit_expr(value),
29+
Scope { value, region_scope: _, lint_level: _ } => visitor.visit_expr(value),
3030
Box { value } => visitor.visit_expr(value),
3131
If { cond, then, else_opt } => {
3232
visitor.visit_expr(cond);
@@ -35,22 +35,22 @@ pub fn walk_expr<'thir, 'tcx, V: Visitor<'thir, 'tcx>>(
3535
visitor.visit_expr(else_expr);
3636
}
3737
}
38-
Call { fun, args, .. } => {
38+
Call { fun, args, ty: _, from_hir_call: _, fn_span: _ } => {
3939
visitor.visit_expr(fun);
4040
for arg in args {
4141
visitor.visit_expr(arg);
4242
}
4343
}
4444
Deref { arg } => visitor.visit_expr(arg),
45-
Binary { lhs, rhs, .. } | LogicalOp { lhs, rhs, .. } => {
45+
Binary { lhs, rhs, op: _ } | LogicalOp { lhs, rhs, op: _ } => {
4646
visitor.visit_expr(lhs);
4747
visitor.visit_expr(rhs);
4848
}
49-
Unary { arg, .. } => visitor.visit_expr(arg),
49+
Unary { arg, op: _ } => visitor.visit_expr(arg),
5050
Cast { source } => visitor.visit_expr(source),
5151
Use { source } => visitor.visit_expr(source),
5252
NeverToAny { source } => visitor.visit_expr(source),
53-
Pointer { source, .. } => visitor.visit_expr(source),
53+
Pointer { source, cast: _ } => visitor.visit_expr(source),
5454
Loop { body } => visitor.visit_expr(body),
5555
Match { scrutinee, arms } => {
5656
visitor.visit_expr(scrutinee);
@@ -59,24 +59,24 @@ pub fn walk_expr<'thir, 'tcx, V: Visitor<'thir, 'tcx>>(
5959
}
6060
}
6161
Block { ref body } => visitor.visit_block(body),
62-
Assign { lhs, rhs } | AssignOp { lhs, rhs, .. } => {
62+
Assign { lhs, rhs } | AssignOp { lhs, rhs, op: _ } => {
6363
visitor.visit_expr(lhs);
6464
visitor.visit_expr(rhs);
6565
}
66-
Field { lhs, .. } => visitor.visit_expr(lhs),
66+
Field { lhs, name: _ } => visitor.visit_expr(lhs),
6767
Index { lhs, index } => {
6868
visitor.visit_expr(lhs);
6969
visitor.visit_expr(index);
7070
}
71-
VarRef { .. } | UpvarRef { .. } => {}
72-
Borrow { arg, .. } => visitor.visit_expr(arg),
73-
AddressOf { arg, .. } => visitor.visit_expr(arg),
74-
Break { value, .. } => {
71+
VarRef { id: _ } | UpvarRef { closure_def_id: _, var_hir_id: _ } => {}
72+
Borrow { arg, borrow_kind: _ } => visitor.visit_expr(arg),
73+
AddressOf { arg, mutability: _ } => visitor.visit_expr(arg),
74+
Break { value, label: _ } => {
7575
if let Some(value) = value {
7676
visitor.visit_expr(value)
7777
}
7878
}
79-
Continue { .. } => {}
79+
Continue { label: _ } => {}
8080
Return { value } => {
8181
if let Some(value) = value {
8282
visitor.visit_expr(value)
@@ -92,40 +92,42 @@ pub fn walk_expr<'thir, 'tcx, V: Visitor<'thir, 'tcx>>(
9292
visitor.visit_expr(field);
9393
}
9494
}
95-
Adt { fields, ref base, .. } => {
95+
Adt { fields, ref base, adt_def: _, variant_index: _, substs: _, user_ty: _ } => {
9696
for field in fields {
9797
visitor.visit_expr(field.expr);
9898
}
9999
if let Some(base) = base {
100100
visitor.visit_expr(base.base);
101101
}
102102
}
103-
PlaceTypeAscription { source, .. } | ValueTypeAscription { source, .. } => {
103+
PlaceTypeAscription { source, user_ty: _ } | ValueTypeAscription { source, user_ty: _ } => {
104104
visitor.visit_expr(source)
105105
}
106-
Closure { .. } => {}
107-
Literal { literal, .. } => visitor.visit_const(literal),
108-
StaticRef { literal, .. } => visitor.visit_const(literal),
109-
InlineAsm { operands, .. } => {
106+
Closure { closure_id: _, substs: _, upvars: _, movability: _, fake_reads: _ } => {}
107+
Literal { literal, user_ty: _, const_id: _ } => visitor.visit_const(literal),
108+
StaticRef { literal, def_id: _ } => visitor.visit_const(literal),
109+
InlineAsm { operands, template: _, options: _, line_spans: _ } => {
110110
for op in operands {
111111
use InlineAsmOperand::*;
112112
match op {
113-
In { expr, .. }
114-
| Out { expr: Some(expr), .. }
115-
| InOut { expr, .. }
113+
In { expr, reg: _ }
114+
| Out { expr: Some(expr), reg: _, late: _ }
115+
| InOut { expr, reg: _, late: _ }
116116
| SymFn { expr } => visitor.visit_expr(expr),
117-
SplitInOut { in_expr, out_expr, .. } => {
117+
SplitInOut { in_expr, out_expr, reg: _, late: _ } => {
118118
visitor.visit_expr(in_expr);
119119
if let Some(out_expr) = out_expr {
120120
visitor.visit_expr(out_expr);
121121
}
122122
}
123-
Out { .. } | Const { .. } | SymStatic { .. } => {}
123+
Out { expr: None, reg: _, late: _ }
124+
| Const { value: _, span: _ }
125+
| SymStatic { def_id: _ } => {}
124126
}
125127
}
126128
}
127129
ThreadLocalRef(_) => {}
128-
LlvmInlineAsm { outputs, inputs, .. } => {
130+
LlvmInlineAsm { outputs, inputs, asm: _ } => {
129131
for out_expr in outputs {
130132
visitor.visit_expr(out_expr);
131133
}
@@ -142,8 +144,14 @@ pub fn walk_stmt<'thir, 'tcx, V: Visitor<'thir, 'tcx>>(
142144
stmt: &'thir Stmt<'thir, 'tcx>,
143145
) {
144146
match stmt.kind {
145-
StmtKind::Expr { expr, .. } => visitor.visit_expr(expr),
146-
StmtKind::Let { pattern: _, initializer, .. } => {
147+
StmtKind::Expr { expr, scope: _ } => visitor.visit_expr(expr),
148+
StmtKind::Let {
149+
initializer,
150+
remainder_scope: _,
151+
init_scope: _,
152+
pattern: _,
153+
lint_level: _,
154+
} => {
147155
if let Some(init) = initializer {
148156
visitor.visit_expr(init);
149157
}

src/tools/tidy/src/ui_tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use std::path::Path;
77

88
const ENTRY_LIMIT: usize = 1000;
99
// FIXME: The following limits should be reduced eventually.
10-
const ROOT_ENTRY_LIMIT: usize = 1369;
11-
const ISSUES_ENTRY_LIMIT: usize = 2551;
10+
const ROOT_ENTRY_LIMIT: usize = 1370;
11+
const ISSUES_ENTRY_LIMIT: usize = 2555;
1212

1313
fn check_entries(path: &Path, bad: &mut bool) {
1414
let dirs = walkdir::WalkDir::new(&path.join("test/ui"))

0 commit comments

Comments
 (0)