Skip to content

Commit

Permalink
Auto merge of #34623 - lfairy:repr-conflict, r=Aatch
Browse files Browse the repository at this point in the history
Warn about multiple conflicting #[repr] hints

Closes #34622
  • Loading branch information
bors authored Aug 31, 2016
2 parents eac4146 + 42b75a5 commit 603d9cc
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/librustc/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1804,4 +1804,5 @@ register_diagnostics! {
E0490, // a value of type `..` is borrowed for too long
E0491, // in type `..`, reference has a longer lifetime than the data it...
E0495, // cannot infer an appropriate lifetime due to conflicting requirements
E0566 // conflicting representation hints
}
19 changes: 18 additions & 1 deletion src/librustc/hir/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ impl<'a> CheckAttrVisitor<'a> {
}
};

let mut conflicting_reprs = 0;
for word in words {
let name = match word.name() {
Some(word) => word,
Expand All @@ -60,13 +61,24 @@ impl<'a> CheckAttrVisitor<'a> {

let message = match &*name {
"C" => {
conflicting_reprs += 1;
if target != Target::Struct && target != Target::Enum {
"attribute should be applied to struct or enum"
} else {
continue
}
}
"packed" | "simd" => {
"packed" => {
// Do not increment conflicting_reprs here, because "packed"
// can be used to modify another repr hint
if target != Target::Struct {
"attribute should be applied to struct"
} else {
continue
}
}
"simd" => {
conflicting_reprs += 1;
if target != Target::Struct {
"attribute should be applied to struct"
} else {
Expand All @@ -76,6 +88,7 @@ impl<'a> CheckAttrVisitor<'a> {
"i8" | "u8" | "i16" | "u16" |
"i32" | "u32" | "i64" | "u64" |
"isize" | "usize" => {
conflicting_reprs += 1;
if target != Target::Enum {
"attribute should be applied to enum"
} else {
Expand All @@ -87,6 +100,10 @@ impl<'a> CheckAttrVisitor<'a> {

span_err!(self.sess, attr.span, E0517, "{}", message);
}
if conflicting_reprs > 1 {
span_warn!(self.sess, attr.span, E0566,
"conflicting representation hints");
}
}

fn check_attribute(&self, attr: &ast::Attribute, target: Target) {
Expand Down
5 changes: 2 additions & 3 deletions src/libsyntax/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -904,9 +904,8 @@ pub fn find_repr_attrs(diagnostic: &Handler, attr: &Attribute) -> Vec<ReprAttr>
}
};

match hint {
Some(h) => acc.push(h),
None => { }
if let Some(h) = hint {
acc.push(h);
}
} else {
span_err!(diagnostic, item.span, E0553,
Expand Down
30 changes: 30 additions & 0 deletions src/test/compile-fail/conflicting-repr-hints.rs
Original file line number Diff line number Diff line change
@@ -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(rustc_attrs)]
#![allow(dead_code)]

#[repr(C)]
enum A { A }

#[repr(u64)]
enum B { B }

#[repr(C, u64)] //~ WARNING conflicting representation hints
enum C { C }

#[repr(u32, u64)] //~ WARNING conflicting representation hints
enum D { D }

#[repr(C, packed)]
struct E(i32);

#[rustc_error]
fn main() {} //~ ERROR compilation successful
2 changes: 1 addition & 1 deletion src/test/run-pass/mir_adt_construction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[repr(C, u32)]
#[repr(C)]
enum CEnum {
Hello = 30,
World = 60
Expand Down

0 comments on commit 603d9cc

Please sign in to comment.