Skip to content

Commit 5a2ca1a

Browse files
committed
Auto merge of #55657 - davidtwco:issue-55651, r=pnkfelix
NLL Diagnostic Review 3: Unions not reinitialized after assignment into field Fixes #55651, #55652. This PR makes two changes: First, it updates the dataflow builder to add an init for the place containing a union if there is an assignment into the field of that union. Second, it stops a "use of uninitialized" error occuring when there is an assignment into the field of an uninitialized union that was previously initialized. Making this assignment would re-initialize the union, as tested in `src/test/ui/borrowck/borrowck-union-move-assign.nll.stderr`. The check for previous initialization ensures that we do not start supporting partial initialization yet (cc #21232, #54499, #54986). This PR also fixes #55652 which was marked as requiring investigation as the changes in this PR add an error that was previously missing (and mentioned in the review comments) and confirms that the error that was present is correct and a result of earlier partial initialization changes in NLL. r? @pnkfelix (due to earlier work with partial initialization) cc @nikomatsakis
2 parents a88613c + 1672c27 commit 5a2ca1a

File tree

6 files changed

+121
-31
lines changed

6 files changed

+121
-31
lines changed

Diff for: src/librustc/mir/mod.rs

+31
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use mir::interpret::{ConstValue, EvalErrorKind, Scalar};
2020
use mir::visit::MirVisitable;
2121
use rustc_apfloat::ieee::{Double, Single};
2222
use rustc_apfloat::Float;
23+
use rustc_data_structures::fx::FxHashSet;
2324
use rustc_data_structures::graph::dominators::{dominators, Dominators};
2425
use rustc_data_structures::graph::{self, GraphPredecessors, GraphSuccessors};
2526
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
@@ -2720,6 +2721,36 @@ impl Location {
27202721
}
27212722
}
27222723

2724+
/// Returns `true` if `other` is earlier in the control flow graph than `self`.
2725+
pub fn is_predecessor_of<'tcx>(&self, other: Location, mir: &Mir<'tcx>) -> bool {
2726+
// If we are in the same block as the other location and are an earlier statement
2727+
// then we are a predecessor of `other`.
2728+
if self.block == other.block && self.statement_index < other.statement_index {
2729+
return true;
2730+
}
2731+
2732+
// If we're in another block, then we want to check that block is a predecessor of `other`.
2733+
let mut queue: Vec<BasicBlock> = mir.predecessors_for(other.block).clone();
2734+
let mut visited = FxHashSet::default();
2735+
2736+
while let Some(block) = queue.pop() {
2737+
// If we haven't visited this block before, then make sure we visit it's predecessors.
2738+
if visited.insert(block) {
2739+
queue.append(&mut mir.predecessors_for(block).clone());
2740+
} else {
2741+
continue;
2742+
}
2743+
2744+
// If we found the block that `self` is in, then we are a predecessor of `other` (since
2745+
// we found that block by looking at the predecessors of `other`).
2746+
if self.block == block {
2747+
return true;
2748+
}
2749+
}
2750+
2751+
false
2752+
}
2753+
27232754
pub fn dominates(&self, other: Location, dominators: &Dominators<BasicBlock>) -> bool {
27242755
if self.block == other.block {
27252756
self.statement_index <= other.statement_index

Diff for: src/librustc_mir/borrow_check/mod.rs

+25-6
Original file line numberDiff line numberDiff line change
@@ -1717,12 +1717,13 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
17171717
}
17181718
}
17191719

1720-
fn check_parent_of_field<'cx, 'gcx, 'tcx>(this: &mut MirBorrowckCtxt<'cx, 'gcx, 'tcx>,
1721-
context: Context,
1722-
base: &Place<'tcx>,
1723-
span: Span,
1724-
flow_state: &Flows<'cx, 'gcx, 'tcx>)
1725-
{
1720+
fn check_parent_of_field<'cx, 'gcx, 'tcx>(
1721+
this: &mut MirBorrowckCtxt<'cx, 'gcx, 'tcx>,
1722+
context: Context,
1723+
base: &Place<'tcx>,
1724+
span: Span,
1725+
flow_state: &Flows<'cx, 'gcx, 'tcx>,
1726+
) {
17261727
// rust-lang/rust#21232: Until Rust allows reads from the
17271728
// initialized parts of partially initialized structs, we
17281729
// will, starting with the 2018 edition, reject attempts
@@ -1774,6 +1775,24 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
17741775
}
17751776

17761777
if let Some((prefix, mpi)) = shortest_uninit_seen {
1778+
// Check for a reassignment into a uninitialized field of a union (for example,
1779+
// after a move out). In this case, do not report a error here. There is an
1780+
// exception, if this is the first assignment into the union (that is, there is
1781+
// no move out from an earlier location) then this is an attempt at initialization
1782+
// of the union - we should error in that case.
1783+
let tcx = this.infcx.tcx;
1784+
if let ty::TyKind::Adt(def, _) = base.ty(this.mir, tcx).to_ty(tcx).sty {
1785+
if def.is_union() {
1786+
if this.move_data.path_map[mpi].iter().any(|moi| {
1787+
this.move_data.moves[*moi].source.is_predecessor_of(
1788+
context.loc, this.mir,
1789+
)
1790+
}) {
1791+
return;
1792+
}
1793+
}
1794+
}
1795+
17771796
this.report_use_of_moved_or_uninitialized(
17781797
context,
17791798
InitializationRequiringAction::PartialAssignment,

Diff for: src/librustc_mir/dataflow/move_paths/builder.rs

+14
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,20 @@ impl<'b, 'a, 'gcx, 'tcx> Gatherer<'b, 'a, 'gcx, 'tcx> {
430430
fn gather_init(&mut self, place: &Place<'tcx>, kind: InitKind) {
431431
debug!("gather_init({:?}, {:?})", self.loc, place);
432432

433+
let place = match place {
434+
// Check if we are assigning into a field of a union, if so, lookup the place
435+
// of the union so it is marked as initialized again.
436+
Place::Projection(box Projection {
437+
base,
438+
elem: ProjectionElem::Field(_, _),
439+
}) if match base.ty(self.builder.mir, self.builder.tcx).to_ty(self.builder.tcx).sty {
440+
ty::TyKind::Adt(def, _) if def.is_union() => true,
441+
_ => false,
442+
} => base,
443+
// Otherwise, lookup the place.
444+
_ => place,
445+
};
446+
433447
if let LookupResult::Exact(path) = self.builder.data.rev_lookup.find(place) {
434448
let init = self.builder.data.inits.push(Init {
435449
location: InitLocation::Statement(self.loc),

Diff for: src/test/ui/borrowck/borrowck-union-move-assign.nll.stderr

+1-23
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,6 @@ LL | let a = u.a; //~ ERROR use of moved value: `u.a`
88
|
99
= note: move occurs because `u` has type `U`, which does not implement the `Copy` trait
1010

11-
error[E0382]: use of moved value: `u`
12-
--> $DIR/borrowck-union-move-assign.rs:33:21
13-
|
14-
LL | let a = u.a;
15-
| --- value moved here
16-
LL | u.a = A;
17-
LL | let a = u.a; // OK
18-
| ^^^ value used here after move
19-
|
20-
= note: move occurs because `u` has type `U`, which does not implement the `Copy` trait
21-
22-
error[E0382]: use of moved value: `u`
23-
--> $DIR/borrowck-union-move-assign.rs:39:21
24-
|
25-
LL | let a = u.a;
26-
| --- value moved here
27-
LL | u.b = B;
28-
LL | let a = u.a; // OK
29-
| ^^^ value used here after move
30-
|
31-
= note: move occurs because `u` has type `U`, which does not implement the `Copy` trait
32-
33-
error: aborting due to 3 previous errors
11+
error: aborting due to previous error
3412

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

Diff for: src/test/ui/borrowck/reassignment_immutable_fields_overlapping.nll.stderr

+12-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ error[E0381]: assign to part of possibly uninitialized variable: `x`
44
LL | x.a = 1; //~ ERROR
55
| ^^^^^^^ use of possibly uninitialized `x`
66

7-
error: aborting due to previous error
7+
error[E0594]: cannot assign to `x.b`, as `x` is not declared as mutable
8+
--> $DIR/reassignment_immutable_fields_overlapping.rs:23:5
9+
|
10+
LL | let x: Foo;
11+
| - help: consider changing this to be mutable: `mut x`
12+
LL | x.a = 1; //~ ERROR
13+
LL | x.b = 22; //~ ERROR
14+
| ^^^^^^^^ cannot assign
15+
16+
error: aborting due to 2 previous errors
817

9-
For more information about this error, try `rustc --explain E0381`.
18+
Some errors occurred: E0381, E0594.
19+
For more information about an error, try `rustc --explain E0381`.

Diff for: src/test/ui/nll/issue-55651.rs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-pass
12+
13+
#![feature(untagged_unions)]
14+
15+
struct A;
16+
struct B;
17+
18+
union U {
19+
a: A,
20+
b: B,
21+
}
22+
23+
fn main() {
24+
unsafe {
25+
{
26+
let mut u = U { a: A };
27+
let a = u.a;
28+
u.a = A;
29+
let a = u.a; // OK
30+
}
31+
{
32+
let mut u = U { a: A };
33+
let a = u.a;
34+
u.b = B;
35+
let a = u.a; // OK
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)