Skip to content

Commit

Permalink
Run check_match and check_liveness when MIR is built instead of h…
Browse files Browse the repository at this point in the history
…aving an explicit phase for them
  • Loading branch information
oli-obk committed Apr 21, 2023
1 parent 5bb58a6 commit 3344232
Show file tree
Hide file tree
Showing 18 changed files with 321 additions and 328 deletions.
21 changes: 0 additions & 21 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -761,27 +761,6 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
// passes are timed inside typeck
rustc_hir_analysis::check_crate(tcx)?;

sess.time("misc_checking_2", || {
parallel!(
{
sess.time("match_checking", || {
tcx.hir().par_body_owners(|def_id| tcx.ensure().check_match(def_id))
});
},
{
sess.time("liveness_checking", || {
tcx.hir().par_body_owners(|def_id| {
// this must run before MIR dump, because
// "not all control paths return a value" is reported here.
//
// maybe move the check to a MIR pass?
tcx.ensure().check_liveness(def_id);
});
});
}
);
});

sess.time("MIR_borrow_checking", || {
tcx.hir().par_body_owners(|def_id| tcx.ensure().mir_borrowck(def_id));
});
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_mir_build/src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ fn mir_build(tcx: TyCtxt<'_>, def: LocalDefId) -> Body<'_> {
// of `mir_build`, so now we can steal it
let thir = thir.steal();

tcx.ensure().check_match(def);
// this must run before MIR dump, because
// "not all control paths return a value" is reported here.
//
// maybe move the check to a MIR pass?
tcx.ensure().check_liveness(def);

match thir.body_type {
thir::BodyTy::Fn(fn_sig) => construct_fn(tcx, def, &thir, expr, fn_sig),
thir::BodyTy::Const(ty) => construct_const(tcx, def, &thir, expr, ty),
Expand Down
10 changes: 8 additions & 2 deletions compiler/rustc_passes/src/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{self, RootVariableMinCaptureList, Ty, TyCtxt};
use rustc_session::lint;
use rustc_span::symbol::{kw, sym, Symbol};
use rustc_span::DUMMY_SP;
use rustc_span::{BytePos, Span};

use std::collections::VecDeque;
Expand Down Expand Up @@ -586,8 +587,13 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
}

fn assigned_on_exit(&self, ln: LiveNode, var: Variable) -> bool {
let successor = self.successors[ln].unwrap();
self.assigned_on_entry(successor, var)
match self.successors[ln] {
Some(successor) => self.assigned_on_entry(successor, var),
None => {
self.ir.tcx.sess.delay_span_bug(DUMMY_SP, "no successor");
true
}
}
}

fn write_vars<F>(&self, wr: &mut dyn Write, mut test: F) -> io::Result<()>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ promoted[0] in FOO: &[&i32; 1] = {
let mut _3: *const i32; // in scope 0 at $DIR/const_promotion_extern_static.rs:+0:42: +0:43

bb0: {
_3 = const {alloc2: *const i32}; // scope 0 at $DIR/const_promotion_extern_static.rs:+0:42: +0:43
_3 = const {alloc3: *const i32}; // scope 0 at $DIR/const_promotion_extern_static.rs:+0:42: +0:43
// mir::Constant
// + span: $DIR/const_promotion_extern_static.rs:13:42: 13:43
// + literal: Const { ty: *const i32, val: Value(Scalar(alloc2)) }
// + literal: Const { ty: *const i32, val: Value(Scalar(alloc3)) }
_2 = &(*_3); // scope 0 at $DIR/const_promotion_extern_static.rs:+0:41: +0:43
_1 = [move _2]; // scope 0 at $DIR/const_promotion_extern_static.rs:+0:31: +0:46
_0 = &_1; // scope 0 at $DIR/const_promotion_extern_static.rs:+0:31: +0:55
return; // scope 0 at $DIR/const_promotion_extern_static.rs:+0:31: +0:55
}
}

alloc2 (extern static: X)
alloc3 (extern static: X)
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
- StorageLive(_3); // scope 0 at $DIR/const_promotion_extern_static.rs:+0:31: +0:46
- StorageLive(_4); // scope 0 at $DIR/const_promotion_extern_static.rs:+0:32: +0:45
- StorageLive(_5); // scope 1 at $DIR/const_promotion_extern_static.rs:+0:42: +0:43
- _5 = const {alloc2: *const i32}; // scope 1 at $DIR/const_promotion_extern_static.rs:+0:42: +0:43
- _5 = const {alloc3: *const i32}; // scope 1 at $DIR/const_promotion_extern_static.rs:+0:42: +0:43
+ _6 = const _; // scope 0 at $DIR/const_promotion_extern_static.rs:+0:31: +0:55
// mir::Constant
- // + span: $DIR/const_promotion_extern_static.rs:13:42: 13:43
- // + literal: Const { ty: *const i32, val: Value(Scalar(alloc2)) }
- // + literal: Const { ty: *const i32, val: Value(Scalar(alloc3)) }
- _4 = &(*_5); // scope 1 at $DIR/const_promotion_extern_static.rs:+0:41: +0:43
- _3 = [move _4]; // scope 0 at $DIR/const_promotion_extern_static.rs:+0:31: +0:46
- _2 = &_3; // scope 0 at $DIR/const_promotion_extern_static.rs:+0:31: +0:55
Expand Down Expand Up @@ -50,5 +50,5 @@
}
}
-
- alloc2 (extern static: X)
- alloc3 (extern static: X)

Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
error: cannot borrow value as mutable because it is also borrowed as immutable
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:36:9
|
LL | ref foo @ [.., ref mut bar] => (),
| ^^^^^^^ ----------- value is mutably borrowed by `bar` here
| |
| value is borrowed by `foo` here

error: cannot borrow value as mutable because it is also borrowed as immutable
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:120:9
|
LL | ref foo @ Some(box ref mut s) => (),
| ^^^^^^^ --------- value is mutably borrowed by `s` here
| |
| value is borrowed by `foo` here

error[E0382]: borrow of moved value: `x`
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:18:5
|
Expand Down Expand Up @@ -43,6 +27,14 @@ LL | &x;
LL | drop(r);
| - mutable borrow later used here

error: cannot borrow value as mutable because it is also borrowed as immutable
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:36:9
|
LL | ref foo @ [.., ref mut bar] => (),
| ^^^^^^^ ----------- value is mutably borrowed by `bar` here
| |
| value is borrowed by `foo` here

error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immutable
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:50:5
|
Expand Down Expand Up @@ -120,6 +112,14 @@ LL | &mut x;
LL | drop(r);
| - immutable borrow later used here

error: cannot borrow value as mutable because it is also borrowed as immutable
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:120:9
|
LL | ref foo @ Some(box ref mut s) => (),
| ^^^^^^^ --------- value is mutably borrowed by `s` here
| |
| value is borrowed by `foo` here

error[E0382]: borrow of moved value: `x`
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:134:5
|
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
error[E0004]: non-exhaustive patterns: type `u8` is non-empty
--> $DIR/pattern-matching-should-fail.rs:67:23
|
LL | let c1 = || match x { };
| ^
|
= note: the matched value is of type `u8`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern as shown
|
LL ~ let c1 = || match x {
LL + _ => todo!(),
LL ~ };
|

error[E0381]: used binding `x` isn't initialized
--> $DIR/pattern-matching-should-fail.rs:8:23
|
Expand Down Expand Up @@ -69,6 +55,20 @@ LL | let t: !;
LL | match t { };
| ^ `t` used here but it isn't initialized

error[E0004]: non-exhaustive patterns: type `u8` is non-empty
--> $DIR/pattern-matching-should-fail.rs:67:23
|
LL | let c1 = || match x { };
| ^
|
= note: the matched value is of type `u8`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern as shown
|
LL ~ let c1 = || match x {
LL + _ => todo!(),
LL ~ };
|

error[E0381]: used binding `x` isn't initialized
--> $DIR/pattern-matching-should-fail.rs:67:23
|
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/lint/lint-uppercase-variables.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ error[E0170]: pattern binding `Foo` is named the same as one of the variants of
LL | let Foo = foo::Foo::Foo;
| ^^^ help: to match on the variant, qualify the path: `foo::Foo::Foo`

error[E0170]: pattern binding `Foo` is named the same as one of the variants of the type `foo::Foo`
--> $DIR/lint-uppercase-variables.rs:33:17
|
LL | fn in_param(Foo: foo::Foo) {}
| ^^^ help: to match on the variant, qualify the path: `foo::Foo::Foo`

warning: unused variable: `Foo`
--> $DIR/lint-uppercase-variables.rs:22:9
|
Expand All @@ -37,6 +31,12 @@ warning: unused variable: `Foo`
LL | let Foo = foo::Foo::Foo;
| ^^^ help: if this is intentional, prefix it with an underscore: `_Foo`

error[E0170]: pattern binding `Foo` is named the same as one of the variants of the type `foo::Foo`
--> $DIR/lint-uppercase-variables.rs:33:17
|
LL | fn in_param(Foo: foo::Foo) {}
| ^^^ help: to match on the variant, qualify the path: `foo::Foo::Foo`

warning: unused variable: `Foo`
--> $DIR/lint-uppercase-variables.rs:33:17
|
Expand Down
24 changes: 12 additions & 12 deletions tests/ui/lint/rfc-2383-lint-reason/expect_nested_lint_levels.stderr
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
error: unused variable: `this_is_my_function`
--> $DIR/expect_nested_lint_levels.rs:48:9
|
LL | let this_is_my_function = 3;
| ^^^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_this_is_my_function`
|
note: the lint level is defined here
--> $DIR/expect_nested_lint_levels.rs:45:10
|
LL | #[forbid(unused_variables)]
| ^^^^^^^^^^^^^^^^

warning: variable does not need to be mutable
--> $DIR/expect_nested_lint_levels.rs:36:13
|
Expand All @@ -25,6 +13,18 @@ note: the lint level is defined here
LL | unused_mut,
| ^^^^^^^^^^

error: unused variable: `this_is_my_function`
--> $DIR/expect_nested_lint_levels.rs:48:9
|
LL | let this_is_my_function = 3;
| ^^^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_this_is_my_function`
|
note: the lint level is defined here
--> $DIR/expect_nested_lint_levels.rs:45:10
|
LL | #[forbid(unused_variables)]
| ^^^^^^^^^^^^^^^^

warning: this lint expectation is unfulfilled
--> $DIR/expect_nested_lint_levels.rs:7:5
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ warning: unused variable: `fox_name`
LL | let fox_name = "Sir Nibbles";
| ^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_fox_name`

warning: unused variable: `this_should_fulfill_the_expectation`
--> $DIR/force_warn_expected_lints_fulfilled.rs:43:9
|
LL | let this_should_fulfill_the_expectation = "The `#[allow]` has no power here";
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_this_should_fulfill_the_expectation`

warning: variable does not need to be mutable
--> $DIR/force_warn_expected_lints_fulfilled.rs:32:9
|
Expand All @@ -28,6 +22,12 @@ LL | let mut what_does_the_fox_say = "*ding* *deng* *dung*";
|
= note: requested on the command line with `--force-warn unused-mut`

warning: unused variable: `this_should_fulfill_the_expectation`
--> $DIR/force_warn_expected_lints_fulfilled.rs:43:9
|
LL | let this_should_fulfill_the_expectation = "The `#[allow]` has no power here";
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_this_should_fulfill_the_expectation`

warning: denote infinite loops with `loop { ... }`
--> $DIR/force_warn_expected_lints_fulfilled.rs:10:5
|
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/lint/unused/lint-unused-variables.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ note: the lint level is defined here
LL | #![deny(unused_variables)]
| ^^^^^^^^^^^^^^^^

error: unused variable: `b`
--> $DIR/lint-unused-variables.rs:14:5
|
LL | b: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_b`

error: unused variable: `a`
--> $DIR/lint-unused-variables.rs:22:9
|
LL | a: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_a`

error: unused variable: `b`
--> $DIR/lint-unused-variables.rs:14:5
|
LL | b: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_b`

error: unused variable: `b`
--> $DIR/lint-unused-variables.rs:29:9
|
Expand Down
53 changes: 27 additions & 26 deletions tests/ui/liveness/liveness-consts.stderr
Original file line number Diff line number Diff line change
@@ -1,32 +1,16 @@
warning: variable `a` is assigned to, but never used
--> $DIR/liveness-consts.rs:7:13
warning: unused variable: `e`
--> $DIR/liveness-consts.rs:24:13
|
LL | let mut a = 0;
| ^
LL | let e = 1;
| ^ help: if this is intentional, prefix it with an underscore: `_e`
|
= note: consider using `_a` instead
note: the lint level is defined here
--> $DIR/liveness-consts.rs:2:9
|
LL | #![warn(unused)]
| ^^^^^^
= note: `#[warn(unused_variables)]` implied by `#[warn(unused)]`

warning: value assigned to `b` is never read
--> $DIR/liveness-consts.rs:17:5
|
LL | b += 1;
| ^
|
= help: maybe it is overwritten before being read?
= note: `#[warn(unused_assignments)]` implied by `#[warn(unused)]`

warning: unused variable: `e`
--> $DIR/liveness-consts.rs:24:13
|
LL | let e = 1;
| ^ help: if this is intentional, prefix it with an underscore: `_e`

warning: unused variable: `s`
--> $DIR/liveness-consts.rs:33:24
|
Expand All @@ -39,6 +23,29 @@ warning: unused variable: `z`
LL | pub fn f(x: [u8; { let s = 17; 100 }]) -> [u8; { let z = 18; 100 }] {
| ^ help: if this is intentional, prefix it with an underscore: `_z`

warning: unused variable: `z`
--> $DIR/liveness-consts.rs:60:13
|
LL | let z = 42;
| ^ help: if this is intentional, prefix it with an underscore: `_z`

warning: variable `a` is assigned to, but never used
--> $DIR/liveness-consts.rs:7:13
|
LL | let mut a = 0;
| ^
|
= note: consider using `_a` instead

warning: value assigned to `b` is never read
--> $DIR/liveness-consts.rs:17:5
|
LL | b += 1;
| ^
|
= help: maybe it is overwritten before being read?
= note: `#[warn(unused_assignments)]` implied by `#[warn(unused)]`

warning: value assigned to `t` is never read
--> $DIR/liveness-consts.rs:42:9
|
Expand All @@ -53,11 +60,5 @@ warning: unused variable: `w`
LL | let w = 10;
| ^ help: if this is intentional, prefix it with an underscore: `_w`

warning: unused variable: `z`
--> $DIR/liveness-consts.rs:60:13
|
LL | let z = 42;
| ^ help: if this is intentional, prefix it with an underscore: `_z`

warning: 8 warnings emitted

Loading

0 comments on commit 3344232

Please sign in to comment.