1 //! Parsing and validation of builtin attributes
4 use rustc_ast::{Attribute, LitKind, MetaItem, MetaItemKind, MetaItemLit, NestedMetaItem, NodeId};
5 use rustc_ast_pretty::pprust;
6 use rustc_feature::{find_gated_cfg, is_builtin_attr_name, Features, GatedCfg};
7 use rustc_macros::HashStable_Generic;
8 use rustc_session::lint::builtin::UNEXPECTED_CFGS;
9 use rustc_session::lint::BuiltinLintDiagnostics;
10 use rustc_session::parse::{feature_err, ParseSess};
11 use rustc_session::Session;
12 use rustc_span::hygiene::Transparency;
13 use rustc_span::{symbol::sym, symbol::Symbol, Span};
14 use std::num::NonZeroU32;
16 use crate::session_diagnostics::{self, IncorrectReprFormatGenericCause};
18 /// The version placeholder that recently stabilized features contain inside the
19 /// `since` field of the `#[stable]` attribute.
21 /// For more, see [this pull request](https://github.com/rust-lang/rust/pull/100591).
22 pub const VERSION_PLACEHOLDER: &str = "CURRENT_RUSTC_VERSION";
24 pub fn rust_version_symbol() -> Symbol {
25 let version = option_env!("CFG_VERSION").unwrap_or("<current>");
26 let version = version.split(' ').next().unwrap();
27 Symbol::intern(&version)
30 pub fn is_builtin_attr(attr: &Attribute) -> bool {
31 attr.is_doc_comment() || attr.ident().filter(|ident| is_builtin_attr_name(ident.name)).is_some()
36 UnknownMetaItem(String, &'static [&'static str]),
40 MultipleStabilityLevels,
41 UnsupportedLiteral(UnsupportedLiteralReason, /* is_bytestr */ bool),
44 pub(crate) enum UnsupportedLiteralReason {
51 fn handle_errors(sess: &ParseSess, span: Span, error: AttrError) {
53 AttrError::MultipleItem(item) => {
54 sess.emit_err(session_diagnostics::MultipleItem { span, item });
56 AttrError::UnknownMetaItem(item, expected) => {
57 sess.emit_err(session_diagnostics::UnknownMetaItem { span, item, expected });
59 AttrError::MissingSince => {
60 sess.emit_err(session_diagnostics::MissingSince { span });
62 AttrError::NonIdentFeature => {
63 sess.emit_err(session_diagnostics::NonIdentFeature { span });
65 AttrError::MissingFeature => {
66 sess.emit_err(session_diagnostics::MissingFeature { span });
68 AttrError::MultipleStabilityLevels => {
69 sess.emit_err(session_diagnostics::MultipleStabilityLevels { span });
71 AttrError::UnsupportedLiteral(reason, is_bytestr) => {
72 sess.emit_err(session_diagnostics::UnsupportedLiteral {
76 start_point_span: sess.source_map().start_point(span),
82 #[derive(Copy, Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
90 #[derive(Clone, Encodable, Decodable, Debug, PartialEq, Eq, HashStable_Generic)]
91 pub enum InstructionSetAttr {
96 #[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
97 pub enum OptimizeAttr {
103 /// Represents the following attributes:
107 #[derive(Encodable, Decodable, Copy, Clone, Debug, PartialEq, Eq, Hash)]
108 #[derive(HashStable_Generic)]
109 pub struct Stability {
110 pub level: StabilityLevel,
115 pub fn is_unstable(&self) -> bool {
116 self.level.is_unstable()
119 pub fn is_stable(&self) -> bool {
120 self.level.is_stable()
124 /// Represents the `#[rustc_const_unstable]` and `#[rustc_const_stable]` attributes.
125 #[derive(Encodable, Decodable, Copy, Clone, Debug, PartialEq, Eq, Hash)]
126 #[derive(HashStable_Generic)]
127 pub struct ConstStability {
128 pub level: StabilityLevel,
130 /// whether the function has a `#[rustc_promotable]` attribute
131 pub promotable: bool,
134 impl ConstStability {
135 pub fn is_const_unstable(&self) -> bool {
136 self.level.is_unstable()
139 pub fn is_const_stable(&self) -> bool {
140 self.level.is_stable()
144 /// Represents the `#[rustc_default_body_unstable]` attribute.
145 #[derive(Encodable, Decodable, Copy, Clone, Debug, PartialEq, Eq, Hash)]
146 #[derive(HashStable_Generic)]
147 pub struct DefaultBodyStability {
148 pub level: StabilityLevel,
152 /// The available stability levels.
153 #[derive(Encodable, Decodable, PartialEq, Copy, Clone, Debug, Eq, Hash)]
154 #[derive(HashStable_Generic)]
155 pub enum StabilityLevel {
158 /// Reason for the current stability level.
159 reason: UnstableReason,
160 /// Relevant `rust-lang/rust` issue.
161 issue: Option<NonZeroU32>,
163 /// If part of a feature is stabilized and a new feature is added for the remaining parts,
164 /// then the `implied_by` attribute is used to indicate which now-stable feature previously
165 /// contained a item.
168 /// #[unstable(feature = "foo", issue = "...")]
170 /// #[unstable(feature = "foo", issue = "...")]
177 /// #[stable(feature = "foo", since = "1.XX.X")]
179 /// #[unstable(feature = "foobar", issue = "...", implied_by = "foo")]
182 implied_by: Option<Symbol>,
186 /// Rust release which stabilized this feature.
188 /// Is this item allowed to be referred to on stable, despite being contained in unstable
190 allowed_through_unstable_modules: bool,
194 impl StabilityLevel {
195 pub fn is_unstable(&self) -> bool {
196 matches!(self, StabilityLevel::Unstable { .. })
198 pub fn is_stable(&self) -> bool {
199 matches!(self, StabilityLevel::Stable { .. })
203 #[derive(Encodable, Decodable, PartialEq, Copy, Clone, Debug, Eq, Hash)]
204 #[derive(HashStable_Generic)]
205 pub enum UnstableReason {
211 impl UnstableReason {
212 fn from_opt_reason(reason: Option<Symbol>) -> Self {
213 // UnstableReason::Default constructed manually
215 Some(r) => Self::Some(r),
220 pub fn to_opt_reason(&self) -> Option<Symbol> {
223 Self::Default => Some(sym::unstable_location_reason_default),
224 Self::Some(r) => Some(*r),
229 /// Collects stability info from all stability attributes in `attrs`.
230 /// Returns `None` if no stability attributes are found.
231 pub fn find_stability(
235 ) -> (Option<(Stability, Span)>, Option<(ConstStability, Span)>, Option<(DefaultBodyStability, Span)>)
237 find_stability_generic(sess, attrs.iter(), item_sp)
240 fn find_stability_generic<'a, I>(
244 ) -> (Option<(Stability, Span)>, Option<(ConstStability, Span)>, Option<(DefaultBodyStability, Span)>)
246 I: Iterator<Item = &'a Attribute>,
248 use StabilityLevel::*;
250 let mut stab: Option<(Stability, Span)> = None;
251 let mut const_stab: Option<(ConstStability, Span)> = None;
252 let mut body_stab: Option<(DefaultBodyStability, Span)> = None;
253 let mut promotable = false;
254 let mut allowed_through_unstable_modules = false;
256 'outer: for attr in attrs_iter {
258 sym::rustc_const_unstable,
259 sym::rustc_const_stable,
262 sym::rustc_promotable,
263 sym::rustc_allowed_through_unstable_modules,
264 sym::rustc_default_body_unstable,
267 .any(|&s| attr.has_name(s))
269 continue; // not a stability level
272 let meta = attr.meta();
274 if attr.has_name(sym::rustc_promotable) {
276 } else if attr.has_name(sym::rustc_allowed_through_unstable_modules) {
277 allowed_through_unstable_modules = true;
279 // attributes with data
280 else if let Some(MetaItem { kind: MetaItemKind::List(ref metas), .. }) = meta {
281 let meta = meta.as_ref().unwrap();
282 let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
287 AttrError::MultipleItem(pprust::path_to_string(&meta.path)),
291 if let Some(v) = meta.value_str() {
295 sess.emit_err(session_diagnostics::IncorrectMetaItem { span: meta.span });
300 let meta_name = meta.name_or_empty();
302 sym::rustc_const_unstable | sym::rustc_default_body_unstable | sym::unstable => {
303 if meta_name == sym::unstable && stab.is_some() {
307 AttrError::MultipleStabilityLevels,
310 } else if meta_name == sym::rustc_const_unstable && const_stab.is_some() {
314 AttrError::MultipleStabilityLevels,
317 } else if meta_name == sym::rustc_default_body_unstable && body_stab.is_some() {
321 AttrError::MultipleStabilityLevels,
326 let mut feature = None;
327 let mut reason = None;
328 let mut issue = None;
329 let mut issue_num = None;
330 let mut is_soft = false;
331 let mut implied_by = None;
333 let Some(mi) = meta.meta_item() else {
337 AttrError::UnsupportedLiteral(UnsupportedLiteralReason::Generic, false),
341 match mi.name_or_empty() {
343 if !get(mi, &mut feature) {
348 if !get(mi, &mut reason) {
353 if !get(mi, &mut issue) {
357 // These unwraps are safe because `get` ensures the meta item
358 // is a name/value pair string literal.
359 issue_num = match issue.unwrap().as_str() {
361 issue => match issue.parse::<NonZeroU32>() {
362 Ok(num) => Some(num),
365 session_diagnostics::InvalidIssueString {
367 cause: session_diagnostics::InvalidIssueStringCause::from_int_error_kind(
368 mi.name_value_literal_span().unwrap(),
380 sess.emit_err(session_diagnostics::SoftNoArgs {
387 if !get(mi, &mut implied_by) {
395 AttrError::UnknownMetaItem(
396 pprust::path_to_string(&mi.path),
397 &["feature", "reason", "issue", "soft"],
405 match (feature, reason, issue) {
406 (Some(feature), reason, Some(_)) => {
407 if !rustc_lexer::is_ident(feature.as_str()) {
411 AttrError::NonIdentFeature,
415 let level = Unstable {
416 reason: UnstableReason::from_opt_reason(reason),
421 if sym::unstable == meta_name {
422 stab = Some((Stability { level, feature }, attr.span));
423 } else if sym::rustc_const_unstable == meta_name {
425 ConstStability { level, feature, promotable: false },
428 } else if sym::rustc_default_body_unstable == meta_name {
430 Some((DefaultBodyStability { level, feature }, attr.span));
432 unreachable!("Unknown stability attribute {meta_name}");
436 handle_errors(&sess.parse_sess, attr.span, AttrError::MissingFeature);
440 sess.emit_err(session_diagnostics::MissingIssue { span: attr.span });
445 sym::rustc_const_stable | sym::stable => {
446 if meta_name == sym::stable && stab.is_some() {
450 AttrError::MultipleStabilityLevels,
453 } else if meta_name == sym::rustc_const_stable && const_stab.is_some() {
457 AttrError::MultipleStabilityLevels,
462 let mut feature = None;
463 let mut since = None;
466 NestedMetaItem::MetaItem(mi) => match mi.name_or_empty() {
468 if !get(mi, &mut feature) {
473 if !get(mi, &mut since) {
481 AttrError::UnknownMetaItem(
482 pprust::path_to_string(&mi.path),
483 &["feature", "since"],
489 NestedMetaItem::Lit(lit) => {
493 AttrError::UnsupportedLiteral(
494 UnsupportedLiteralReason::Generic,
503 if let Some(s) = since && s.as_str() == VERSION_PLACEHOLDER {
504 since = Some(rust_version_symbol());
507 match (feature, since) {
508 (Some(feature), Some(since)) => {
509 let level = Stable { since, allowed_through_unstable_modules: false };
510 if sym::stable == meta_name {
511 stab = Some((Stability { level, feature }, attr.span));
514 ConstStability { level, feature, promotable: false },
520 handle_errors(&sess.parse_sess, attr.span, AttrError::MissingFeature);
524 handle_errors(&sess.parse_sess, attr.span, AttrError::MissingSince);
534 // Merge the const-unstable info into the stability info
536 if let Some((ref mut stab, _)) = const_stab {
537 stab.promotable = promotable;
539 sess.emit_err(session_diagnostics::RustcPromotablePairing { span: item_sp });
543 if allowed_through_unstable_modules {
546 level: StabilityLevel::Stable { ref mut allowed_through_unstable_modules, .. },
552 *allowed_through_unstable_modules = true;
554 sess.emit_err(session_diagnostics::RustcAllowedUnstablePairing { span: item_sp });
558 (stab, const_stab, body_stab)
561 pub fn find_crate_name(sess: &Session, attrs: &[Attribute]) -> Option<Symbol> {
562 sess.first_attr_value_str_by_name(attrs, sym::crate_name)
565 #[derive(Clone, Debug)]
566 pub struct Condition {
569 pub value: Option<Symbol>,
570 pub value_span: Option<Span>,
574 /// Tests if a cfg-pattern matches the cfg set
578 lint_node_id: NodeId,
579 features: Option<&Features>,
581 eval_condition(cfg, sess, features, &mut |cfg| {
582 try_gate_cfg(cfg.name, cfg.span, sess, features);
583 if let Some(names_valid) = &sess.check_config.names_valid {
584 if !names_valid.contains(&cfg.name) {
585 sess.buffer_lint_with_diagnostic(
589 "unexpected `cfg` condition name",
590 BuiltinLintDiagnostics::UnexpectedCfg((cfg.name, cfg.name_span), None),
594 if let Some(value) = cfg.value {
595 if let Some(values) = &sess.check_config.values_valid.get(&cfg.name) {
596 if !values.contains(&value) {
597 sess.buffer_lint_with_diagnostic(
601 "unexpected `cfg` condition value",
602 BuiltinLintDiagnostics::UnexpectedCfg(
603 (cfg.name, cfg.name_span),
604 cfg.value_span.map(|vs| (value, vs)),
610 sess.config.contains(&(cfg.name, cfg.value))
614 fn try_gate_cfg(name: Symbol, span: Span, sess: &ParseSess, features: Option<&Features>) {
615 let gate = find_gated_cfg(|sym| sym == name);
616 if let (Some(feats), Some(gated_cfg)) = (features, gate) {
617 gate_cfg(&gated_cfg, span, sess, feats);
621 fn gate_cfg(gated_cfg: &GatedCfg, cfg_span: Span, sess: &ParseSess, features: &Features) {
622 let (cfg, feature, has_feature) = gated_cfg;
623 if !has_feature(features) && !cfg_span.allows_unstable(*feature) {
624 let explain = format!("`cfg({})` is experimental and subject to change", cfg);
625 feature_err(sess, *feature, cfg_span, &explain).emit();
629 #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
636 fn parse_version(s: &str, allow_appendix: bool) -> Option<Version> {
637 let mut components = s.split('-');
638 let d = components.next()?;
639 if !allow_appendix && components.next().is_some() {
642 let mut digits = d.splitn(3, '.');
643 let major = digits.next()?.parse().ok()?;
644 let minor = digits.next()?.parse().ok()?;
645 let patch = digits.next().unwrap_or("0").parse().ok()?;
646 Some(Version { major, minor, patch })
649 /// Evaluate a cfg-like condition (with `any` and `all`), using `eval` to
650 /// evaluate individual items.
651 pub fn eval_condition(
654 features: Option<&Features>,
655 eval: &mut impl FnMut(Condition) -> bool,
658 ast::MetaItemKind::List(ref mis) if cfg.name_or_empty() == sym::version => {
659 try_gate_cfg(sym::version, cfg.span, sess, features);
660 let (min_version, span) = match &mis[..] {
661 [NestedMetaItem::Lit(MetaItemLit { kind: LitKind::Str(sym, ..), span, .. })] => {
665 NestedMetaItem::Lit(MetaItemLit { span, .. })
666 | NestedMetaItem::MetaItem(MetaItem { span, .. }),
668 sess.emit_err(session_diagnostics::ExpectedVersionLiteral { span: *span });
672 sess.emit_err(session_diagnostics::ExpectedSingleVersionLiteral {
678 let Some(min_version) = parse_version(min_version.as_str(), false) else {
679 sess.emit_warning(session_diagnostics::UnknownVersionLiteral { span: *span });
682 let rustc_version = parse_version(env!("CFG_RELEASE"), true).unwrap();
684 // See https://github.com/rust-lang/rust/issues/64796#issuecomment-640851454 for details
685 if sess.assume_incomplete_release {
686 rustc_version > min_version
688 rustc_version >= min_version
691 ast::MetaItemKind::List(ref mis) => {
692 for mi in mis.iter() {
693 if !mi.is_meta_item() {
697 AttrError::UnsupportedLiteral(UnsupportedLiteralReason::Generic, false),
703 // The unwraps below may look dangerous, but we've already asserted
704 // that they won't fail with the loop above.
705 match cfg.name_or_empty() {
708 // We don't use any() here, because we want to evaluate all cfg condition
709 // as eval_condition can (and does) extra checks
710 .fold(false, |res, mi| {
711 res | eval_condition(mi.meta_item().unwrap(), sess, features, eval)
715 // We don't use all() here, because we want to evaluate all cfg condition
716 // as eval_condition can (and does) extra checks
717 .fold(true, |res, mi| {
718 res & eval_condition(mi.meta_item().unwrap(), sess, features, eval)
722 sess.emit_err(session_diagnostics::ExpectedOneCfgPattern {
728 !eval_condition(mis[0].meta_item().unwrap(), sess, features, eval)
731 if let Some(features) = features && !features.cfg_target_compact {
734 sym::cfg_target_compact,
736 &"compact `cfg(target(..))` is experimental and subject to change"
740 mis.iter().fold(true, |res, mi| {
741 let mut mi = mi.meta_item().unwrap().clone();
742 if let [seg, ..] = &mut mi.path.segments[..] {
743 seg.ident.name = Symbol::intern(&format!("target_{}", seg.ident.name));
746 res & eval_condition(&mi, sess, features, eval)
750 sess.emit_err(session_diagnostics::InvalidPredicate {
752 predicate: pprust::path_to_string(&cfg.path),
758 ast::MetaItemKind::Word | MetaItemKind::NameValue(..) if cfg.path.segments.len() != 1 => {
759 sess.emit_err(session_diagnostics::CfgPredicateIdentifier { span: cfg.path.span });
762 MetaItemKind::NameValue(ref lit) if !lit.kind.is_str() => {
766 AttrError::UnsupportedLiteral(
767 UnsupportedLiteralReason::CfgString,
768 lit.kind.is_bytestr(),
773 ast::MetaItemKind::Word | ast::MetaItemKind::NameValue(..) => {
774 let ident = cfg.ident().expect("multi-segment cfg predicate");
777 name_span: ident.span,
778 value: cfg.value_str(),
779 value_span: cfg.name_value_literal_span(),
786 #[derive(Copy, Debug, Encodable, Decodable, Clone, HashStable_Generic)]
787 pub struct Deprecation {
788 pub since: Option<Symbol>,
789 /// The note to issue a reason.
790 pub note: Option<Symbol>,
791 /// A text snippet used to completely replace any use of the deprecated item in an expression.
793 /// This is currently unstable.
794 pub suggestion: Option<Symbol>,
796 /// Whether to treat the since attribute as being a Rust version identifier
797 /// (rather than an opaque string).
798 pub is_since_rustc_version: bool,
801 /// Finds the deprecation attribute. `None` if none exists.
802 pub fn find_deprecation(sess: &Session, attrs: &[Attribute]) -> Option<(Deprecation, Span)> {
803 find_deprecation_generic(sess, attrs.iter())
806 fn find_deprecation_generic<'a, I>(sess: &Session, attrs_iter: I) -> Option<(Deprecation, Span)>
808 I: Iterator<Item = &'a Attribute>,
810 let mut depr: Option<(Deprecation, Span)> = None;
811 let is_rustc = sess.features_untracked().staged_api;
813 'outer: for attr in attrs_iter {
814 if !attr.has_name(sym::deprecated) {
818 let Some(meta) = attr.meta() else {
821 let mut since = None;
823 let mut suggestion = None;
825 MetaItemKind::Word => {}
826 MetaItemKind::NameValue(..) => note = meta.value_str(),
827 MetaItemKind::List(list) => {
828 let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
833 AttrError::MultipleItem(pprust::path_to_string(&meta.path)),
837 if let Some(v) = meta.value_str() {
841 if let Some(lit) = meta.name_value_literal() {
845 AttrError::UnsupportedLiteral(
846 UnsupportedLiteralReason::DeprecatedString,
847 lit.kind.is_bytestr(),
851 sess.emit_err(session_diagnostics::IncorrectMetaItem2 {
862 NestedMetaItem::MetaItem(mi) => match mi.name_or_empty() {
864 if !get(mi, &mut since) {
869 if !get(mi, &mut note) {
874 if !sess.features_untracked().deprecated_suggestion {
875 sess.emit_err(session_diagnostics::DeprecatedItemSuggestion {
877 is_nightly: sess.is_nightly_build().then_some(()),
882 if !get(mi, &mut suggestion) {
890 AttrError::UnknownMetaItem(
891 pprust::path_to_string(&mi.path),
892 if sess.features_untracked().deprecated_suggestion {
893 &["since", "note", "suggestion"]
902 NestedMetaItem::Lit(lit) => {
906 AttrError::UnsupportedLiteral(
907 UnsupportedLiteralReason::DeprecatedKvPair,
920 handle_errors(&sess.parse_sess, attr.span, AttrError::MissingSince);
925 sess.emit_err(session_diagnostics::MissingNote { span: attr.span });
931 Deprecation { since, note, suggestion, is_since_rustc_version: is_rustc },
939 #[derive(PartialEq, Debug, Encodable, Decodable, Copy, Clone)]
949 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
950 #[derive(Encodable, Decodable, HashStable_Generic)]
952 SignedInt(ast::IntTy),
953 UnsignedInt(ast::UintTy),
958 pub fn is_signed(self) -> bool {
962 SignedInt(..) => true,
963 UnsignedInt(..) => false,
968 /// Parse #[repr(...)] forms.
970 /// Valid repr contents: any of the primitive integral type names (see
971 /// `int_type_of_word`, below) to specify enum discriminant type; `C`, to use
972 /// the same discriminant size that the corresponding C enum would or C
973 /// structure layout, `packed` to remove padding, and `transparent` to delegate representation
974 /// concerns to the only non-ZST field.
975 pub fn find_repr_attrs(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
976 if attr.has_name(sym::repr) { parse_repr_attr(sess, attr) } else { Vec::new() }
979 pub fn parse_repr_attr(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
980 assert!(attr.has_name(sym::repr), "expected `#[repr(..)]`, found: {:?}", attr);
982 let mut acc = Vec::new();
983 let diagnostic = &sess.parse_sess.span_diagnostic;
985 if let Some(items) = attr.meta_item_list() {
987 let mut recognised = false;
989 let hint = match item.name_or_empty() {
990 sym::C => Some(ReprC),
991 sym::packed => Some(ReprPacked(1)),
992 sym::simd => Some(ReprSimd),
993 sym::transparent => Some(ReprTransparent),
995 sess.emit_err(session_diagnostics::InvalidReprAlignNeedArg {
1001 name => int_type_of_word(name).map(ReprInt),
1004 if let Some(h) = hint {
1008 } else if let Some((name, value)) = item.name_value_literal() {
1009 let mut literal_error = None;
1010 if name == sym::align {
1012 match parse_alignment(&value.kind) {
1013 Ok(literal) => acc.push(ReprAlign(literal)),
1014 Err(message) => literal_error = Some(message),
1016 } else if name == sym::packed {
1018 match parse_alignment(&value.kind) {
1019 Ok(literal) => acc.push(ReprPacked(literal)),
1020 Err(message) => literal_error = Some(message),
1022 } else if matches!(name, sym::C | sym::simd | sym::transparent)
1023 || int_type_of_word(name).is_some()
1026 sess.emit_err(session_diagnostics::InvalidReprHintNoParen {
1028 name: name.to_ident_string(),
1031 if let Some(literal_error) = literal_error {
1032 sess.emit_err(session_diagnostics::InvalidReprGeneric {
1034 repr_arg: name.to_ident_string(),
1035 error_part: literal_error,
1038 } else if let Some(meta_item) = item.meta_item() {
1039 if let MetaItemKind::NameValue(ref value) = meta_item.kind {
1040 if meta_item.has_name(sym::align) || meta_item.has_name(sym::packed) {
1041 let name = meta_item.name_or_empty().to_ident_string();
1043 sess.emit_err(session_diagnostics::IncorrectReprFormatGeneric {
1046 cause: IncorrectReprFormatGenericCause::from_lit_kind(
1053 meta_item.name_or_empty(),
1054 sym::C | sym::simd | sym::transparent
1055 ) || int_type_of_word(meta_item.name_or_empty()).is_some()
1058 sess.emit_err(session_diagnostics::InvalidReprHintNoValue {
1059 span: meta_item.span,
1060 name: meta_item.name_or_empty().to_ident_string(),
1063 } else if let MetaItemKind::List(_) = meta_item.kind {
1064 if meta_item.has_name(sym::align) {
1066 sess.emit_err(session_diagnostics::IncorrectReprFormatAlignOneArg {
1067 span: meta_item.span,
1069 } else if meta_item.has_name(sym::packed) {
1071 sess.emit_err(session_diagnostics::IncorrectReprFormatPackedOneOrZeroArg {
1072 span: meta_item.span,
1075 meta_item.name_or_empty(),
1076 sym::C | sym::simd | sym::transparent
1077 ) || int_type_of_word(meta_item.name_or_empty()).is_some()
1080 sess.emit_err(session_diagnostics::InvalidReprHintNoParen {
1081 span: meta_item.span,
1082 name: meta_item.name_or_empty().to_ident_string(),
1088 // Not a word we recognize. This will be caught and reported by
1089 // the `check_mod_attrs` pass, but this pass doesn't always run
1090 // (e.g. if we only pretty-print the source), so we have to gate
1091 // the `delay_span_bug` call as follows:
1092 if sess.opts.pretty.map_or(true, |pp| pp.needs_analysis()) {
1093 diagnostic.delay_span_bug(item.span(), "unrecognized representation hint");
1101 fn int_type_of_word(s: Symbol) -> Option<IntType> {
1105 sym::i8 => Some(SignedInt(ast::IntTy::I8)),
1106 sym::u8 => Some(UnsignedInt(ast::UintTy::U8)),
1107 sym::i16 => Some(SignedInt(ast::IntTy::I16)),
1108 sym::u16 => Some(UnsignedInt(ast::UintTy::U16)),
1109 sym::i32 => Some(SignedInt(ast::IntTy::I32)),
1110 sym::u32 => Some(UnsignedInt(ast::UintTy::U32)),
1111 sym::i64 => Some(SignedInt(ast::IntTy::I64)),
1112 sym::u64 => Some(UnsignedInt(ast::UintTy::U64)),
1113 sym::i128 => Some(SignedInt(ast::IntTy::I128)),
1114 sym::u128 => Some(UnsignedInt(ast::UintTy::U128)),
1115 sym::isize => Some(SignedInt(ast::IntTy::Isize)),
1116 sym::usize => Some(UnsignedInt(ast::UintTy::Usize)),
1121 pub enum TransparencyError {
1122 UnknownTransparency(Symbol, Span),
1123 MultipleTransparencyAttrs(Span, Span),
1126 pub fn find_transparency(
1127 attrs: &[Attribute],
1129 ) -> (Transparency, Option<TransparencyError>) {
1130 let mut transparency = None;
1131 let mut error = None;
1133 if attr.has_name(sym::rustc_macro_transparency) {
1134 if let Some((_, old_span)) = transparency {
1135 error = Some(TransparencyError::MultipleTransparencyAttrs(old_span, attr.span));
1137 } else if let Some(value) = attr.value_str() {
1138 transparency = Some((
1140 sym::transparent => Transparency::Transparent,
1141 sym::semitransparent => Transparency::SemiTransparent,
1142 sym::opaque => Transparency::Opaque,
1144 error = Some(TransparencyError::UnknownTransparency(value, attr.span));
1153 let fallback = if macro_rules { Transparency::SemiTransparent } else { Transparency::Opaque };
1154 (transparency.map_or(fallback, |t| t.0), error)
1157 pub fn allow_internal_unstable<'a>(
1159 attrs: &'a [Attribute],
1160 ) -> impl Iterator<Item = Symbol> + 'a {
1161 allow_unstable(sess, attrs, sym::allow_internal_unstable)
1164 pub fn rustc_allow_const_fn_unstable<'a>(
1166 attrs: &'a [Attribute],
1167 ) -> impl Iterator<Item = Symbol> + 'a {
1168 allow_unstable(sess, attrs, sym::rustc_allow_const_fn_unstable)
1171 fn allow_unstable<'a>(
1173 attrs: &'a [Attribute],
1175 ) -> impl Iterator<Item = Symbol> + 'a {
1176 let attrs = sess.filter_by_name(attrs, symbol);
1178 .filter_map(move |attr| {
1179 attr.meta_item_list().or_else(|| {
1180 sess.emit_err(session_diagnostics::ExpectsFeatureList {
1182 name: symbol.to_ident_string(),
1189 list.into_iter().filter_map(move |it| {
1190 let name = it.ident().map(|ident| ident.name);
1192 sess.emit_err(session_diagnostics::ExpectsFeatures {
1194 name: symbol.to_ident_string(),
1201 pub fn parse_alignment(node: &ast::LitKind) -> Result<u32, &'static str> {
1202 if let ast::LitKind::Int(literal, ast::LitIntType::Unsuffixed) = node {
1203 if literal.is_power_of_two() {
1204 // rustc_middle::ty::layout::Align restricts align to <= 2^29
1205 if *literal <= 1 << 29 { Ok(*literal as u32) } else { Err("larger than 2^29") }
1207 Err("not a power of two")
1210 Err("not an unsuffixed integer")