Auto merge of #34623 - lfairy:repr-conflict, r=Aatch
Warn about multiple conflicting #[repr] hints Closes #34622
This commit is contained in:
commit
603d9ccfbe
@ -1804,4 +1804,5 @@ fn main() {
|
||||
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
|
||||
}
|
||||
|
@ -52,6 +52,7 @@ fn check_repr(&self, attr: &ast::Attribute, target: Target) {
|
||||
}
|
||||
};
|
||||
|
||||
let mut conflicting_reprs = 0;
|
||||
for word in words {
|
||||
let name = match word.name() {
|
||||
Some(word) => word,
|
||||
@ -60,13 +61,24 @@ fn check_repr(&self, attr: &ast::Attribute, target: Target) {
|
||||
|
||||
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 {
|
||||
@ -76,6 +88,7 @@ fn check_repr(&self, attr: &ast::Attribute, target: Target) {
|
||||
"i8" | "u8" | "i16" | "u16" |
|
||||
"i32" | "u32" | "i64" | "u64" |
|
||||
"isize" | "usize" => {
|
||||
conflicting_reprs += 1;
|
||||
if target != Target::Enum {
|
||||
"attribute should be applied to enum"
|
||||
} else {
|
||||
@ -87,6 +100,10 @@ fn check_repr(&self, attr: &ast::Attribute, target: Target) {
|
||||
|
||||
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) {
|
||||
|
@ -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,
|
||||
|
30
src/test/compile-fail/conflicting-repr-hints.rs
Normal file
30
src/test/compile-fail/conflicting-repr-hints.rs
Normal 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
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user