From 7aae56301d26ed67a614ac7a846dce4d655a02cb Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Thu, 22 Mar 2018 16:56:28 -0400 Subject: [PATCH 1/2] Have the HIR borrowck force the MIR borrowck Fixes #48697 --- src/librustc_borrowck/borrowck/mod.rs | 15 ++++++--------- src/test/ui/nll/issue-48697.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 9 deletions(-) create mode 100644 src/test/ui/nll/issue-48697.rs diff --git a/src/librustc_borrowck/borrowck/mod.rs b/src/librustc_borrowck/borrowck/mod.rs index 93d6247eeae47..deecea95900dc 100644 --- a/src/librustc_borrowck/borrowck/mod.rs +++ b/src/librustc_borrowck/borrowck/mod.rs @@ -120,15 +120,12 @@ fn borrowck<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, owner_def_id: DefId) used_mut_nodes: RefCell::new(FxHashSet()), }; - // Eventually, borrowck will always read the MIR, but at the - // moment we do not. So, for now, we always force MIR to be - // constructed for a given fn, since this may result in errors - // being reported and we want that to happen. - // - // Note that `mir_validated` is a "stealable" result; the - // thief, `optimized_mir()`, forces borrowck, so we know that - // is not yet stolen. - tcx.mir_validated(owner_def_id).borrow(); + // Force the MIR-based borrow checker to run; this also forces the + // MIR to be built. This may lead to errors being reported, + // particularly in NLL mode, where some ordinary region errors are + // suppressed. It's important that those errors get reported to + // avoid ICEs like #48697. + let _ = tcx.mir_borrowck(owner_def_id); // option dance because you can't capture an uninitialized variable // by mut-ref. diff --git a/src/test/ui/nll/issue-48697.rs b/src/test/ui/nll/issue-48697.rs new file mode 100644 index 0000000000000..c6948cf237c2e --- /dev/null +++ b/src/test/ui/nll/issue-48697.rs @@ -0,0 +1,26 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Test that we do not ICE when we have suppressed a lexical region +// error that would otherwise cause AST borrowck to die. +// +// Regression test for #48697. + +#![feature(nll)] +#![allow(warnings)] + +fn foo(x: &i32) -> &i32 { + let z = 4; + let f = &|y| { y }; + let k = f(&z); + f(x) +} + +fn main() {} From 6b4acccd004438f1400d50bfe5fe2cd9ff599f2e Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Fri, 23 Mar 2018 15:38:57 -0400 Subject: [PATCH 2/2] when NLL is enabled, expect weird regions in HIR borrowck --- src/librustc_borrowck/borrowck/check_loans.rs | 16 +++++++++++----- src/librustc_mir/transform/elaborate_drops.rs | 8 +++++++- src/test/ui/nll/issue-48697.rs | 2 ++ src/test/ui/nll/issue-48697.stderr | 8 ++++++++ 4 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 src/test/ui/nll/issue-48697.stderr diff --git a/src/librustc_borrowck/borrowck/check_loans.rs b/src/librustc_borrowck/borrowck/check_loans.rs index a01b3cbf47bee..8b2c9cee16a5b 100644 --- a/src/librustc_borrowck/borrowck/check_loans.rs +++ b/src/librustc_borrowck/borrowck/check_loans.rs @@ -417,11 +417,17 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> { RegionKind::ReLateBound(..) | RegionKind::ReFree(..) | RegionKind::ReStatic => { - self.bccx - .tcx - .sess.delay_span_bug(borrow_span, - &format!("unexpected region for local data {:?}", - loan_region)); + if !self.bccx.tcx.nll() { + self.bccx + .tcx + .sess.delay_span_bug(borrow_span, + &format!("unexpected region for local data {:?}", + loan_region)); + } else { + // When in NLL mode, invalid region errors get + // suppressed; it ought to be reported later, by + // the NLL region analysis. + } return } diff --git a/src/librustc_mir/transform/elaborate_drops.rs b/src/librustc_mir/transform/elaborate_drops.rs index 2e8dd623d744d..dbf3717fc0a7e 100644 --- a/src/librustc_mir/transform/elaborate_drops.rs +++ b/src/librustc_mir/transform/elaborate_drops.rs @@ -51,7 +51,13 @@ impl MirPass for ElaborateDrops { _ => return } let param_env = tcx.param_env(src.def_id); - let move_data = MoveData::gather_moves(mir, tcx).unwrap(); + let move_data = match MoveData::gather_moves(mir, tcx) { + Ok(d) => d, + Err(_) => { + tcx.sess.delay_span_bug(mir.span, "ElaborateDrops: failed to gather move data"); + return; + } + }; let elaborate_patch = { let mir = &*mir; let env = MoveDataParamEnv { diff --git a/src/test/ui/nll/issue-48697.rs b/src/test/ui/nll/issue-48697.rs index c6948cf237c2e..624b4b51dc2ec 100644 --- a/src/test/ui/nll/issue-48697.rs +++ b/src/test/ui/nll/issue-48697.rs @@ -12,6 +12,8 @@ // error that would otherwise cause AST borrowck to die. // // Regression test for #48697. +// +// run-pass #![feature(nll)] #![allow(warnings)] diff --git a/src/test/ui/nll/issue-48697.stderr b/src/test/ui/nll/issue-48697.stderr new file mode 100644 index 0000000000000..552cc6d225bdb --- /dev/null +++ b/src/test/ui/nll/issue-48697.stderr @@ -0,0 +1,8 @@ +error: internal compiler error: unexpected region for local data ReFree(DefId(0/0:3 ~ issue_48697[317d]::foo[0]), BrAnon(0)) + --> $DIR/issue-48697.rs:22:16 + | +LL | let k = f(&z); + | ^ + +error: aborting due to previous error +