Skip to content
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

Warn about multiple conflicting #[repr] hints #34623

Merged
merged 1 commit into from
Aug 31, 2016
Merged
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
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