Skip to content

Add -Zborrowck-unreachable=no #103356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions compiler/rustc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,19 @@ fn run_compiler(
return early_exit();
}

// We need to get at least far before we have even a hope of generating a binary.
// FIXME: is that true? maybe we can ignore unresolved locals in dead code and that sort of thing
// That might require making name-resolution incremental, though.
if !sess.opts.unstable_opts.borrowck_unreachable {
// Make sure the user sees dead code warnings, so they know which items aren't checked.
// Normally that's done by `tcx.analysis()`, but we bypass that here.
queries.global_ctxt()?.peek_mut().enter(|tcx| {
tcx.hir()
.par_for_each_module(|module| tcx.ensure().check_mod_deathness(module));
});
return queries.linker().map(Some);
}

queries.global_ctxt()?;

if sess.opts.unstable_opts.no_analysis {
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_interface/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ impl<'tcx> Queries<'tcx> {
let crate_name = self.crate_name()?.peek().clone();
let outputs = self.prepare_outputs()?.peek().clone();
let dep_graph = self.dep_graph()?.peek().clone();
let (krate, resolver, lint_store) = self.expansion()?.take();
let (krate, resolver, lint_store) = self.expansion()?.peek().clone();
Ok(passes::create_global_ctxt(
self.compiler,
lint_store,
Expand All @@ -237,7 +237,9 @@ impl<'tcx> Queries<'tcx> {
self.ongoing_codegen.compute(|| {
let outputs = self.prepare_outputs()?;
self.global_ctxt()?.peek_mut().enter(|tcx| {
tcx.analysis(()).ok();
if tcx.sess.opts.unstable_opts.borrowck_unreachable {
tcx.analysis(()).ok();
}

// Don't do code generation if there were any errors
self.session().compile_status()?;
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_middle/src/mir/mono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,13 @@ impl<'tcx> MonoItem<'tcx> {
MonoItem::GlobalAsm(..) => return true,
};

// We need this for `-Zborrowck-unreachable=no`, since we don't borrowck the whole crate at once, only on-demand.
if let Some(local) = def_id.as_local() {
if tcx.hir().maybe_body_owned_by(local).is_some() {
tcx.ensure().mir_borrowck(local);
}
}

!tcx.subst_and_check_impossible_predicates((def_id, &substs))
}

Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_session/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,8 @@ options! {
binary_dep_depinfo: bool = (false, parse_bool, [TRACKED],
"include artifacts (sysroot, crate dependencies) used during compilation in dep-info \
(default: no)"),
borrowck_unreachable: bool = (true, parse_bool, [TRACKED],
"force borrowck to run even on functions that are never used"),
box_noalias: Option<bool> = (None, parse_opt_bool, [TRACKED],
"emit noalias metadata for box (default: yes)"),
branch_protection: Option<BranchProtection> = (None, parse_branch_protection, [TRACKED],
Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/fuckit/borrowck-live-code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// compile-flags: -Z borrowck-unreachable=no
// build-fail
fn live_code(s: &str) -> &'static str {
s //~ ERROR lifetime may not live long enough
}

fn main() {
println!("{}", live_code("he he he"));
}
10 changes: 10 additions & 0 deletions src/test/ui/fuckit/borrowck-live-code.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: lifetime may not live long enough
--> $DIR/borrowck-live-code.rs:4:5
|
LL | fn live_code(s: &str) -> &'static str {
| - let's call the lifetime of this reference `'1`
LL | s
| ^ returning this value requires that `'1` must outlive `'static`

error: aborting due to previous error

10 changes: 10 additions & 0 deletions src/test/ui/fuckit/no-borrowck-dead-code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// compile-flags: -Z borrowck-unreachable=no
// run-pass

fn dead_code(s: &str) -> &'static str { //~ WARNING dead_code
s
}

fn main() {
println!("he he he")
}
10 changes: 10 additions & 0 deletions src/test/ui/fuckit/no-borrowck-dead-code.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
warning: function `dead_code` is never used
--> $DIR/no-borrowck-dead-code.rs:4:4
|
LL | fn dead_code(s: &str) -> &'static str {
| ^^^^^^^^^
|
= note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted

10 changes: 10 additions & 0 deletions src/test/ui/fuckit/no-typeck-dead-code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// compile-flags: -Z borrowck-unreachable=no
// run-pass

fn dead_code(s: &str) -> bool { //~ WARNING never used
true
}

fn main() {
println!("he he he")
}
10 changes: 10 additions & 0 deletions src/test/ui/fuckit/no-typeck-dead-code.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
warning: function `dead_code` is never used
--> $DIR/no-typeck-dead-code.rs:4:4
|
LL | fn dead_code(s: &str) -> bool {
| ^^^^^^^^^
|
= note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted

8 changes: 8 additions & 0 deletions src/test/ui/fuckit/typeck-live-code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// compile-flags: -Z borrowck-unreachable=no
fn live_code(s: &str) -> bool {
s //~ ERROR mismatched types
}

fn main() {
println!("{}", live_code("he he he"));
}
11 changes: 11 additions & 0 deletions src/test/ui/fuckit/typeck-live-code.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0308]: mismatched types
--> $DIR/typeck-live-code.rs:3:5
|
LL | fn live_code(s: &str) -> bool {
| ---- expected `bool` because of return type
LL | s
| ^ expected `bool`, found `&str`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.