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(meta @ MetaItem { kind: MetaItemKind::List(metas), .. }) = &meta {
281 let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
286 AttrError::MultipleItem(pprust::path_to_string(&meta.path)),
290 if let Some(v) = meta.value_str() {
294 sess.emit_err(session_diagnostics::IncorrectMetaItem { span: meta.span });
299 let meta_name = meta.name_or_empty();
301 sym::rustc_const_unstable | sym::rustc_default_body_unstable | sym::unstable => {
302 if meta_name == sym::unstable && stab.is_some() {
306 AttrError::MultipleStabilityLevels,
309 } else if meta_name == sym::rustc_const_unstable && const_stab.is_some() {
313 AttrError::MultipleStabilityLevels,
316 } else if meta_name == sym::rustc_default_body_unstable && body_stab.is_some() {
320 AttrError::MultipleStabilityLevels,
325 let mut feature = None;
326 let mut reason = None;
327 let mut issue = None;
328 let mut issue_num = None;
329 let mut is_soft = false;
330 let mut implied_by = None;
332 let Some(mi) = meta.meta_item() else {
336 AttrError::UnsupportedLiteral(UnsupportedLiteralReason::Generic, false),
340 match mi.name_or_empty() {
342 if !get(mi, &mut feature) {
347 if !get(mi, &mut reason) {
352 if !get(mi, &mut issue) {
356 // These unwraps are safe because `get` ensures the meta item
357 // is a name/value pair string literal.
358 issue_num = match issue.unwrap().as_str() {
360 issue => match issue.parse::<NonZeroU32>() {
361 Ok(num) => Some(num),
364 session_diagnostics::InvalidIssueString {
366 cause: session_diagnostics::InvalidIssueStringCause::from_int_error_kind(
367 mi.name_value_literal_span().unwrap(),
379 sess.emit_err(session_diagnostics::SoftNoArgs {
386 if !get(mi, &mut implied_by) {
394 AttrError::UnknownMetaItem(
395 pprust::path_to_string(&mi.path),
396 &["feature", "reason", "issue", "soft"],
404 match (feature, reason, issue) {
405 (Some(feature), reason, Some(_)) => {
406 if !rustc_lexer::is_ident(feature.as_str()) {
410 AttrError::NonIdentFeature,
414 let level = Unstable {
415 reason: UnstableReason::from_opt_reason(reason),
420 if sym::unstable == meta_name {
421 stab = Some((Stability { level, feature }, attr.span));
422 } else if sym::rustc_const_unstable == meta_name {
424 ConstStability { level, feature, promotable: false },
427 } else if sym::rustc_default_body_unstable == meta_name {
429 Some((DefaultBodyStability { level, feature }, attr.span));
431 unreachable!("Unknown stability attribute {meta_name}");
435 handle_errors(&sess.parse_sess, attr.span, AttrError::MissingFeature);
439 sess.emit_err(session_diagnostics::MissingIssue { span: attr.span });
444 sym::rustc_const_stable | sym::stable => {
445 if meta_name == sym::stable && stab.is_some() {
449 AttrError::MultipleStabilityLevels,
452 } else if meta_name == sym::rustc_const_stable && const_stab.is_some() {
456 AttrError::MultipleStabilityLevels,
461 let mut feature = None;
462 let mut since = None;
465 NestedMetaItem::MetaItem(mi) => match mi.name_or_empty() {
467 if !get(mi, &mut feature) {
472 if !get(mi, &mut since) {
480 AttrError::UnknownMetaItem(
481 pprust::path_to_string(&mi.path),
482 &["feature", "since"],
488 NestedMetaItem::Lit(lit) => {
492 AttrError::UnsupportedLiteral(
493 UnsupportedLiteralReason::Generic,
502 if let Some(s) = since && s.as_str() == VERSION_PLACEHOLDER {
503 since = Some(rust_version_symbol());
506 match (feature, since) {
507 (Some(feature), Some(since)) => {
508 let level = Stable { since, allowed_through_unstable_modules: false };
509 if sym::stable == meta_name {
510 stab = Some((Stability { level, feature }, attr.span));
513 ConstStability { level, feature, promotable: false },
519 handle_errors(&sess.parse_sess, attr.span, AttrError::MissingFeature);
523 handle_errors(&sess.parse_sess, attr.span, AttrError::MissingSince);
533 // Merge the const-unstable info into the stability info
535 match &mut const_stab {
536 Some((stab, _)) => stab.promotable = promotable,
537 _ => _ = sess.emit_err(session_diagnostics::RustcPromotablePairing { span: item_sp }),
541 if allowed_through_unstable_modules {
545 level: StabilityLevel::Stable { allowed_through_unstable_modules, .. },
549 )) => *allowed_through_unstable_modules = true,
551 sess.emit_err(session_diagnostics::RustcAllowedUnstablePairing { span: item_sp });
556 (stab, const_stab, body_stab)
559 pub fn find_crate_name(sess: &Session, attrs: &[Attribute]) -> Option<Symbol> {
560 sess.first_attr_value_str_by_name(attrs, sym::crate_name)
563 #[derive(Clone, Debug)]
564 pub struct Condition {
567 pub value: Option<Symbol>,
568 pub value_span: Option<Span>,
572 /// Tests if a cfg-pattern matches the cfg set
576 lint_node_id: NodeId,
577 features: Option<&Features>,
579 eval_condition(cfg, sess, features, &mut |cfg| {
580 try_gate_cfg(cfg.name, cfg.span, sess, features);
581 if let Some(names_valid) = &sess.check_config.names_valid {
582 if !names_valid.contains(&cfg.name) {
583 sess.buffer_lint_with_diagnostic(
587 "unexpected `cfg` condition name",
588 BuiltinLintDiagnostics::UnexpectedCfg((cfg.name, cfg.name_span), None),
592 if let Some(value) = cfg.value {
593 if let Some(values) = &sess.check_config.values_valid.get(&cfg.name) {
594 if !values.contains(&value) {
595 sess.buffer_lint_with_diagnostic(
599 "unexpected `cfg` condition value",
600 BuiltinLintDiagnostics::UnexpectedCfg(
601 (cfg.name, cfg.name_span),
602 cfg.value_span.map(|vs| (value, vs)),
608 sess.config.contains(&(cfg.name, cfg.value))
612 fn try_gate_cfg(name: Symbol, span: Span, sess: &ParseSess, features: Option<&Features>) {
613 let gate = find_gated_cfg(|sym| sym == name);
614 if let (Some(feats), Some(gated_cfg)) = (features, gate) {
615 gate_cfg(&gated_cfg, span, sess, feats);
619 fn gate_cfg(gated_cfg: &GatedCfg, cfg_span: Span, sess: &ParseSess, features: &Features) {
620 let (cfg, feature, has_feature) = gated_cfg;
621 if !has_feature(features) && !cfg_span.allows_unstable(*feature) {
622 let explain = format!("`cfg({})` is experimental and subject to change", cfg);
623 feature_err(sess, *feature, cfg_span, &explain).emit();
627 #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
634 fn parse_version(s: &str, allow_appendix: bool) -> Option<Version> {
635 let mut components = s.split('-');
636 let d = components.next()?;
637 if !allow_appendix && components.next().is_some() {
640 let mut digits = d.splitn(3, '.');
641 let major = digits.next()?.parse().ok()?;
642 let minor = digits.next()?.parse().ok()?;
643 let patch = digits.next().unwrap_or("0").parse().ok()?;
644 Some(Version { major, minor, patch })
647 /// Evaluate a cfg-like condition (with `any` and `all`), using `eval` to
648 /// evaluate individual items.
649 pub fn eval_condition(
652 features: Option<&Features>,
653 eval: &mut impl FnMut(Condition) -> bool,
656 ast::MetaItemKind::List(mis) if cfg.name_or_empty() == sym::version => {
657 try_gate_cfg(sym::version, cfg.span, sess, features);
658 let (min_version, span) = match &mis[..] {
659 [NestedMetaItem::Lit(MetaItemLit { kind: LitKind::Str(sym, ..), span, .. })] => {
663 NestedMetaItem::Lit(MetaItemLit { span, .. })
664 | NestedMetaItem::MetaItem(MetaItem { span, .. }),
666 sess.emit_err(session_diagnostics::ExpectedVersionLiteral { span: *span });
670 sess.emit_err(session_diagnostics::ExpectedSingleVersionLiteral {
676 let Some(min_version) = parse_version(min_version.as_str(), false) else {
677 sess.emit_warning(session_diagnostics::UnknownVersionLiteral { span: *span });
680 let rustc_version = parse_version(env!("CFG_RELEASE"), true).unwrap();
682 // See https://github.com/rust-lang/rust/issues/64796#issuecomment-640851454 for details
683 if sess.assume_incomplete_release {
684 rustc_version > min_version
686 rustc_version >= min_version
689 ast::MetaItemKind::List(mis) => {
690 for mi in mis.iter() {
691 if !mi.is_meta_item() {
695 AttrError::UnsupportedLiteral(UnsupportedLiteralReason::Generic, false),
701 // The unwraps below may look dangerous, but we've already asserted
702 // that they won't fail with the loop above.
703 match cfg.name_or_empty() {
706 // We don't use any() here, because we want to evaluate all cfg condition
707 // as eval_condition can (and does) extra checks
708 .fold(false, |res, mi| {
709 res | eval_condition(mi.meta_item().unwrap(), sess, features, eval)
713 // We don't use all() here, because we want to evaluate all cfg condition
714 // as eval_condition can (and does) extra checks
715 .fold(true, |res, mi| {
716 res & eval_condition(mi.meta_item().unwrap(), sess, features, eval)
720 sess.emit_err(session_diagnostics::ExpectedOneCfgPattern {
726 !eval_condition(mis[0].meta_item().unwrap(), sess, features, eval)
729 if let Some(features) = features && !features.cfg_target_compact {
732 sym::cfg_target_compact,
734 &"compact `cfg(target(..))` is experimental and subject to change"
738 mis.iter().fold(true, |res, mi| {
739 let mut mi = mi.meta_item().unwrap().clone();
740 if let [seg, ..] = &mut mi.path.segments[..] {
741 seg.ident.name = Symbol::intern(&format!("target_{}", seg.ident.name));
744 res & eval_condition(&mi, sess, features, eval)
748 sess.emit_err(session_diagnostics::InvalidPredicate {
750 predicate: pprust::path_to_string(&cfg.path),
756 ast::MetaItemKind::Word | MetaItemKind::NameValue(..) if cfg.path.segments.len() != 1 => {
757 sess.emit_err(session_diagnostics::CfgPredicateIdentifier { span: cfg.path.span });
760 MetaItemKind::NameValue(lit) if !lit.kind.is_str() => {
764 AttrError::UnsupportedLiteral(
765 UnsupportedLiteralReason::CfgString,
766 lit.kind.is_bytestr(),
771 ast::MetaItemKind::Word | ast::MetaItemKind::NameValue(..) => {
772 let ident = cfg.ident().expect("multi-segment cfg predicate");
775 name_span: ident.span,
776 value: cfg.value_str(),
777 value_span: cfg.name_value_literal_span(),
784 #[derive(Copy, Debug, Encodable, Decodable, Clone, HashStable_Generic)]
785 pub struct Deprecation {
786 pub since: Option<Symbol>,
787 /// The note to issue a reason.
788 pub note: Option<Symbol>,
789 /// A text snippet used to completely replace any use of the deprecated item in an expression.
791 /// This is currently unstable.
792 pub suggestion: Option<Symbol>,
794 /// Whether to treat the since attribute as being a Rust version identifier
795 /// (rather than an opaque string).
796 pub is_since_rustc_version: bool,
799 /// Finds the deprecation attribute. `None` if none exists.
800 pub fn find_deprecation(sess: &Session, attrs: &[Attribute]) -> Option<(Deprecation, Span)> {
801 find_deprecation_generic(sess, attrs.iter())
804 fn find_deprecation_generic<'a, I>(sess: &Session, attrs_iter: I) -> Option<(Deprecation, Span)>
806 I: Iterator<Item = &'a Attribute>,
808 let mut depr: Option<(Deprecation, Span)> = None;
809 let is_rustc = sess.features_untracked().staged_api;
811 'outer: for attr in attrs_iter {
812 if !attr.has_name(sym::deprecated) {
816 let Some(meta) = attr.meta() else {
819 let mut since = None;
821 let mut suggestion = None;
823 MetaItemKind::Word => {}
824 MetaItemKind::NameValue(..) => note = meta.value_str(),
825 MetaItemKind::List(list) => {
826 let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
831 AttrError::MultipleItem(pprust::path_to_string(&meta.path)),
835 if let Some(v) = meta.value_str() {
839 if let Some(lit) = meta.name_value_literal() {
843 AttrError::UnsupportedLiteral(
844 UnsupportedLiteralReason::DeprecatedString,
845 lit.kind.is_bytestr(),
849 sess.emit_err(session_diagnostics::IncorrectMetaItem2 {
860 NestedMetaItem::MetaItem(mi) => match mi.name_or_empty() {
862 if !get(mi, &mut since) {
867 if !get(mi, &mut note) {
872 if !sess.features_untracked().deprecated_suggestion {
873 sess.emit_err(session_diagnostics::DeprecatedItemSuggestion {
875 is_nightly: sess.is_nightly_build().then_some(()),
880 if !get(mi, &mut suggestion) {
888 AttrError::UnknownMetaItem(
889 pprust::path_to_string(&mi.path),
890 if sess.features_untracked().deprecated_suggestion {
891 &["since", "note", "suggestion"]
900 NestedMetaItem::Lit(lit) => {
904 AttrError::UnsupportedLiteral(
905 UnsupportedLiteralReason::DeprecatedKvPair,
918 handle_errors(&sess.parse_sess, attr.span, AttrError::MissingSince);
923 sess.emit_err(session_diagnostics::MissingNote { span: attr.span });
929 Deprecation { since, note, suggestion, is_since_rustc_version: is_rustc },
937 #[derive(PartialEq, Debug, Encodable, Decodable, Copy, Clone)]
947 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
948 #[derive(Encodable, Decodable, HashStable_Generic)]
950 SignedInt(ast::IntTy),
951 UnsignedInt(ast::UintTy),
956 pub fn is_signed(self) -> bool {
960 SignedInt(..) => true,
961 UnsignedInt(..) => false,
966 /// Parse #[repr(...)] forms.
968 /// Valid repr contents: any of the primitive integral type names (see
969 /// `int_type_of_word`, below) to specify enum discriminant type; `C`, to use
970 /// the same discriminant size that the corresponding C enum would or C
971 /// structure layout, `packed` to remove padding, and `transparent` to delegate representation
972 /// concerns to the only non-ZST field.
973 pub fn find_repr_attrs(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
974 if attr.has_name(sym::repr) { parse_repr_attr(sess, attr) } else { Vec::new() }
977 pub fn parse_repr_attr(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
978 assert!(attr.has_name(sym::repr), "expected `#[repr(..)]`, found: {:?}", attr);
980 let mut acc = Vec::new();
981 let diagnostic = &sess.parse_sess.span_diagnostic;
983 if let Some(items) = attr.meta_item_list() {
985 let mut recognised = false;
987 let hint = match item.name_or_empty() {
988 sym::C => Some(ReprC),
989 sym::packed => Some(ReprPacked(1)),
990 sym::simd => Some(ReprSimd),
991 sym::transparent => Some(ReprTransparent),
993 sess.emit_err(session_diagnostics::InvalidReprAlignNeedArg {
999 name => int_type_of_word(name).map(ReprInt),
1002 if let Some(h) = hint {
1006 } else if let Some((name, value)) = item.name_value_literal() {
1007 let mut literal_error = None;
1008 if name == sym::align {
1010 match parse_alignment(&value.kind) {
1011 Ok(literal) => acc.push(ReprAlign(literal)),
1012 Err(message) => literal_error = Some(message),
1014 } else if name == sym::packed {
1016 match parse_alignment(&value.kind) {
1017 Ok(literal) => acc.push(ReprPacked(literal)),
1018 Err(message) => literal_error = Some(message),
1020 } else if matches!(name, sym::C | sym::simd | sym::transparent)
1021 || int_type_of_word(name).is_some()
1024 sess.emit_err(session_diagnostics::InvalidReprHintNoParen {
1026 name: name.to_ident_string(),
1029 if let Some(literal_error) = literal_error {
1030 sess.emit_err(session_diagnostics::InvalidReprGeneric {
1032 repr_arg: name.to_ident_string(),
1033 error_part: literal_error,
1036 } else if let Some(meta_item) = item.meta_item() {
1037 match &meta_item.kind {
1038 MetaItemKind::NameValue(value) => {
1039 if meta_item.has_name(sym::align) || meta_item.has_name(sym::packed) {
1040 let name = meta_item.name_or_empty().to_ident_string();
1042 sess.emit_err(session_diagnostics::IncorrectReprFormatGeneric {
1045 cause: IncorrectReprFormatGenericCause::from_lit_kind(
1052 meta_item.name_or_empty(),
1053 sym::C | sym::simd | sym::transparent
1054 ) || int_type_of_word(meta_item.name_or_empty()).is_some()
1057 sess.emit_err(session_diagnostics::InvalidReprHintNoValue {
1058 span: meta_item.span,
1059 name: meta_item.name_or_empty().to_ident_string(),
1063 MetaItemKind::List(_) => {
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) {
1072 session_diagnostics::IncorrectReprFormatPackedOneOrZeroArg {
1073 span: meta_item.span,
1077 meta_item.name_or_empty(),
1078 sym::C | sym::simd | sym::transparent
1079 ) || int_type_of_word(meta_item.name_or_empty()).is_some()
1082 sess.emit_err(session_diagnostics::InvalidReprHintNoParen {
1083 span: meta_item.span,
1084 name: meta_item.name_or_empty().to_ident_string(),
1092 // Not a word we recognize. This will be caught and reported by
1093 // the `check_mod_attrs` pass, but this pass doesn't always run
1094 // (e.g. if we only pretty-print the source), so we have to gate
1095 // the `delay_span_bug` call as follows:
1096 if sess.opts.pretty.map_or(true, |pp| pp.needs_analysis()) {
1097 diagnostic.delay_span_bug(item.span(), "unrecognized representation hint");
1105 fn int_type_of_word(s: Symbol) -> Option<IntType> {
1109 sym::i8 => Some(SignedInt(ast::IntTy::I8)),
1110 sym::u8 => Some(UnsignedInt(ast::UintTy::U8)),
1111 sym::i16 => Some(SignedInt(ast::IntTy::I16)),
1112 sym::u16 => Some(UnsignedInt(ast::UintTy::U16)),
1113 sym::i32 => Some(SignedInt(ast::IntTy::I32)),
1114 sym::u32 => Some(UnsignedInt(ast::UintTy::U32)),
1115 sym::i64 => Some(SignedInt(ast::IntTy::I64)),
1116 sym::u64 => Some(UnsignedInt(ast::UintTy::U64)),
1117 sym::i128 => Some(SignedInt(ast::IntTy::I128)),
1118 sym::u128 => Some(UnsignedInt(ast::UintTy::U128)),
1119 sym::isize => Some(SignedInt(ast::IntTy::Isize)),
1120 sym::usize => Some(UnsignedInt(ast::UintTy::Usize)),
1125 pub enum TransparencyError {
1126 UnknownTransparency(Symbol, Span),
1127 MultipleTransparencyAttrs(Span, Span),
1130 pub fn find_transparency(
1131 attrs: &[Attribute],
1133 ) -> (Transparency, Option<TransparencyError>) {
1134 let mut transparency = None;
1135 let mut error = None;
1137 if attr.has_name(sym::rustc_macro_transparency) {
1138 if let Some((_, old_span)) = transparency {
1139 error = Some(TransparencyError::MultipleTransparencyAttrs(old_span, attr.span));
1141 } else if let Some(value) = attr.value_str() {
1142 transparency = Some((
1144 sym::transparent => Transparency::Transparent,
1145 sym::semitransparent => Transparency::SemiTransparent,
1146 sym::opaque => Transparency::Opaque,
1148 error = Some(TransparencyError::UnknownTransparency(value, attr.span));
1157 let fallback = if macro_rules { Transparency::SemiTransparent } else { Transparency::Opaque };
1158 (transparency.map_or(fallback, |t| t.0), error)
1161 pub fn allow_internal_unstable<'a>(
1163 attrs: &'a [Attribute],
1164 ) -> impl Iterator<Item = Symbol> + 'a {
1165 allow_unstable(sess, attrs, sym::allow_internal_unstable)
1168 pub fn rustc_allow_const_fn_unstable<'a>(
1170 attrs: &'a [Attribute],
1171 ) -> impl Iterator<Item = Symbol> + 'a {
1172 allow_unstable(sess, attrs, sym::rustc_allow_const_fn_unstable)
1175 fn allow_unstable<'a>(
1177 attrs: &'a [Attribute],
1179 ) -> impl Iterator<Item = Symbol> + 'a {
1180 let attrs = sess.filter_by_name(attrs, symbol);
1182 .filter_map(move |attr| {
1183 attr.meta_item_list().or_else(|| {
1184 sess.emit_err(session_diagnostics::ExpectsFeatureList {
1186 name: symbol.to_ident_string(),
1193 list.into_iter().filter_map(move |it| {
1194 let name = it.ident().map(|ident| ident.name);
1196 sess.emit_err(session_diagnostics::ExpectsFeatures {
1198 name: symbol.to_ident_string(),
1205 pub fn parse_alignment(node: &ast::LitKind) -> Result<u32, &'static str> {
1206 if let ast::LitKind::Int(literal, ast::LitIntType::Unsuffixed) = node {
1207 if literal.is_power_of_two() {
1208 // rustc_middle::ty::layout::Align restricts align to <= 2^29
1209 if *literal <= 1 << 29 { Ok(*literal as u32) } else { Err("larger than 2^29") }
1211 Err("not a power of two")
1214 Err("not an unsuffixed integer")