Skip to content

Commit 47bb3fd

Browse files
committed
Debug flag to bypass restriction of mutation in match guards.
Now, if you pass `-Z disable-ast-check-for-mutation-in-guard`, then we will just allow you to mutably-borrow and assign in guards of `match` arms. This is wildly unsound with AST-borrowck. It is also unsound with MIR-borrowck without further adjustments, which come in later in the commit series on this Pull Request. See also rust-lang#24535 and rust-lang/rfcs#1006.
1 parent 524ad9b commit 47bb3fd

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

Diff for: src/librustc/session/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
12901290
useful for profiling / PGO."),
12911291
relro_level: Option<RelroLevel> = (None, parse_relro_level, [TRACKED],
12921292
"choose which RELRO level to use"),
1293+
disable_ast_check_for_mutation_in_guard: bool = (false, parse_bool, [UNTRACKED],
1294+
"skip AST-based mutation-in-guard check (mir-borrowck provides more precise check)"),
12931295
nll_subminimal_causes: bool = (false, parse_bool, [UNTRACKED],
12941296
"when tracking region error causes, accept subminimal results for faster execution."),
12951297
nll_facts: bool = (false, parse_bool, [UNTRACKED],

Diff for: src/librustc/ty/context.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,12 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
13441344
self.on_disk_query_result_cache.serialize(self.global_tcx(), encoder)
13451345
}
13461346

1347+
/// If true, we should use a naive AST walk to determine if match
1348+
/// guard could perform bad mutations (or mutable-borrows).
1349+
pub fn check_for_mutation_in_guard_via_ast_walk(self) -> bool {
1350+
!self.sess.opts.debugging_opts.disable_ast_check_for_mutation_in_guard
1351+
}
1352+
13471353
/// If true, we should use the MIR-based borrowck (we may *also* use
13481354
/// the AST-based borrowck).
13491355
pub fn use_mir_borrowck(self) -> bool {

Diff for: src/librustc_mir/hair/pattern/check_match.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@ impl<'a, 'tcx> MatchVisitor<'a, 'tcx> {
181181
// Second, if there is a guard on each arm, make sure it isn't
182182
// assigning or borrowing anything mutably.
183183
if let Some(ref guard) = arm.guard {
184-
check_for_mutation_in_guard(self, &guard);
184+
if self.tcx.check_for_mutation_in_guard_via_ast_walk() {
185+
check_for_mutation_in_guard(self, &guard);
186+
}
185187
}
186188

187189
// Third, perform some lints.

0 commit comments

Comments
 (0)