]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_attr/src/builtin.rs
New upstream version 1.52.0~beta.3+dfsg1
[rustc.git] / compiler / rustc_attr / src / builtin.rs
1 //! Parsing and validation of builtin attributes
2
3 use rustc_ast::{self as ast, Attribute, Lit, LitKind, MetaItem, MetaItemKind, NestedMetaItem};
4 use rustc_ast_pretty::pprust;
5 use rustc_errors::{struct_span_err, Applicability};
6 use rustc_feature::{find_gated_cfg, is_builtin_attr_name, Features, GatedCfg};
7 use rustc_macros::HashStable_Generic;
8 use rustc_session::parse::{feature_err, ParseSess};
9 use rustc_session::Session;
10 use rustc_span::hygiene::Transparency;
11 use rustc_span::{symbol::sym, symbol::Symbol, Span};
12 use std::num::NonZeroU32;
13
14 pub fn is_builtin_attr(attr: &Attribute) -> bool {
15 attr.is_doc_comment() || attr.ident().filter(|ident| is_builtin_attr_name(ident.name)).is_some()
16 }
17
18 enum AttrError {
19 MultipleItem(String),
20 UnknownMetaItem(String, &'static [&'static str]),
21 MissingSince,
22 NonIdentFeature,
23 MissingFeature,
24 MultipleStabilityLevels,
25 UnsupportedLiteral(&'static str, /* is_bytestr */ bool),
26 }
27
28 fn handle_errors(sess: &ParseSess, span: Span, error: AttrError) {
29 let diag = &sess.span_diagnostic;
30 match error {
31 AttrError::MultipleItem(item) => {
32 struct_span_err!(diag, span, E0538, "multiple '{}' items", item).emit();
33 }
34 AttrError::UnknownMetaItem(item, expected) => {
35 let expected = expected.iter().map(|name| format!("`{}`", name)).collect::<Vec<_>>();
36 struct_span_err!(diag, span, E0541, "unknown meta item '{}'", item)
37 .span_label(span, format!("expected one of {}", expected.join(", ")))
38 .emit();
39 }
40 AttrError::MissingSince => {
41 struct_span_err!(diag, span, E0542, "missing 'since'").emit();
42 }
43 AttrError::NonIdentFeature => {
44 struct_span_err!(diag, span, E0546, "'feature' is not an identifier").emit();
45 }
46 AttrError::MissingFeature => {
47 struct_span_err!(diag, span, E0546, "missing 'feature'").emit();
48 }
49 AttrError::MultipleStabilityLevels => {
50 struct_span_err!(diag, span, E0544, "multiple stability levels").emit();
51 }
52 AttrError::UnsupportedLiteral(msg, is_bytestr) => {
53 let mut err = struct_span_err!(diag, span, E0565, "{}", msg);
54 if is_bytestr {
55 if let Ok(lint_str) = sess.source_map().span_to_snippet(span) {
56 err.span_suggestion(
57 span,
58 "consider removing the prefix",
59 lint_str[1..].to_string(),
60 Applicability::MaybeIncorrect,
61 );
62 }
63 }
64 err.emit();
65 }
66 }
67 }
68
69 #[derive(Copy, Clone, PartialEq, Encodable, Decodable, Debug)]
70 pub enum InlineAttr {
71 None,
72 Hint,
73 Always,
74 Never,
75 }
76
77 #[derive(Clone, Encodable, Decodable, Debug, PartialEq, Eq)]
78 pub enum InstructionSetAttr {
79 ArmA32,
80 ArmT32,
81 }
82
83 #[derive(Clone, Encodable, Decodable, Debug)]
84 pub enum OptimizeAttr {
85 None,
86 Speed,
87 Size,
88 }
89
90 #[derive(Copy, Clone, PartialEq)]
91 pub enum UnwindAttr {
92 Allowed,
93 Aborts,
94 }
95
96 /// Determine what `#[unwind]` attribute is present in `attrs`, if any.
97 pub fn find_unwind_attr(sess: &Session, attrs: &[Attribute]) -> Option<UnwindAttr> {
98 attrs.iter().fold(None, |ia, attr| {
99 if sess.check_name(attr, sym::unwind) {
100 if let Some(meta) = attr.meta() {
101 if let MetaItemKind::List(items) = meta.kind {
102 if items.len() == 1 {
103 if items[0].has_name(sym::allowed) {
104 return Some(UnwindAttr::Allowed);
105 } else if items[0].has_name(sym::aborts) {
106 return Some(UnwindAttr::Aborts);
107 }
108 }
109
110 struct_span_err!(
111 sess.diagnostic(),
112 attr.span,
113 E0633,
114 "malformed `unwind` attribute input"
115 )
116 .span_label(attr.span, "invalid argument")
117 .span_suggestions(
118 attr.span,
119 "the allowed arguments are `allowed` and `aborts`",
120 (vec!["allowed", "aborts"])
121 .into_iter()
122 .map(|s| format!("#[unwind({})]", s)),
123 Applicability::MachineApplicable,
124 )
125 .emit();
126 }
127 }
128 }
129
130 ia
131 })
132 }
133
134 /// Represents the following attributes:
135 ///
136 /// - `#[stable]`
137 /// - `#[unstable]`
138 #[derive(Encodable, Decodable, Copy, Clone, Debug, PartialEq, Eq, Hash)]
139 #[derive(HashStable_Generic)]
140 pub struct Stability {
141 pub level: StabilityLevel,
142 pub feature: Symbol,
143 }
144
145 /// Represents the `#[rustc_const_unstable]` and `#[rustc_const_stable]` attributes.
146 #[derive(Encodable, Decodable, Copy, Clone, Debug, PartialEq, Eq, Hash)]
147 #[derive(HashStable_Generic)]
148 pub struct ConstStability {
149 pub level: StabilityLevel,
150 pub feature: Symbol,
151 /// whether the function has a `#[rustc_promotable]` attribute
152 pub promotable: bool,
153 }
154
155 /// The available stability levels.
156 #[derive(Encodable, Decodable, PartialEq, Copy, Clone, Debug, Eq, Hash)]
157 #[derive(HashStable_Generic)]
158 pub enum StabilityLevel {
159 // Reason for the current stability level and the relevant rust-lang issue
160 Unstable { reason: Option<Symbol>, issue: Option<NonZeroU32>, is_soft: bool },
161 Stable { since: Symbol },
162 }
163
164 impl StabilityLevel {
165 pub fn is_unstable(&self) -> bool {
166 matches!(self, StabilityLevel::Unstable { .. })
167 }
168 pub fn is_stable(&self) -> bool {
169 matches!(self, StabilityLevel::Stable { .. })
170 }
171 }
172
173 /// Collects stability info from all stability attributes in `attrs`.
174 /// Returns `None` if no stability attributes are found.
175 pub fn find_stability(
176 sess: &Session,
177 attrs: &[Attribute],
178 item_sp: Span,
179 ) -> (Option<(Stability, Span)>, Option<(ConstStability, Span)>) {
180 find_stability_generic(sess, attrs.iter(), item_sp)
181 }
182
183 fn find_stability_generic<'a, I>(
184 sess: &Session,
185 attrs_iter: I,
186 item_sp: Span,
187 ) -> (Option<(Stability, Span)>, Option<(ConstStability, Span)>)
188 where
189 I: Iterator<Item = &'a Attribute>,
190 {
191 use StabilityLevel::*;
192
193 let mut stab: Option<(Stability, Span)> = None;
194 let mut const_stab: Option<(ConstStability, Span)> = None;
195 let mut promotable = false;
196
197 let diagnostic = &sess.parse_sess.span_diagnostic;
198
199 'outer: for attr in attrs_iter {
200 if ![
201 sym::rustc_const_unstable,
202 sym::rustc_const_stable,
203 sym::unstable,
204 sym::stable,
205 sym::rustc_promotable,
206 ]
207 .iter()
208 .any(|&s| attr.has_name(s))
209 {
210 continue; // not a stability level
211 }
212
213 sess.mark_attr_used(attr);
214
215 let meta = attr.meta();
216
217 if attr.has_name(sym::rustc_promotable) {
218 promotable = true;
219 }
220 // attributes with data
221 else if let Some(MetaItem { kind: MetaItemKind::List(ref metas), .. }) = meta {
222 let meta = meta.as_ref().unwrap();
223 let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
224 if item.is_some() {
225 handle_errors(
226 &sess.parse_sess,
227 meta.span,
228 AttrError::MultipleItem(pprust::path_to_string(&meta.path)),
229 );
230 return false;
231 }
232 if let Some(v) = meta.value_str() {
233 *item = Some(v);
234 true
235 } else {
236 struct_span_err!(diagnostic, meta.span, E0539, "incorrect meta item").emit();
237 false
238 }
239 };
240
241 let meta_name = meta.name_or_empty();
242 match meta_name {
243 sym::rustc_const_unstable | sym::unstable => {
244 if meta_name == sym::unstable && stab.is_some() {
245 handle_errors(
246 &sess.parse_sess,
247 attr.span,
248 AttrError::MultipleStabilityLevels,
249 );
250 break;
251 } else if meta_name == sym::rustc_const_unstable && const_stab.is_some() {
252 handle_errors(
253 &sess.parse_sess,
254 attr.span,
255 AttrError::MultipleStabilityLevels,
256 );
257 break;
258 }
259
260 let mut feature = None;
261 let mut reason = None;
262 let mut issue = None;
263 let mut issue_num = None;
264 let mut is_soft = false;
265 for meta in metas {
266 if let Some(mi) = meta.meta_item() {
267 match mi.name_or_empty() {
268 sym::feature => {
269 if !get(mi, &mut feature) {
270 continue 'outer;
271 }
272 }
273 sym::reason => {
274 if !get(mi, &mut reason) {
275 continue 'outer;
276 }
277 }
278 sym::issue => {
279 if !get(mi, &mut issue) {
280 continue 'outer;
281 }
282
283 // These unwraps are safe because `get` ensures the meta item
284 // is a name/value pair string literal.
285 issue_num = match &*issue.unwrap().as_str() {
286 "none" => None,
287 issue => {
288 let emit_diag = |msg: &str| {
289 struct_span_err!(
290 diagnostic,
291 mi.span,
292 E0545,
293 "`issue` must be a non-zero numeric string \
294 or \"none\"",
295 )
296 .span_label(
297 mi.name_value_literal_span().unwrap(),
298 msg,
299 )
300 .emit();
301 };
302 match issue.parse() {
303 Ok(0) => {
304 emit_diag(
305 "`issue` must not be \"0\", \
306 use \"none\" instead",
307 );
308 continue 'outer;
309 }
310 Ok(num) => NonZeroU32::new(num),
311 Err(err) => {
312 emit_diag(&err.to_string());
313 continue 'outer;
314 }
315 }
316 }
317 };
318 }
319 sym::soft => {
320 if !mi.is_word() {
321 let msg = "`soft` should not have any arguments";
322 sess.parse_sess.span_diagnostic.span_err(mi.span, msg);
323 }
324 is_soft = true;
325 }
326 _ => {
327 handle_errors(
328 &sess.parse_sess,
329 meta.span(),
330 AttrError::UnknownMetaItem(
331 pprust::path_to_string(&mi.path),
332 &["feature", "reason", "issue", "soft"],
333 ),
334 );
335 continue 'outer;
336 }
337 }
338 } else {
339 handle_errors(
340 &sess.parse_sess,
341 meta.span(),
342 AttrError::UnsupportedLiteral("unsupported literal", false),
343 );
344 continue 'outer;
345 }
346 }
347
348 match (feature, reason, issue) {
349 (Some(feature), reason, Some(_)) => {
350 if !rustc_lexer::is_ident(&feature.as_str()) {
351 handle_errors(
352 &sess.parse_sess,
353 attr.span,
354 AttrError::NonIdentFeature,
355 );
356 continue;
357 }
358 let level = Unstable { reason, issue: issue_num, is_soft };
359 if sym::unstable == meta_name {
360 stab = Some((Stability { level, feature }, attr.span));
361 } else {
362 const_stab = Some((
363 ConstStability { level, feature, promotable: false },
364 attr.span,
365 ));
366 }
367 }
368 (None, _, _) => {
369 handle_errors(&sess.parse_sess, attr.span, AttrError::MissingFeature);
370 continue;
371 }
372 _ => {
373 struct_span_err!(diagnostic, attr.span, E0547, "missing 'issue'")
374 .emit();
375 continue;
376 }
377 }
378 }
379 sym::rustc_const_stable | sym::stable => {
380 if meta_name == sym::stable && stab.is_some() {
381 handle_errors(
382 &sess.parse_sess,
383 attr.span,
384 AttrError::MultipleStabilityLevels,
385 );
386 break;
387 } else if meta_name == sym::rustc_const_stable && const_stab.is_some() {
388 handle_errors(
389 &sess.parse_sess,
390 attr.span,
391 AttrError::MultipleStabilityLevels,
392 );
393 break;
394 }
395
396 let mut feature = None;
397 let mut since = None;
398 for meta in metas {
399 match meta {
400 NestedMetaItem::MetaItem(mi) => match mi.name_or_empty() {
401 sym::feature => {
402 if !get(mi, &mut feature) {
403 continue 'outer;
404 }
405 }
406 sym::since => {
407 if !get(mi, &mut since) {
408 continue 'outer;
409 }
410 }
411 _ => {
412 handle_errors(
413 &sess.parse_sess,
414 meta.span(),
415 AttrError::UnknownMetaItem(
416 pprust::path_to_string(&mi.path),
417 &["since", "note"],
418 ),
419 );
420 continue 'outer;
421 }
422 },
423 NestedMetaItem::Literal(lit) => {
424 handle_errors(
425 &sess.parse_sess,
426 lit.span,
427 AttrError::UnsupportedLiteral("unsupported literal", false),
428 );
429 continue 'outer;
430 }
431 }
432 }
433
434 match (feature, since) {
435 (Some(feature), Some(since)) => {
436 let level = Stable { since };
437 if sym::stable == meta_name {
438 stab = Some((Stability { level, feature }, attr.span));
439 } else {
440 const_stab = Some((
441 ConstStability { level, feature, promotable: false },
442 attr.span,
443 ));
444 }
445 }
446 (None, _) => {
447 handle_errors(&sess.parse_sess, attr.span, AttrError::MissingFeature);
448 continue;
449 }
450 _ => {
451 handle_errors(&sess.parse_sess, attr.span, AttrError::MissingSince);
452 continue;
453 }
454 }
455 }
456 _ => unreachable!(),
457 }
458 }
459 }
460
461 // Merge the const-unstable info into the stability info
462 if promotable {
463 if let Some((ref mut stab, _)) = const_stab {
464 stab.promotable = promotable;
465 } else {
466 struct_span_err!(
467 diagnostic,
468 item_sp,
469 E0717,
470 "`rustc_promotable` attribute must be paired with either a `rustc_const_unstable` \
471 or a `rustc_const_stable` attribute"
472 )
473 .emit();
474 }
475 }
476
477 (stab, const_stab)
478 }
479
480 pub fn find_crate_name(sess: &Session, attrs: &[Attribute]) -> Option<Symbol> {
481 sess.first_attr_value_str_by_name(attrs, sym::crate_name)
482 }
483
484 /// Tests if a cfg-pattern matches the cfg set
485 pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Features>) -> bool {
486 eval_condition(cfg, sess, features, &mut |cfg| {
487 try_gate_cfg(cfg, sess, features);
488 let error = |span, msg| {
489 sess.span_diagnostic.span_err(span, msg);
490 true
491 };
492 if cfg.path.segments.len() != 1 {
493 return error(cfg.path.span, "`cfg` predicate key must be an identifier");
494 }
495 match &cfg.kind {
496 MetaItemKind::List(..) => {
497 error(cfg.span, "unexpected parentheses after `cfg` predicate key")
498 }
499 MetaItemKind::NameValue(lit) if !lit.kind.is_str() => {
500 handle_errors(
501 sess,
502 lit.span,
503 AttrError::UnsupportedLiteral(
504 "literal in `cfg` predicate value must be a string",
505 lit.kind.is_bytestr(),
506 ),
507 );
508 true
509 }
510 MetaItemKind::NameValue(..) | MetaItemKind::Word => {
511 let ident = cfg.ident().expect("multi-segment cfg predicate");
512 sess.config.contains(&(ident.name, cfg.value_str()))
513 }
514 }
515 })
516 }
517
518 fn try_gate_cfg(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Features>) {
519 let gate = find_gated_cfg(|sym| cfg.has_name(sym));
520 if let (Some(feats), Some(gated_cfg)) = (features, gate) {
521 gate_cfg(&gated_cfg, cfg.span, sess, feats);
522 }
523 }
524
525 fn gate_cfg(gated_cfg: &GatedCfg, cfg_span: Span, sess: &ParseSess, features: &Features) {
526 let (cfg, feature, has_feature) = gated_cfg;
527 if !has_feature(features) && !cfg_span.allows_unstable(*feature) {
528 let explain = format!("`cfg({})` is experimental and subject to change", cfg);
529 feature_err(sess, *feature, cfg_span, &explain).emit();
530 }
531 }
532
533 #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
534 struct Version {
535 major: u16,
536 minor: u16,
537 patch: u16,
538 }
539
540 fn parse_version(s: &str, allow_appendix: bool) -> Option<Version> {
541 let mut components = s.split('-');
542 let d = components.next()?;
543 if !allow_appendix && components.next().is_some() {
544 return None;
545 }
546 let mut digits = d.splitn(3, '.');
547 let major = digits.next()?.parse().ok()?;
548 let minor = digits.next()?.parse().ok()?;
549 let patch = digits.next().unwrap_or("0").parse().ok()?;
550 Some(Version { major, minor, patch })
551 }
552
553 /// Evaluate a cfg-like condition (with `any` and `all`), using `eval` to
554 /// evaluate individual items.
555 pub fn eval_condition(
556 cfg: &ast::MetaItem,
557 sess: &ParseSess,
558 features: Option<&Features>,
559 eval: &mut impl FnMut(&ast::MetaItem) -> bool,
560 ) -> bool {
561 match cfg.kind {
562 ast::MetaItemKind::List(ref mis) if cfg.name_or_empty() == sym::version => {
563 try_gate_cfg(cfg, sess, features);
564 let (min_version, span) = match &mis[..] {
565 [NestedMetaItem::Literal(Lit { kind: LitKind::Str(sym, ..), span, .. })] => {
566 (sym, span)
567 }
568 [NestedMetaItem::Literal(Lit { span, .. })
569 | NestedMetaItem::MetaItem(MetaItem { span, .. })] => {
570 sess.span_diagnostic
571 .struct_span_err(*span, "expected a version literal")
572 .emit();
573 return false;
574 }
575 [..] => {
576 sess.span_diagnostic
577 .struct_span_err(cfg.span, "expected single version literal")
578 .emit();
579 return false;
580 }
581 };
582 let min_version = match parse_version(&min_version.as_str(), false) {
583 Some(ver) => ver,
584 None => {
585 sess.span_diagnostic
586 .struct_span_warn(
587 *span,
588 "unknown version literal format, assuming it refers to a future version",
589 )
590 .emit();
591 return false;
592 }
593 };
594 let rustc_version = parse_version(env!("CFG_RELEASE"), true).unwrap();
595
596 // See https://github.com/rust-lang/rust/issues/64796#issuecomment-640851454 for details
597 if sess.assume_incomplete_release {
598 rustc_version > min_version
599 } else {
600 rustc_version >= min_version
601 }
602 }
603 ast::MetaItemKind::List(ref mis) => {
604 for mi in mis.iter() {
605 if !mi.is_meta_item() {
606 handle_errors(
607 sess,
608 mi.span(),
609 AttrError::UnsupportedLiteral("unsupported literal", false),
610 );
611 return false;
612 }
613 }
614
615 // The unwraps below may look dangerous, but we've already asserted
616 // that they won't fail with the loop above.
617 match cfg.name_or_empty() {
618 sym::any => mis
619 .iter()
620 .any(|mi| eval_condition(mi.meta_item().unwrap(), sess, features, eval)),
621 sym::all => mis
622 .iter()
623 .all(|mi| eval_condition(mi.meta_item().unwrap(), sess, features, eval)),
624 sym::not => {
625 if mis.len() != 1 {
626 struct_span_err!(
627 sess.span_diagnostic,
628 cfg.span,
629 E0536,
630 "expected 1 cfg-pattern"
631 )
632 .emit();
633 return false;
634 }
635
636 !eval_condition(mis[0].meta_item().unwrap(), sess, features, eval)
637 }
638 _ => {
639 struct_span_err!(
640 sess.span_diagnostic,
641 cfg.span,
642 E0537,
643 "invalid predicate `{}`",
644 pprust::path_to_string(&cfg.path)
645 )
646 .emit();
647 false
648 }
649 }
650 }
651 ast::MetaItemKind::Word | ast::MetaItemKind::NameValue(..) => eval(cfg),
652 }
653 }
654
655 #[derive(Debug, Encodable, Decodable, Clone, HashStable_Generic)]
656 pub struct Deprecation {
657 pub since: Option<Symbol>,
658 /// The note to issue a reason.
659 pub note: Option<Symbol>,
660 /// A text snippet used to completely replace any use of the deprecated item in an expression.
661 ///
662 /// This is currently unstable.
663 pub suggestion: Option<Symbol>,
664
665 /// Whether to treat the since attribute as being a Rust version identifier
666 /// (rather than an opaque string).
667 pub is_since_rustc_version: bool,
668 }
669
670 /// Finds the deprecation attribute. `None` if none exists.
671 pub fn find_deprecation(sess: &Session, attrs: &[Attribute]) -> Option<(Deprecation, Span)> {
672 find_deprecation_generic(sess, attrs.iter())
673 }
674
675 fn find_deprecation_generic<'a, I>(sess: &Session, attrs_iter: I) -> Option<(Deprecation, Span)>
676 where
677 I: Iterator<Item = &'a Attribute>,
678 {
679 let mut depr: Option<(Deprecation, Span)> = None;
680 let diagnostic = &sess.parse_sess.span_diagnostic;
681
682 'outer: for attr in attrs_iter {
683 if !(sess.check_name(attr, sym::deprecated) || sess.check_name(attr, sym::rustc_deprecated))
684 {
685 continue;
686 }
687
688 if let Some((_, span)) = &depr {
689 struct_span_err!(diagnostic, attr.span, E0550, "multiple deprecated attributes")
690 .span_label(attr.span, "repeated deprecation attribute")
691 .span_label(*span, "first deprecation attribute")
692 .emit();
693 break;
694 }
695
696 let meta = match attr.meta() {
697 Some(meta) => meta,
698 None => continue,
699 };
700 let mut since = None;
701 let mut note = None;
702 let mut suggestion = None;
703 match &meta.kind {
704 MetaItemKind::Word => {}
705 MetaItemKind::NameValue(..) => note = meta.value_str(),
706 MetaItemKind::List(list) => {
707 let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
708 if item.is_some() {
709 handle_errors(
710 &sess.parse_sess,
711 meta.span,
712 AttrError::MultipleItem(pprust::path_to_string(&meta.path)),
713 );
714 return false;
715 }
716 if let Some(v) = meta.value_str() {
717 *item = Some(v);
718 true
719 } else {
720 if let Some(lit) = meta.name_value_literal() {
721 handle_errors(
722 &sess.parse_sess,
723 lit.span,
724 AttrError::UnsupportedLiteral(
725 "literal in `deprecated` \
726 value must be a string",
727 lit.kind.is_bytestr(),
728 ),
729 );
730 } else {
731 struct_span_err!(diagnostic, meta.span, E0551, "incorrect meta item")
732 .emit();
733 }
734
735 false
736 }
737 };
738
739 for meta in list {
740 match meta {
741 NestedMetaItem::MetaItem(mi) => match mi.name_or_empty() {
742 sym::since => {
743 if !get(mi, &mut since) {
744 continue 'outer;
745 }
746 }
747 sym::note if sess.check_name(attr, sym::deprecated) => {
748 if !get(mi, &mut note) {
749 continue 'outer;
750 }
751 }
752 sym::reason if sess.check_name(attr, sym::rustc_deprecated) => {
753 if !get(mi, &mut note) {
754 continue 'outer;
755 }
756 }
757 sym::suggestion if sess.check_name(attr, sym::rustc_deprecated) => {
758 if !get(mi, &mut suggestion) {
759 continue 'outer;
760 }
761 }
762 _ => {
763 handle_errors(
764 &sess.parse_sess,
765 meta.span(),
766 AttrError::UnknownMetaItem(
767 pprust::path_to_string(&mi.path),
768 if sess.check_name(attr, sym::deprecated) {
769 &["since", "note"]
770 } else {
771 &["since", "reason", "suggestion"]
772 },
773 ),
774 );
775 continue 'outer;
776 }
777 },
778 NestedMetaItem::Literal(lit) => {
779 handle_errors(
780 &sess.parse_sess,
781 lit.span,
782 AttrError::UnsupportedLiteral(
783 "item in `deprecated` must be a key/value pair",
784 false,
785 ),
786 );
787 continue 'outer;
788 }
789 }
790 }
791 }
792 }
793
794 if suggestion.is_some() && sess.check_name(attr, sym::deprecated) {
795 unreachable!("only allowed on rustc_deprecated")
796 }
797
798 if sess.check_name(attr, sym::rustc_deprecated) {
799 if since.is_none() {
800 handle_errors(&sess.parse_sess, attr.span, AttrError::MissingSince);
801 continue;
802 }
803
804 if note.is_none() {
805 struct_span_err!(diagnostic, attr.span, E0543, "missing 'reason'").emit();
806 continue;
807 }
808 }
809
810 sess.mark_attr_used(&attr);
811
812 let is_since_rustc_version = sess.check_name(attr, sym::rustc_deprecated);
813 depr = Some((Deprecation { since, note, suggestion, is_since_rustc_version }, attr.span));
814 }
815
816 depr
817 }
818
819 #[derive(PartialEq, Debug, Encodable, Decodable, Copy, Clone)]
820 pub enum ReprAttr {
821 ReprInt(IntType),
822 ReprC,
823 ReprPacked(u32),
824 ReprSimd,
825 ReprTransparent,
826 ReprAlign(u32),
827 ReprNoNiche,
828 }
829
830 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
831 #[derive(Encodable, Decodable, HashStable_Generic)]
832 pub enum IntType {
833 SignedInt(ast::IntTy),
834 UnsignedInt(ast::UintTy),
835 }
836
837 impl IntType {
838 #[inline]
839 pub fn is_signed(self) -> bool {
840 use IntType::*;
841
842 match self {
843 SignedInt(..) => true,
844 UnsignedInt(..) => false,
845 }
846 }
847 }
848
849 /// Parse #[repr(...)] forms.
850 ///
851 /// Valid repr contents: any of the primitive integral type names (see
852 /// `int_type_of_word`, below) to specify enum discriminant type; `C`, to use
853 /// the same discriminant size that the corresponding C enum would or C
854 /// structure layout, `packed` to remove padding, and `transparent` to elegate representation
855 /// concerns to the only non-ZST field.
856 pub fn find_repr_attrs(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
857 use ReprAttr::*;
858
859 let mut acc = Vec::new();
860 let diagnostic = &sess.parse_sess.span_diagnostic;
861 if attr.has_name(sym::repr) {
862 if let Some(items) = attr.meta_item_list() {
863 sess.mark_attr_used(attr);
864 for item in items {
865 if !item.is_meta_item() {
866 handle_errors(
867 &sess.parse_sess,
868 item.span(),
869 AttrError::UnsupportedLiteral(
870 "meta item in `repr` must be an identifier",
871 false,
872 ),
873 );
874 continue;
875 }
876
877 let mut recognised = false;
878 if item.is_word() {
879 let hint = match item.name_or_empty() {
880 sym::C => Some(ReprC),
881 sym::packed => Some(ReprPacked(1)),
882 sym::simd => Some(ReprSimd),
883 sym::transparent => Some(ReprTransparent),
884 sym::no_niche => Some(ReprNoNiche),
885 name => int_type_of_word(name).map(ReprInt),
886 };
887
888 if let Some(h) = hint {
889 recognised = true;
890 acc.push(h);
891 }
892 } else if let Some((name, value)) = item.name_value_literal() {
893 let parse_alignment = |node: &ast::LitKind| -> Result<u32, &'static str> {
894 if let ast::LitKind::Int(literal, ast::LitIntType::Unsuffixed) = node {
895 if literal.is_power_of_two() {
896 // rustc_middle::ty::layout::Align restricts align to <= 2^29
897 if *literal <= 1 << 29 {
898 Ok(*literal as u32)
899 } else {
900 Err("larger than 2^29")
901 }
902 } else {
903 Err("not a power of two")
904 }
905 } else {
906 Err("not an unsuffixed integer")
907 }
908 };
909
910 let mut literal_error = None;
911 if name == sym::align {
912 recognised = true;
913 match parse_alignment(&value.kind) {
914 Ok(literal) => acc.push(ReprAlign(literal)),
915 Err(message) => literal_error = Some(message),
916 };
917 } else if name == sym::packed {
918 recognised = true;
919 match parse_alignment(&value.kind) {
920 Ok(literal) => acc.push(ReprPacked(literal)),
921 Err(message) => literal_error = Some(message),
922 };
923 }
924 if let Some(literal_error) = literal_error {
925 struct_span_err!(
926 diagnostic,
927 item.span(),
928 E0589,
929 "invalid `repr(align)` attribute: {}",
930 literal_error
931 )
932 .emit();
933 }
934 } else if let Some(meta_item) = item.meta_item() {
935 if meta_item.has_name(sym::align) {
936 if let MetaItemKind::NameValue(ref value) = meta_item.kind {
937 recognised = true;
938 let mut err = struct_span_err!(
939 diagnostic,
940 item.span(),
941 E0693,
942 "incorrect `repr(align)` attribute format"
943 );
944 match value.kind {
945 ast::LitKind::Int(int, ast::LitIntType::Unsuffixed) => {
946 err.span_suggestion(
947 item.span(),
948 "use parentheses instead",
949 format!("align({})", int),
950 Applicability::MachineApplicable,
951 );
952 }
953 ast::LitKind::Str(s, _) => {
954 err.span_suggestion(
955 item.span(),
956 "use parentheses instead",
957 format!("align({})", s),
958 Applicability::MachineApplicable,
959 );
960 }
961 _ => {}
962 }
963 err.emit();
964 }
965 }
966 }
967 if !recognised {
968 // Not a word we recognize
969 struct_span_err!(
970 diagnostic,
971 item.span(),
972 E0552,
973 "unrecognized representation hint"
974 )
975 .emit();
976 }
977 }
978 }
979 }
980 acc
981 }
982
983 fn int_type_of_word(s: Symbol) -> Option<IntType> {
984 use IntType::*;
985
986 match s {
987 sym::i8 => Some(SignedInt(ast::IntTy::I8)),
988 sym::u8 => Some(UnsignedInt(ast::UintTy::U8)),
989 sym::i16 => Some(SignedInt(ast::IntTy::I16)),
990 sym::u16 => Some(UnsignedInt(ast::UintTy::U16)),
991 sym::i32 => Some(SignedInt(ast::IntTy::I32)),
992 sym::u32 => Some(UnsignedInt(ast::UintTy::U32)),
993 sym::i64 => Some(SignedInt(ast::IntTy::I64)),
994 sym::u64 => Some(UnsignedInt(ast::UintTy::U64)),
995 sym::i128 => Some(SignedInt(ast::IntTy::I128)),
996 sym::u128 => Some(UnsignedInt(ast::UintTy::U128)),
997 sym::isize => Some(SignedInt(ast::IntTy::Isize)),
998 sym::usize => Some(UnsignedInt(ast::UintTy::Usize)),
999 _ => None,
1000 }
1001 }
1002
1003 pub enum TransparencyError {
1004 UnknownTransparency(Symbol, Span),
1005 MultipleTransparencyAttrs(Span, Span),
1006 }
1007
1008 pub fn find_transparency(
1009 sess: &Session,
1010 attrs: &[Attribute],
1011 macro_rules: bool,
1012 ) -> (Transparency, Option<TransparencyError>) {
1013 let mut transparency = None;
1014 let mut error = None;
1015 for attr in attrs {
1016 if sess.check_name(attr, sym::rustc_macro_transparency) {
1017 if let Some((_, old_span)) = transparency {
1018 error = Some(TransparencyError::MultipleTransparencyAttrs(old_span, attr.span));
1019 break;
1020 } else if let Some(value) = attr.value_str() {
1021 transparency = Some((
1022 match value {
1023 sym::transparent => Transparency::Transparent,
1024 sym::semitransparent => Transparency::SemiTransparent,
1025 sym::opaque => Transparency::Opaque,
1026 _ => {
1027 error = Some(TransparencyError::UnknownTransparency(value, attr.span));
1028 continue;
1029 }
1030 },
1031 attr.span,
1032 ));
1033 }
1034 }
1035 }
1036 let fallback = if macro_rules { Transparency::SemiTransparent } else { Transparency::Opaque };
1037 (transparency.map_or(fallback, |t| t.0), error)
1038 }
1039
1040 pub fn allow_internal_unstable<'a>(
1041 sess: &'a Session,
1042 attrs: &'a [Attribute],
1043 ) -> impl Iterator<Item = Symbol> + 'a {
1044 allow_unstable(sess, attrs, sym::allow_internal_unstable)
1045 }
1046
1047 pub fn rustc_allow_const_fn_unstable<'a>(
1048 sess: &'a Session,
1049 attrs: &'a [Attribute],
1050 ) -> impl Iterator<Item = Symbol> + 'a {
1051 allow_unstable(sess, attrs, sym::rustc_allow_const_fn_unstable)
1052 }
1053
1054 fn allow_unstable<'a>(
1055 sess: &'a Session,
1056 attrs: &'a [Attribute],
1057 symbol: Symbol,
1058 ) -> impl Iterator<Item = Symbol> + 'a {
1059 let attrs = sess.filter_by_name(attrs, symbol);
1060 let list = attrs
1061 .filter_map(move |attr| {
1062 attr.meta_item_list().or_else(|| {
1063 sess.diagnostic().span_err(
1064 attr.span,
1065 &format!("`{}` expects a list of feature names", symbol.to_ident_string()),
1066 );
1067 None
1068 })
1069 })
1070 .flatten();
1071
1072 list.into_iter().filter_map(move |it| {
1073 let name = it.ident().map(|ident| ident.name);
1074 if name.is_none() {
1075 sess.diagnostic().span_err(
1076 it.span(),
1077 &format!("`{}` expects feature names", symbol.to_ident_string()),
1078 );
1079 }
1080 name
1081 })
1082 }