Rollup merge of #83054 - tmiasko:rustc_layout_scalar_valid_range, r=davidtwco

Validate rustc_layout_scalar_valid_range_{start,end} attributes

Fixes #82251, fixes #82981.
This commit is contained in:
Dylan DPC 2021-03-15 16:22:51 +01:00 committed by GitHub
commit b8622f2b3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 91 additions and 1 deletions

View File

@ -120,6 +120,7 @@ pub fn name_value_literal_span(&self) -> Option<Span> {
}
impl Attribute {
#[inline]
pub fn has_name(&self, name: Symbol) -> bool {
match self.kind {
AttrKind::Normal(ref item, _) => item.path == name,

View File

@ -8,7 +8,7 @@
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::TyCtxt;
use rustc_ast::{Attribute, LitKind, NestedMetaItem};
use rustc_ast::{Attribute, Lit, LitKind, NestedMetaItem};
use rustc_errors::{pluralize, struct_span_err};
use rustc_hir as hir;
use rustc_hir::def_id::LocalDefId;
@ -87,6 +87,10 @@ fn check_attributes(
self.check_export_name(hir_id, &attr, span, target)
} else if self.tcx.sess.check_name(attr, sym::rustc_args_required_const) {
self.check_rustc_args_required_const(&attr, span, target, item)
} else if self.tcx.sess.check_name(attr, sym::rustc_layout_scalar_valid_range_start) {
self.check_rustc_layout_scalar_valid_range(&attr, span, target)
} else if self.tcx.sess.check_name(attr, sym::rustc_layout_scalar_valid_range_end) {
self.check_rustc_layout_scalar_valid_range(&attr, span, target)
} else if self.tcx.sess.check_name(attr, sym::allow_internal_unstable) {
self.check_allow_internal_unstable(hir_id, &attr, span, target, &attrs)
} else if self.tcx.sess.check_name(attr, sym::rustc_allow_const_fn_unstable) {
@ -807,6 +811,37 @@ fn check_rustc_args_required_const(
}
}
fn check_rustc_layout_scalar_valid_range(
&self,
attr: &Attribute,
span: &Span,
target: Target,
) -> bool {
if target != Target::Struct {
self.tcx
.sess
.struct_span_err(attr.span, "attribute should be applied to a struct")
.span_label(*span, "not a struct")
.emit();
return false;
}
let list = match attr.meta_item_list() {
None => return false,
Some(it) => it,
};
if matches!(&list[..], &[NestedMetaItem::Literal(Lit { kind: LitKind::Int(..), .. })]) {
true
} else {
self.tcx
.sess
.struct_span_err(attr.span, "expected exactly one integer literal argument")
.emit();
false
}
}
/// Checks if `#[rustc_legacy_const_generics]` is applied to a function and has a valid argument.
fn check_rustc_legacy_const_generics(
&self,

View File

@ -0,0 +1,23 @@
#![feature(rustc_attrs)]
#[rustc_layout_scalar_valid_range_start(u32::MAX)] //~ ERROR
pub struct A(u32);
#[rustc_layout_scalar_valid_range_end(1, 2)] //~ ERROR
pub struct B(u8);
#[rustc_layout_scalar_valid_range_end(a = "a")] //~ ERROR
pub struct C(i32);
#[rustc_layout_scalar_valid_range_end(1)] //~ ERROR
enum E {
X = 1,
Y = 14,
}
fn main() {
let _ = A(0);
let _ = B(0);
let _ = C(0);
let _ = E::X;
}

View File

@ -0,0 +1,31 @@
error: expected exactly one integer literal argument
--> $DIR/invalid_rustc_layout_scalar_valid_range.rs:3:1
|
LL | #[rustc_layout_scalar_valid_range_start(u32::MAX)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: expected exactly one integer literal argument
--> $DIR/invalid_rustc_layout_scalar_valid_range.rs:6:1
|
LL | #[rustc_layout_scalar_valid_range_end(1, 2)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: expected exactly one integer literal argument
--> $DIR/invalid_rustc_layout_scalar_valid_range.rs:9:1
|
LL | #[rustc_layout_scalar_valid_range_end(a = "a")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: attribute should be applied to a struct
--> $DIR/invalid_rustc_layout_scalar_valid_range.rs:12:1
|
LL | #[rustc_layout_scalar_valid_range_end(1)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | / enum E {
LL | | X = 1,
LL | | Y = 14,
LL | | }
| |_- not a struct
error: aborting due to 4 previous errors