Skip to content

Commit

Permalink
Address comments and add requested tests
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Sep 3, 2016
1 parent e67c228 commit 93067ca
Show file tree
Hide file tree
Showing 47 changed files with 582 additions and 61 deletions.
43 changes: 20 additions & 23 deletions src/librustc/middle/expr_use_visitor.rs
Expand Up @@ -671,31 +671,28 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {

// Select just those fields of the `with`
// expression that will actually be used
match with_cmt.ty.sty {
ty::TyStruct(def, substs) => {
// Consume those fields of the with expression that are needed.
for with_field in &def.struct_variant().fields {
if !contains_field_named(with_field, fields) {
let cmt_field = self.mc.cat_field(
&*with_expr,
with_cmt.clone(),
with_field.name,
with_field.ty(self.tcx(), substs)
);
self.delegate_consume(with_expr.id, with_expr.span, cmt_field);
}
if let ty::TyStruct(def, substs) = with_cmt.ty.sty {
// Consume those fields of the with expression that are needed.
for with_field in &def.struct_variant().fields {
if !contains_field_named(with_field, fields) {
let cmt_field = self.mc.cat_field(
&*with_expr,
with_cmt.clone(),
with_field.name,
with_field.ty(self.tcx(), substs)
);
self.delegate_consume(with_expr.id, with_expr.span, cmt_field);
}
}
_ => {
// the base expression should always evaluate to a
// struct; however, when EUV is run during typeck, it
// may not. This will generate an error earlier in typeck,
// so we can just ignore it.
if !self.tcx().sess.has_errors() {
span_bug!(
with_expr.span,
"with expression doesn't evaluate to a struct");
}
} else {
// the base expression should always evaluate to a
// struct; however, when EUV is run during typeck, it
// may not. This will generate an error earlier in typeck,
// so we can just ignore it.
if !self.tcx().sess.has_errors() {
span_bug!(
with_expr.span,
"with expression doesn't evaluate to a struct");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/mod.rs
Expand Up @@ -1423,7 +1423,7 @@ bitflags! {
const IS_PHANTOM_DATA = 1 << 3,
const IS_SIMD = 1 << 4,
const IS_FUNDAMENTAL = 1 << 5,
const IS_UNION = 1 << 7,
const IS_UNION = 1 << 6,
}
}

Expand Down
Expand Up @@ -102,6 +102,7 @@ impl<'a, 'tcx> RestrictionsContext<'a, 'tcx> {
let interior = interior.cleaned();
let base_ty = cmt_base.ty;
let result = self.restrict(cmt_base);
// Borrowing one union field automatically borrows all its fields.
if let ty::TyUnion(ref adt_def, _) = base_ty.sty {
match result {
RestrictionResult::Safe => RestrictionResult::Safe,
Expand Down
12 changes: 6 additions & 6 deletions src/librustc_borrowck/borrowck/move_data.rs
Expand Up @@ -442,12 +442,12 @@ impl<'a, 'tcx> MoveData<'tcx> {
self.add_assignment_helper(tcx, lp.clone(), assign_id, span, assignee_id, mode);
}

pub fn add_assignment_helper(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
lp: Rc<LoanPath<'tcx>>,
assign_id: ast::NodeId,
span: Span,
assignee_id: ast::NodeId,
mode: euv::MutateMode) {
fn add_assignment_helper(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
lp: Rc<LoanPath<'tcx>>,
assign_id: ast::NodeId,
span: Span,
assignee_id: ast::NodeId,
mode: euv::MutateMode) {
debug!("add_assignment(lp={:?}, assign_id={}, assignee_id={}",
lp, assign_id, assignee_id);

Expand Down
23 changes: 14 additions & 9 deletions src/librustc_privacy/lib.rs
Expand Up @@ -429,19 +429,24 @@ impl<'a, 'tcx, 'v> Visitor<'v> for PrivacyVisitor<'a, 'tcx> {
let method = self.tcx.tables.borrow().method_map[&method_call];
self.check_method(expr.span, method.def_id);
}
hir::ExprStruct(_, ref fields, _) => {
hir::ExprStruct(_, ref expr_fields, _) => {
let adt = self.tcx.expr_ty(expr).ty_adt_def().unwrap();
let variant = adt.variant_of_def(self.tcx.expect_def(expr.id));
// RFC 736: ensure all unmentioned fields are visible.
// Rather than computing the set of unmentioned fields
// (i.e. `all_fields - fields`), just check them all.
for field in variant.fields.iter() {
let span = if let Some(f) = fields.iter().find(|f| f.name.node == field.name) {
f.span
} else {
expr.span
};
self.check_field(span, adt, field);
// (i.e. `all_fields - fields`), just check them all,
// unless the ADT is a union, then unmentioned fields
// are not checked.
if adt.adt_kind() == ty::AdtKind::Union {
for expr_field in expr_fields {
self.check_field(expr.span, adt, variant.field_named(expr_field.name.node));
}
} else {
for field in &variant.fields {
let expr_field = expr_fields.iter().find(|f| f.name.node == field.name);
let span = if let Some(f) = expr_field { f.span } else { expr.span };
self.check_field(span, adt, field);
}
}
}
hir::ExprPath(..) => {
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_trans/glue.rs
Expand Up @@ -265,13 +265,13 @@ pub fn implement_drop_glue<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
fcx.finish(bcx, DebugLoc::None);
}

fn trans_struct_drop<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
fn trans_custom_dtor<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
t: Ty<'tcx>,
v0: ValueRef,
shallow_drop: bool)
-> Block<'blk, 'tcx>
{
debug!("trans_struct_drop t: {}", t);
debug!("trans_custom_dtor t: {}", t);
let tcx = bcx.tcx();
let mut bcx = bcx;

Expand Down Expand Up @@ -489,11 +489,11 @@ fn make_drop_glue<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, v0: ValueRef, g: DropGlueK
}
ty::TyStruct(def, _) | ty::TyEnum(def, _)
if def.dtor_kind().is_present() && !skip_dtor => {
trans_struct_drop(bcx, t, v0, false)
trans_custom_dtor(bcx, t, v0, false)
}
ty::TyUnion(def, _) => {
if def.dtor_kind().is_present() && !skip_dtor {
trans_struct_drop(bcx, t, v0, true)
trans_custom_dtor(bcx, t, v0, true)
} else {
bcx
}
Expand Down
9 changes: 4 additions & 5 deletions src/librustc_typeck/check/_match.rs
Expand Up @@ -718,12 +718,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {

// Report an error if incorrect number of the fields were specified.
if kind_name == "union" {
if fields.len() > 1 {
tcx.sess.span_err(span, "union patterns can have at most one field");
if fields.len() != 1 {
tcx.sess.span_err(span, "union patterns should have exactly one field");
}
if fields.is_empty() && !etc {
tcx.sess.span_err(span, "union patterns without `..` \
should have at least one field");
if etc {
tcx.sess.span_err(span, "`..` cannot be used in union patterns");
}
} else if !etc {
for field in variant.fields
Expand Down
21 changes: 21 additions & 0 deletions src/rt/rust_test_helpers.c
Expand Up @@ -247,3 +247,24 @@ double rust_interesting_average(uint64_t n, ...) {
int32_t rust_int8_to_int32(int8_t x) {
return (int32_t)x;
}

typedef union LARGE_INTEGER {
struct {
uint32_t LowPart;
uint32_t HighPart;
};
struct {
uint32_t LowPart;
uint32_t HighPart;
} u;
uint64_t QuadPart;
} LARGE_INTEGER;

LARGE_INTEGER increment_all_parts(LARGE_INTEGER li) {
li.LowPart += 1;
li.HighPart += 1;
li.u.LowPart += 1;
li.u.HighPart += 1;
li.QuadPart += 1;
return li;
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
30 changes: 30 additions & 0 deletions src/test/compile-fail/privacy/union-field-privacy-1.rs
@@ -0,0 +1,30 @@
// Copyright 2016 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(pub_restricted)]
#![feature(untagged_unions)]

mod m {
pub union U {
pub a: u8,
pub(super) b: u8,
c: u8,
}
}

fn main() {
let u = m::U { a: 0 }; // OK
let u = m::U { b: 0 }; // OK
let u = m::U { c: 0 }; //~ ERROR field `c` of union `m::U` is private

let m::U { a } = u; // OK
let m::U { b } = u; // OK
let m::U { c } = u; //~ ERROR field `c` of union `m::U` is private
}
28 changes: 28 additions & 0 deletions src/test/compile-fail/privacy/union-field-privacy-2.rs
@@ -0,0 +1,28 @@
// Copyright 2016 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(pub_restricted)]
#![feature(untagged_unions)]

mod m {
pub union U {
pub a: u8,
pub(super) b: u8,
c: u8,
}
}

fn main() {
let u = m::U { a: 10 };

let a = u.a; // OK
let b = u.b; // OK
let c = u.c; //~ ERROR field `c` of struct `m::U` is private
}
File renamed without changes.
File renamed without changes.
26 changes: 26 additions & 0 deletions src/test/compile-fail/union/union-copy.rs
@@ -0,0 +1,26 @@
// Copyright 2016 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(untagged_unions)]

union U {
a: u8
}

union W {
a: String
}

impl Clone for U { fn clone(&self) { panic!(); } }
impl Clone for W { fn clone(&self) { panic!(); } }
impl Copy for U {} // OK
impl Copy for W {} //~ ERROR the trait `Copy` may not be implemented for this type

fn main() {}
File renamed without changes.
File renamed without changes.
15 changes: 15 additions & 0 deletions src/test/compile-fail/union/union-feature-gate.rs
@@ -0,0 +1,15 @@
// Copyright 2016 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

union U { //~ ERROR unions are unstable and possibly buggy
a: u8,
}

fn main() {}
Expand Up @@ -24,11 +24,12 @@ fn main() {
let u = U { ..u }; //~ ERROR union expressions should have exactly one field
//~^ ERROR functional record update syntax requires a struct

let U {} = u; //~ ERROR union patterns without `..` should have at least one field
let U {} = u; //~ ERROR union patterns should have exactly one field
let U { a } = u; // OK
let U { a, b } = u; //~ ERROR union patterns can have at most one field
let U { a, b, c } = u; //~ ERROR union patterns can have at most one field
let U { a, b } = u; //~ ERROR union patterns should have exactly one field
let U { a, b, c } = u; //~ ERROR union patterns should have exactly one field
//~^ ERROR union `U` does not have a field named `c`
let U { .. } = u; // OK
let U { a, .. } = u; // OK
let U { .. } = u; //~ ERROR union patterns should have exactly one field
//~^ ERROR `..` cannot be used in union patterns
let U { a, .. } = u; //~ ERROR `..` cannot be used in union patterns
}
24 changes: 24 additions & 0 deletions src/test/compile-fail/union/union-generic.rs
@@ -0,0 +1,24 @@
// Copyright 2016 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(untagged_unions)]

use std::rc::Rc;

union U<T: Copy> {
a: T
}

fn main() {
let u = U { a: Rc::new(0u32) };
//~^ ERROR the trait bound `std::rc::Rc<u32>: std::marker::Copy` is not satisfied
let u = U::<Rc<u32>> { a: Default::default() };
//~^ ERROR the trait bound `std::rc::Rc<u32>: std::marker::Copy` is not satisfied
}
29 changes: 29 additions & 0 deletions src/test/compile-fail/union/union-repr-c.rs
@@ -0,0 +1,29 @@
// Copyright 2016 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(untagged_unions)]
#![allow(unused)]
#![deny(improper_ctypes)]

#[repr(C)]
union U {
a: u8,
}

union W {
a: u8,
}

extern "C" {
static FOREIGN1: U; // OK
static FOREIGN2: W; //~ ERROR found union without foreign-function-safe representation
}

fn main() {}
29 changes: 29 additions & 0 deletions src/test/compile-fail/union/union-suggest-field.rs
@@ -0,0 +1,29 @@
// Copyright 2016 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(untagged_unions)]

union U {
principal: u8,
}

impl U {
fn calculate(&self) {}
}

fn main() {
let u = U { principle: 0 }; //~ ERROR union `U` has no field named `principle`
//~^ HELP did you mean `principal`?
let w = u.principial; //~ ERROR attempted access of field `principial` on type `U`
//~^ HELP did you mean `principal`?

let y = u.calculate; //~ ERROR attempted to take value of method `calculate` on type `U`
//~^ HELP maybe a `()` to call it is missing?
}

0 comments on commit 93067ca

Please sign in to comment.