Auto merge of #34623 - lfairy:repr-conflict, r=Aatch

Warn about multiple conflicting #[repr] hints

Closes #34622
This commit is contained in:
bors
2016-08-31 00:40:42 -07:00
committed by GitHub
5 changed files with 52 additions and 5 deletions

View File

@@ -1804,4 +1804,5 @@ register_diagnostics! {
E0490, // a value of type `..` is borrowed for too long E0490, // a value of type `..` is borrowed for too long
E0491, // in type `..`, reference has a longer lifetime than the data it... E0491, // in type `..`, reference has a longer lifetime than the data it...
E0495, // cannot infer an appropriate lifetime due to conflicting requirements E0495, // cannot infer an appropriate lifetime due to conflicting requirements
E0566 // conflicting representation hints
} }

View File

@@ -52,6 +52,7 @@ impl<'a> CheckAttrVisitor<'a> {
} }
}; };
let mut conflicting_reprs = 0;
for word in words { for word in words {
let name = match word.name() { let name = match word.name() {
Some(word) => word, Some(word) => word,
@@ -60,13 +61,24 @@ impl<'a> CheckAttrVisitor<'a> {
let message = match &*name { let message = match &*name {
"C" => { "C" => {
conflicting_reprs += 1;
if target != Target::Struct && target != Target::Enum { if target != Target::Struct && target != Target::Enum {
"attribute should be applied to struct or enum" "attribute should be applied to struct or enum"
} else { } else {
continue 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 { if target != Target::Struct {
"attribute should be applied to struct" "attribute should be applied to struct"
} else { } else {
@@ -76,6 +88,7 @@ impl<'a> CheckAttrVisitor<'a> {
"i8" | "u8" | "i16" | "u16" | "i8" | "u8" | "i16" | "u16" |
"i32" | "u32" | "i64" | "u64" | "i32" | "u32" | "i64" | "u64" |
"isize" | "usize" => { "isize" | "usize" => {
conflicting_reprs += 1;
if target != Target::Enum { if target != Target::Enum {
"attribute should be applied to enum" "attribute should be applied to enum"
} else { } else {
@@ -87,6 +100,10 @@ impl<'a> CheckAttrVisitor<'a> {
span_err!(self.sess, attr.span, E0517, "{}", message); 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) { fn check_attribute(&self, attr: &ast::Attribute, target: Target) {

View File

@@ -904,9 +904,8 @@ pub fn find_repr_attrs(diagnostic: &Handler, attr: &Attribute) -> Vec<ReprAttr>
} }
}; };
match hint { if let Some(h) = hint {
Some(h) => acc.push(h), acc.push(h);
None => { }
} }
} else { } else {
span_err!(diagnostic, item.span, E0553, span_err!(diagnostic, item.span, E0553,

View File

@@ -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

View File

@@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
#[repr(C, u32)] #[repr(C)]
enum CEnum { enum CEnum {
Hello = 30, Hello = 30,
World = 60 World = 60