Skip to content

Commit 2839f4f

Browse files
committed
Get rid of token passing
1 parent b68bb63 commit 2839f4f

File tree

1 file changed

+35
-55
lines changed

1 file changed

+35
-55
lines changed

Diff for: src/librustc_mir/transform/qualify_min_const_fn.rs

+35-55
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,7 @@ use rustc::ty::{self, Predicate, TyCtxt};
55
use std::borrow::Cow;
66
use syntax_pos::Span;
77

8-
mod helper {
9-
pub struct IsMinConstFn(());
10-
/// This should only ever be used *once* and then passed around as a token.
11-
pub fn ensure_that_you_really_intended_to_create_an_instance_of_this() -> IsMinConstFn {
12-
IsMinConstFn(())
13-
}
14-
}
15-
16-
use self::helper::*;
17-
18-
type McfResult = Result<IsMinConstFn, (Span, Cow<'static, str>)>;
8+
type McfResult = Result<(), (Span, Cow<'static, str>)>;
199

2010
pub fn is_min_const_fn(
2111
tcx: TyCtxt<'a, 'tcx, 'tcx>,
@@ -74,39 +64,35 @@ pub fn is_min_const_fn(
7464
}
7565
}
7666

77-
let mut token = ensure_that_you_really_intended_to_create_an_instance_of_this();
78-
7967
for local in mir.vars_iter() {
8068
return Err((
8169
mir.local_decls[local].source_info.span,
8270
"local variables in const fn are unstable".into(),
8371
));
8472
}
8573
for local in &mir.local_decls {
86-
token = check_ty(tcx, local.ty, local.source_info.span, token)?;
74+
check_ty(tcx, local.ty, local.source_info.span)?;
8775
}
8876
// impl trait is gone in MIR, so check the return type manually
89-
token = check_ty(
77+
check_ty(
9078
tcx,
9179
tcx.fn_sig(def_id).output().skip_binder(),
9280
mir.local_decls.iter().next().unwrap().source_info.span,
93-
token,
9481
)?;
9582

9683
for bb in mir.basic_blocks() {
97-
token = check_terminator(tcx, mir, bb.terminator(), token)?;
84+
check_terminator(tcx, mir, bb.terminator())?;
9885
for stmt in &bb.statements {
99-
token = check_statement(tcx, mir, stmt, token)?;
86+
check_statement(tcx, mir, stmt)?;
10087
}
10188
}
102-
Ok(token)
89+
Ok(())
10390
}
10491

10592
fn check_ty(
10693
tcx: TyCtxt<'a, 'tcx, 'tcx>,
10794
ty: ty::Ty<'tcx>,
10895
span: Span,
109-
token: IsMinConstFn,
11096
) -> McfResult {
11197
for ty in ty.walk() {
11298
match ty.sty {
@@ -146,22 +132,21 @@ fn check_ty(
146132
_ => {}
147133
}
148134
}
149-
Ok(token)
135+
Ok(())
150136
}
151137

152138
fn check_rvalue(
153139
tcx: TyCtxt<'a, 'tcx, 'tcx>,
154140
mir: &'a Mir<'tcx>,
155141
rvalue: &Rvalue<'tcx>,
156142
span: Span,
157-
token: IsMinConstFn,
158143
) -> McfResult {
159144
match rvalue {
160145
Rvalue::Repeat(operand, _) | Rvalue::Use(operand) => {
161-
check_operand(tcx, mir, operand, span, token)
146+
check_operand(tcx, mir, operand, span)
162147
}
163148
Rvalue::Len(place) | Rvalue::Discriminant(place) | Rvalue::Ref(_, _, place) => {
164-
check_place(tcx, mir, place, span, token, PlaceMode::Read)
149+
check_place(tcx, mir, place, span, PlaceMode::Read)
165150
}
166151
Rvalue::Cast(_, operand, cast_ty) => {
167152
use rustc::ty::cast::CastTy;
@@ -175,16 +160,16 @@ fn check_rvalue(
175160
(CastTy::RPtr(_), CastTy::Float) => bug!(),
176161
(CastTy::RPtr(_), CastTy::Int(_)) => bug!(),
177162
(CastTy::Ptr(_), CastTy::RPtr(_)) => bug!(),
178-
_ => check_operand(tcx, mir, operand, span, token),
163+
_ => check_operand(tcx, mir, operand, span),
179164
}
180165
}
181166
// binops are fine on integers
182167
Rvalue::BinaryOp(_, lhs, rhs) | Rvalue::CheckedBinaryOp(_, lhs, rhs) => {
183-
let token = check_operand(tcx, mir, lhs, span, token)?;
184-
let token = check_operand(tcx, mir, rhs, span, token)?;
168+
check_operand(tcx, mir, lhs, span)?;
169+
check_operand(tcx, mir, rhs, span)?;
185170
let ty = lhs.ty(mir, tcx);
186171
if ty.is_integral() || ty.is_bool() || ty.is_char() {
187-
Ok(token)
172+
Ok(())
188173
} else {
189174
Err((
190175
span,
@@ -193,11 +178,11 @@ fn check_rvalue(
193178
}
194179
}
195180
// checked by regular const fn checks
196-
Rvalue::NullaryOp(..) => Ok(token),
181+
Rvalue::NullaryOp(..) => Ok(()),
197182
Rvalue::UnaryOp(_, operand) => {
198183
let ty = operand.ty(mir, tcx);
199184
if ty.is_integral() || ty.is_bool() {
200-
check_operand(tcx, mir, operand, span, token)
185+
check_operand(tcx, mir, operand, span)
201186
} else {
202187
Err((
203188
span,
@@ -206,11 +191,10 @@ fn check_rvalue(
206191
}
207192
}
208193
Rvalue::Aggregate(_, operands) => {
209-
let mut token = token;
210194
for operand in operands {
211-
token = check_operand(tcx, mir, operand, span, token)?;
195+
check_operand(tcx, mir, operand, span)?;
212196
}
213-
Ok(token)
197+
Ok(())
214198
}
215199
}
216200
}
@@ -224,19 +208,18 @@ fn check_statement(
224208
tcx: TyCtxt<'a, 'tcx, 'tcx>,
225209
mir: &'a Mir<'tcx>,
226210
statement: &Statement<'tcx>,
227-
token: IsMinConstFn,
228211
) -> McfResult {
229212
let span = statement.source_info.span;
230213
match &statement.kind {
231214
StatementKind::Assign(place, rval) => {
232-
let token = check_place(tcx, mir, place, span, token, PlaceMode::Assign)?;
233-
check_rvalue(tcx, mir, rval, span, token)
215+
check_place(tcx, mir, place, span, PlaceMode::Assign)?;
216+
check_rvalue(tcx, mir, rval, span)
234217
}
235218

236219
StatementKind::ReadForMatch(_) => Err((span, "match in const fn is unstable".into())),
237220

238221
// just an assignment
239-
StatementKind::SetDiscriminant { .. } => Ok(token),
222+
StatementKind::SetDiscriminant { .. } => Ok(()),
240223

241224
| StatementKind::InlineAsm { .. } => {
242225
Err((span, "cannot use inline assembly in const fn".into()))
@@ -248,7 +231,7 @@ fn check_statement(
248231
| StatementKind::Validate(..)
249232
| StatementKind::EndRegion(_)
250233
| StatementKind::UserAssertTy(..)
251-
| StatementKind::Nop => Ok(token),
234+
| StatementKind::Nop => Ok(()),
252235
}
253236
}
254237

@@ -257,13 +240,12 @@ fn check_operand(
257240
mir: &'a Mir<'tcx>,
258241
operand: &Operand<'tcx>,
259242
span: Span,
260-
token: IsMinConstFn,
261243
) -> McfResult {
262244
match operand {
263245
Operand::Move(place) | Operand::Copy(place) => {
264-
check_place(tcx, mir, place, span, token, PlaceMode::Read)
246+
check_place(tcx, mir, place, span, PlaceMode::Read)
265247
}
266-
Operand::Constant(_) => Ok(token),
248+
Operand::Constant(_) => Ok(()),
267249
}
268250
}
269251

@@ -272,26 +254,25 @@ fn check_place(
272254
mir: &'a Mir<'tcx>,
273255
place: &Place<'tcx>,
274256
span: Span,
275-
token: IsMinConstFn,
276257
mode: PlaceMode,
277258
) -> McfResult {
278259
match place {
279260
Place::Local(l) => match mode {
280261
PlaceMode::Assign => match mir.local_kind(*l) {
281-
LocalKind::Temp | LocalKind::ReturnPointer => Ok(token),
262+
LocalKind::Temp | LocalKind::ReturnPointer => Ok(()),
282263
LocalKind::Arg | LocalKind::Var => {
283264
Err((span, "assignments in const fn are unstable".into()))
284265
}
285266
},
286-
PlaceMode::Read => Ok(token),
267+
PlaceMode::Read => Ok(()),
287268
},
288269
// promoteds are always fine, they are essentially constants
289-
Place::Promoted(_) => Ok(token),
270+
Place::Promoted(_) => Ok(()),
290271
Place::Static(_) => Err((span, "cannot access `static` items in const fn".into())),
291272
Place::Projection(proj) => {
292273
match proj.elem {
293274
| ProjectionElem::Deref | ProjectionElem::Field(..) | ProjectionElem::Index(_) => {
294-
check_place(tcx, mir, &proj.base, span, token, mode)
275+
check_place(tcx, mir, &proj.base, span, mode)
295276
}
296277
// slice patterns are unstable
297278
| ProjectionElem::ConstantIndex { .. } | ProjectionElem::Subslice { .. } => {
@@ -309,20 +290,19 @@ fn check_terminator(
309290
tcx: TyCtxt<'a, 'tcx, 'tcx>,
310291
mir: &'a Mir<'tcx>,
311292
terminator: &Terminator<'tcx>,
312-
token: IsMinConstFn,
313293
) -> McfResult {
314294
let span = terminator.source_info.span;
315295
match &terminator.kind {
316296
| TerminatorKind::Goto { .. }
317297
| TerminatorKind::Return
318-
| TerminatorKind::Resume => Ok(token),
298+
| TerminatorKind::Resume => Ok(()),
319299

320300
TerminatorKind::Drop { location, .. } => {
321-
check_place(tcx, mir, location, span, token, PlaceMode::Read)
301+
check_place(tcx, mir, location, span, PlaceMode::Read)
322302
}
323303
TerminatorKind::DropAndReplace { location, value, .. } => {
324-
let token = check_place(tcx, mir, location, span, token, PlaceMode::Read)?;
325-
check_operand(tcx, mir, value, span, token)
304+
check_place(tcx, mir, location, span, PlaceMode::Read)?;
305+
check_operand(tcx, mir, value, span)
326306
},
327307
TerminatorKind::SwitchInt { .. } => Err((
328308
span,
@@ -344,12 +324,12 @@ fn check_terminator(
344324
let fn_ty = func.ty(mir, tcx);
345325
if let ty::FnDef(def_id, _) = fn_ty.sty {
346326
if tcx.is_min_const_fn(def_id) {
347-
let mut token = check_operand(tcx, mir, func, span, token)?;
327+
check_operand(tcx, mir, func, span)?;
348328

349329
for arg in args {
350-
token = check_operand(tcx, mir, arg, span, token)?;
330+
check_operand(tcx, mir, arg, span)?;
351331
}
352-
Ok(token)
332+
Ok(())
353333
} else {
354334
Err((
355335
span,
@@ -367,7 +347,7 @@ fn check_terminator(
367347
msg: _,
368348
target: _,
369349
cleanup: _,
370-
} => check_operand(tcx, mir, cond, span, token),
350+
} => check_operand(tcx, mir, cond, span),
371351

372352
| TerminatorKind::FalseEdges { .. } | TerminatorKind::FalseUnwind { .. } => span_bug!(
373353
terminator.source_info.span,

0 commit comments

Comments
 (0)