diff options
| author | Dylan DPC <dylan.dpc@gmail.com> | 2021-03-15 16:22:51 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-15 16:22:51 +0100 |
| commit | b8622f2b3bad72a3f4fd5fb6de88b55e04017c55 (patch) | |
| tree | 3bfab8a217e34363add6ca7ed2b6b86d4f0e2f68 /compiler/rustc_passes/src | |
| parent | 4eca4929ec77b34a274a3090310247a1ec485953 (diff) | |
| parent | 49431909a6a8ccb915302d57c869e86d2a576af6 (diff) | |
| download | rust-b8622f2b3bad72a3f4fd5fb6de88b55e04017c55.tar.gz rust-b8622f2b3bad72a3f4fd5fb6de88b55e04017c55.zip | |
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.
Diffstat (limited to 'compiler/rustc_passes/src')
| -rw-r--r-- | compiler/rustc_passes/src/check_attr.rs | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index c7b266f18bf..ae8883754d6 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -8,7 +8,7 @@ use rustc_middle::hir::map::Map; 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 @@ impl CheckAttrVisitor<'tcx> { 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 @@ impl CheckAttrVisitor<'tcx> { } } + 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, |
