1use std::borrow::Cow;
2use std::path::PathBuf;
3
4pub use ReprAttr::*;
5use rustc_abi::Align;
6pub use rustc_ast::attr::data_structures::*;
7use rustc_ast::token::DocFragmentKind;
8use rustc_ast::{AttrStyle, ast};
9use rustc_data_structures::fx::FxIndexMap;
10use rustc_error_messages::{DiagArgValue, IntoDiagArg};
11use rustc_macros::{Decodable, Encodable, HashStable_Generic, PrintAttribute};
12use rustc_span::def_id::DefId;
13use rustc_span::hygiene::Transparency;
14use rustc_span::{Ident, Span, Symbol};
15pub use rustc_target::spec::SanitizerSet;
16use thin_vec::ThinVec;
17
18use crate::attrs::pretty_printing::PrintAttribute;
19use crate::limit::Limit;
20use crate::{DefaultBodyStability, PartialConstStability, RustcVersion, Stability};
21
22#[derive(Copy, Clone, Debug, HashStable_Generic, Encodable, Decodable, PrintAttribute)]
23pub struct EiiImpl {
24 pub eii_macro: DefId,
25 pub impl_marked_unsafe: bool,
26 pub span: Span,
27 pub inner_span: Span,
28 pub is_default: bool,
29}
30
31#[derive(Copy, Clone, Debug, HashStable_Generic, Encodable, Decodable, PrintAttribute)]
32pub struct EiiDecl {
33 pub eii_extern_target: DefId,
34 pub impl_unsafe: bool,
36 pub span: Span,
37}
38
39#[derive(Copy, Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic, PrintAttribute)]
40pub enum InlineAttr {
41 None,
42 Hint,
43 Always,
44 Never,
45 Force {
49 attr_span: Span,
50 reason: Option<Symbol>,
51 },
52}
53
54impl InlineAttr {
55 pub fn always(&self) -> bool {
56 match self {
57 InlineAttr::Always | InlineAttr::Force { .. } => true,
58 InlineAttr::None | InlineAttr::Hint | InlineAttr::Never => false,
59 }
60 }
61}
62
63#[derive(
64 Copy,
65 Clone,
66 Encodable,
67 Decodable,
68 Debug,
69 PartialEq,
70 Eq,
71 HashStable_Generic,
72 PrintAttribute
73)]
74pub enum InstructionSetAttr {
75 ArmA32,
76 ArmT32,
77}
78
79#[derive(Copy, Clone, Debug, PartialEq, Eq, Default, PrintAttribute)]
80#[derive(Encodable, Decodable, HashStable_Generic)]
81pub enum OptimizeAttr {
82 #[default]
84 Default,
85 DoNotOptimize,
87 Speed,
89 Size,
91}
92
93impl OptimizeAttr {
94 pub fn do_not_optimize(&self) -> bool {
95 matches!(self, Self::DoNotOptimize)
96 }
97}
98
99#[derive(PartialEq, Debug, Encodable, Decodable, Copy, Clone, HashStable_Generic, PrintAttribute)]
100pub enum ReprAttr {
101 ReprInt(IntType),
102 ReprRust,
103 ReprC,
104 ReprPacked(Align),
105 ReprSimd,
106 ReprTransparent,
107 ReprAlign(Align),
108}
109
110pub enum TransparencyError {
111 UnknownTransparency(Symbol, Span),
112 MultipleTransparencyAttrs(Span, Span),
113}
114
115#[derive(Eq, PartialEq, Debug, Copy, Clone)]
116#[derive(Encodable, Decodable, HashStable_Generic, PrintAttribute)]
117pub enum IntType {
118 SignedInt(ast::IntTy),
119 UnsignedInt(ast::UintTy),
120}
121
122#[derive(Copy, Debug, Encodable, Decodable, Clone, HashStable_Generic, PrintAttribute)]
123pub struct Deprecation {
124 pub since: DeprecatedSince,
125 pub note: Option<Symbol>,
127 pub suggestion: Option<Symbol>,
131}
132
133#[derive(Copy, Debug, Encodable, Decodable, Clone, HashStable_Generic, PrintAttribute)]
135pub enum DeprecatedSince {
136 RustcVersion(RustcVersion),
137 Future,
139 NonStandard(Symbol),
142 Unspecified,
144 Err,
147}
148
149#[derive(Copy, Debug, Eq, PartialEq, Encodable, Decodable, Clone)]
151#[derive(HashStable_Generic, PrintAttribute)]
152pub enum CoverageAttrKind {
153 On,
154 Off,
155}
156
157impl Deprecation {
158 pub fn is_in_effect(&self) -> bool {
162 match self.since {
163 DeprecatedSince::RustcVersion(since) => since <= RustcVersion::CURRENT,
164 DeprecatedSince::Future => false,
165 DeprecatedSince::NonStandard(_) => true,
167 DeprecatedSince::Unspecified | DeprecatedSince::Err => true,
169 }
170 }
171
172 pub fn is_since_rustc_version(&self) -> bool {
173 matches!(self.since, DeprecatedSince::RustcVersion(_))
174 }
175}
176
177#[derive(Encodable, Decodable, Copy, Clone, Debug, PartialEq, Eq, Hash)]
182#[derive(HashStable_Generic, PrintAttribute)]
183pub enum UsedBy {
184 Default,
185 Compiler,
186 Linker,
187}
188
189#[derive(Encodable, Decodable, Clone, Debug, PartialEq, Eq, Hash)]
190#[derive(HashStable_Generic, PrintAttribute)]
191pub enum MacroUseArgs {
192 UseAll,
193 UseSpecific(ThinVec<Ident>),
194}
195
196impl Default for MacroUseArgs {
197 fn default() -> Self {
198 Self::UseSpecific(ThinVec::new())
199 }
200}
201
202#[derive(Debug, Clone, Encodable, Decodable, HashStable_Generic)]
203pub struct StrippedCfgItem<ModId = DefId> {
204 pub parent_module: ModId,
205 pub ident: Ident,
206 pub cfg: (CfgEntry, Span),
207}
208
209impl<ModId> StrippedCfgItem<ModId> {
210 pub fn map_mod_id<New>(self, f: impl FnOnce(ModId) -> New) -> StrippedCfgItem<New> {
211 StrippedCfgItem { parent_module: f(self.parent_module), ident: self.ident, cfg: self.cfg }
212 }
213}
214
215#[derive(Encodable, Decodable, Clone, Copy, Debug, PartialEq, Eq, Hash)]
220#[derive(HashStable_Generic, PrintAttribute)]
221pub enum Linkage {
222 AvailableExternally,
223 Common,
224 ExternalWeak,
225 External,
226 Internal,
227 LinkOnceAny,
228 LinkOnceODR,
229 WeakAny,
230 WeakODR,
231}
232
233#[derive(Clone, Copy, Decodable, Debug, Encodable, PartialEq)]
234#[derive(HashStable_Generic, PrintAttribute)]
235pub enum MirDialect {
236 Analysis,
237 Built,
238 Runtime,
239}
240
241impl IntoDiagArg for MirDialect {
242 fn into_diag_arg(self, _path: &mut Option<PathBuf>) -> DiagArgValue {
243 let arg = match self {
244 MirDialect::Analysis => "analysis",
245 MirDialect::Built => "built",
246 MirDialect::Runtime => "runtime",
247 };
248 DiagArgValue::Str(Cow::Borrowed(arg))
249 }
250}
251
252#[derive(Clone, Copy, Decodable, Debug, Encodable, PartialEq)]
253#[derive(HashStable_Generic, PrintAttribute)]
254pub enum MirPhase {
255 Initial,
256 PostCleanup,
257 Optimized,
258}
259
260impl IntoDiagArg for MirPhase {
261 fn into_diag_arg(self, _path: &mut Option<PathBuf>) -> DiagArgValue {
262 let arg = match self {
263 MirPhase::Initial => "initial",
264 MirPhase::PostCleanup => "post-cleanup",
265 MirPhase::Optimized => "optimized",
266 };
267 DiagArgValue::Str(Cow::Borrowed(arg))
268 }
269}
270
271#[derive(
274 Copy,
275 Clone,
276 Debug,
277 Encodable,
278 Decodable,
279 HashStable_Generic,
280 PartialEq,
281 Eq,
282 PrintAttribute
283)]
284pub enum PeImportNameType {
285 Ordinal(u16),
288 Decorated,
291 NoPrefix,
294 Undecorated,
298}
299
300#[derive(
301 Copy,
302 Clone,
303 Debug,
304 PartialEq,
305 Eq,
306 PartialOrd,
307 Ord,
308 Hash,
309 Encodable,
310 Decodable,
311 PrintAttribute
312)]
313#[derive(HashStable_Generic)]
314pub enum NativeLibKind {
315 Static {
317 bundle: Option<bool>,
319 whole_archive: Option<bool>,
321 },
322 Dylib {
325 as_needed: Option<bool>,
327 },
328 RawDylib {
331 as_needed: Option<bool>,
333 },
334 Framework {
336 as_needed: Option<bool>,
338 },
339 LinkArg,
342
343 WasmImportModule,
345
346 Unspecified,
348}
349
350impl NativeLibKind {
351 pub fn has_modifiers(&self) -> bool {
352 match self {
353 NativeLibKind::Static { bundle, whole_archive } => {
354 bundle.is_some() || whole_archive.is_some()
355 }
356 NativeLibKind::Dylib { as_needed }
357 | NativeLibKind::Framework { as_needed }
358 | NativeLibKind::RawDylib { as_needed } => as_needed.is_some(),
359 NativeLibKind::Unspecified
360 | NativeLibKind::LinkArg
361 | NativeLibKind::WasmImportModule => false,
362 }
363 }
364
365 pub fn is_statically_included(&self) -> bool {
366 matches!(self, NativeLibKind::Static { .. })
367 }
368
369 pub fn is_dllimport(&self) -> bool {
370 matches!(
371 self,
372 NativeLibKind::Dylib { .. }
373 | NativeLibKind::RawDylib { .. }
374 | NativeLibKind::Unspecified
375 )
376 }
377}
378
379#[derive(Debug, Encodable, Decodable, Clone, HashStable_Generic, PrintAttribute)]
380pub struct LinkEntry {
381 pub span: Span,
382 pub kind: NativeLibKind,
383 pub name: Symbol,
384 pub cfg: Option<CfgEntry>,
385 pub verbatim: Option<bool>,
386 pub import_name_type: Option<(PeImportNameType, Span)>,
387}
388
389#[derive(HashStable_Generic, PrintAttribute)]
390#[derive(Copy, PartialEq, PartialOrd, Clone, Ord, Eq, Hash, Debug, Encodable, Decodable)]
391pub enum DebuggerVisualizerType {
392 Natvis,
393 GdbPrettyPrinter,
394}
395
396#[derive(Debug, Encodable, Decodable, Clone, HashStable_Generic, PrintAttribute)]
397pub struct DebugVisualizer {
398 pub span: Span,
399 pub visualizer_type: DebuggerVisualizerType,
400 pub path: Symbol,
401}
402
403#[derive(Clone, Copy, Debug, Decodable, Encodable, Eq, PartialEq)]
404#[derive(HashStable_Generic, PrintAttribute)]
405#[derive_const(Default)]
406pub enum RtsanSetting {
407 Nonblocking,
408 Blocking,
409 #[default]
410 Caller,
411}
412
413#[derive(Eq, PartialEq, Debug, Copy, Clone)]
414#[derive(Encodable, Decodable, HashStable_Generic, PrintAttribute)]
415pub enum WindowsSubsystemKind {
416 Console,
417 Windows,
418}
419
420impl WindowsSubsystemKind {
421 pub fn as_str(&self) -> &'static str {
422 match self {
423 WindowsSubsystemKind::Console => "console",
424 WindowsSubsystemKind::Windows => "windows",
425 }
426 }
427}
428
429#[derive(Copy, Clone, Debug, PartialEq)]
430#[derive(HashStable_Generic, Encodable, Decodable, PrintAttribute)]
431pub enum DocInline {
432 Inline,
433 NoInline,
434}
435
436#[derive(Copy, Clone, Debug, PartialEq)]
437#[derive(HashStable_Generic, Encodable, Decodable, PrintAttribute)]
438pub enum HideOrShow {
439 Hide,
440 Show,
441}
442
443#[derive(Clone, Debug, HashStable_Generic, Encodable, Decodable, PrintAttribute)]
444pub struct CfgInfo {
445 pub name: Symbol,
446 pub name_span: Span,
447 pub value: Option<(Symbol, Span)>,
448}
449
450impl CfgInfo {
451 pub fn span_for_name_and_value(&self) -> Span {
452 if let Some((_, value_span)) = self.value {
453 self.name_span.with_hi(value_span.hi())
454 } else {
455 self.name_span
456 }
457 }
458}
459
460#[derive(Clone, Debug, HashStable_Generic, Encodable, Decodable, PrintAttribute)]
461pub struct CfgHideShow {
462 pub kind: HideOrShow,
463 pub values: ThinVec<CfgInfo>,
464}
465
466#[derive(Clone, Debug, Default, HashStable_Generic, Decodable, PrintAttribute)]
467pub struct DocAttribute {
468 pub aliases: FxIndexMap<Symbol, Span>,
469 pub hidden: Option<Span>,
470 pub inline: ThinVec<(DocInline, Span)>,
473
474 pub cfg: ThinVec<CfgEntry>,
476 pub auto_cfg: ThinVec<(CfgHideShow, Span)>,
477 pub auto_cfg_change: ThinVec<(bool, Span)>,
479
480 pub fake_variadic: Option<Span>,
482 pub keyword: Option<(Symbol, Span)>,
483 pub attribute: Option<(Symbol, Span)>,
484 pub masked: Option<Span>,
485 pub notable_trait: Option<Span>,
486 pub search_unbox: Option<Span>,
487
488 pub html_favicon_url: Option<(Symbol, Span)>,
490 pub html_logo_url: Option<(Symbol, Span)>,
491 pub html_playground_url: Option<(Symbol, Span)>,
492 pub html_root_url: Option<(Symbol, Span)>,
493 pub html_no_source: Option<Span>,
494 pub issue_tracker_base_url: Option<(Symbol, Span)>,
495 pub rust_logo: Option<Span>,
496
497 pub test_attrs: ThinVec<Span>,
499 pub no_crate_inject: Option<Span>,
500}
501
502impl<E: rustc_span::SpanEncoder> rustc_serialize::Encodable<E> for DocAttribute {
503 fn encode(&self, encoder: &mut E) {
504 let DocAttribute {
505 aliases,
506 hidden,
507 inline,
508 cfg,
509 auto_cfg,
510 auto_cfg_change,
511 fake_variadic,
512 keyword,
513 attribute,
514 masked,
515 notable_trait,
516 search_unbox,
517 html_favicon_url,
518 html_logo_url,
519 html_playground_url,
520 html_root_url,
521 html_no_source,
522 issue_tracker_base_url,
523 rust_logo,
524 test_attrs,
525 no_crate_inject,
526 } = self;
527 rustc_serialize::Encodable::<E>::encode(aliases, encoder);
528 rustc_serialize::Encodable::<E>::encode(hidden, encoder);
529
530 let inline: ThinVec<_> =
534 inline.iter().filter(|(i, _)| *i != DocInline::Inline).cloned().collect();
535 rustc_serialize::Encodable::<E>::encode(&inline, encoder);
536
537 rustc_serialize::Encodable::<E>::encode(cfg, encoder);
538 rustc_serialize::Encodable::<E>::encode(auto_cfg, encoder);
539 rustc_serialize::Encodable::<E>::encode(auto_cfg_change, encoder);
540 rustc_serialize::Encodable::<E>::encode(fake_variadic, encoder);
541 rustc_serialize::Encodable::<E>::encode(keyword, encoder);
542 rustc_serialize::Encodable::<E>::encode(attribute, encoder);
543 rustc_serialize::Encodable::<E>::encode(masked, encoder);
544 rustc_serialize::Encodable::<E>::encode(notable_trait, encoder);
545 rustc_serialize::Encodable::<E>::encode(search_unbox, encoder);
546 rustc_serialize::Encodable::<E>::encode(html_favicon_url, encoder);
547 rustc_serialize::Encodable::<E>::encode(html_logo_url, encoder);
548 rustc_serialize::Encodable::<E>::encode(html_playground_url, encoder);
549 rustc_serialize::Encodable::<E>::encode(html_root_url, encoder);
550 rustc_serialize::Encodable::<E>::encode(html_no_source, encoder);
551 rustc_serialize::Encodable::<E>::encode(issue_tracker_base_url, encoder);
552 rustc_serialize::Encodable::<E>::encode(rust_logo, encoder);
553 rustc_serialize::Encodable::<E>::encode(test_attrs, encoder);
554 rustc_serialize::Encodable::<E>::encode(no_crate_inject, encoder);
555 }
556}
557
558#[derive(Clone, Debug, HashStable_Generic, Encodable, Decodable, PrintAttribute)]
608pub enum AttributeKind {
609 Align { align: Align, span: Span },
613
614 AllowConstFnUnstable(ThinVec<Symbol>, Span),
616
617 AllowIncoherentImpl(Span),
619
620 AllowInternalUnsafe(Span),
622
623 AllowInternalUnstable(ThinVec<(Symbol, Span)>, Span),
625
626 AsPtr(Span),
628
629 AutomaticallyDerived(Span),
631
632 BodyStability {
634 stability: DefaultBodyStability,
635 span: Span,
637 },
638
639 CfgAttrTrace,
641
642 CfgTrace(ThinVec<(CfgEntry, Span)>),
644
645 CfiEncoding { encoding: Symbol },
647
648 Coinductive(Span),
650
651 Cold(Span),
653
654 Confusables {
656 symbols: ThinVec<Symbol>,
657 first_span: Span,
659 },
660
661 ConstContinue(Span),
663
664 ConstStability {
666 stability: PartialConstStability,
667 span: Span,
669 },
670
671 ConstStabilityIndirect,
673
674 Coroutine(Span),
676
677 Coverage(Span, CoverageAttrKind),
679
680 CrateName { name: Symbol, name_span: Span, attr_span: Span },
682
683 CustomMir(Option<(MirDialect, Span)>, Option<(MirPhase, Span)>, Span),
685
686 DebuggerVisualizer(ThinVec<DebugVisualizer>),
688
689 DenyExplicitImpl(Span),
691
692 Deprecation { deprecation: Deprecation, span: Span },
694
695 DoNotImplementViaObject(Span),
697
698 Doc(Box<DocAttribute>),
702
703 DocComment { style: AttrStyle, kind: DocFragmentKind, span: Span, comment: Symbol },
706
707 Dummy,
709
710 EiiExternItem,
712
713 EiiExternTarget(EiiDecl),
715
716 EiiImpls(ThinVec<EiiImpl>),
718
719 ExportName {
721 name: Symbol,
724 span: Span,
725 },
726
727 ExportStable,
729
730 FfiConst(Span),
732
733 FfiPure(Span),
735
736 Fundamental,
738
739 Ignore {
741 span: Span,
742 reason: Option<Symbol>,
744 },
745
746 Inline(InlineAttr, Span),
748
749 InstructionSet(InstructionSetAttr),
751
752 Link(ThinVec<LinkEntry>, Span),
754
755 LinkName { name: Symbol, span: Span },
757
758 LinkOrdinal { ordinal: u16, span: Span },
760
761 LinkSection { name: Symbol, span: Span },
763
764 Linkage(Linkage, Span),
766
767 LoopMatch(Span),
769
770 MacroEscape(Span),
772
773 MacroExport { span: Span, local_inner_macros: bool },
775
776 MacroTransparency(Transparency),
778
779 MacroUse { span: Span, arguments: MacroUseArgs },
781
782 Marker(Span),
784
785 MayDangle(Span),
787
788 MoveSizeLimit { attr_span: Span, limit_span: Span, limit: Limit },
790
791 MustUse {
793 span: Span,
794 reason: Option<Symbol>,
796 },
797
798 Naked(Span),
800
801 NoCore(Span),
803
804 NoImplicitPrelude(Span),
806
807 NoLink,
809
810 NoMangle(Span),
812
813 NoStd(Span),
815
816 NonExhaustive(Span),
818
819 ObjcClass { classname: Symbol, span: Span },
821
822 ObjcSelector { methname: Symbol, span: Span },
824
825 Optimize(OptimizeAttr, Span),
827
828 ParenSugar(Span),
830
831 PassByValue(Span),
833
834 Path(Symbol, Span),
836
837 PatternComplexityLimit { attr_span: Span, limit_span: Span, limit: Limit },
839
840 PinV2(Span),
842
843 Pointee(Span),
845
846 ProcMacro(Span),
848
849 ProcMacroAttribute(Span),
851
852 ProcMacroDerive { trait_name: Symbol, helper_attrs: ThinVec<Symbol>, span: Span },
854
855 PubTransparent(Span),
857
858 RecursionLimit { attr_span: Span, limit_span: Span, limit: Limit },
860
861 Repr { reprs: ThinVec<(ReprAttr, Span)>, first_span: Span },
863
864 RustcBuiltinMacro { builtin_name: Option<Symbol>, helper_attrs: ThinVec<Symbol>, span: Span },
866
867 RustcCoherenceIsCore(Span),
869
870 RustcLayoutScalarValidRangeEnd(Box<u128>, Span),
872
873 RustcLayoutScalarValidRangeStart(Box<u128>, Span),
875
876 RustcLegacyConstGenerics { fn_indexes: ThinVec<(usize, Span)>, attr_span: Span },
878
879 RustcLintDiagnostics,
881
882 RustcLintOptDenyFieldAccess { lint_message: Symbol },
884
885 RustcLintOptTy,
887
888 RustcLintQueryInstability,
890
891 RustcLintUntrackedQueryInformation,
893
894 RustcMain,
896
897 RustcMustImplementOneOf { attr_span: Span, fn_names: ThinVec<Ident> },
899
900 RustcNeverReturnsNullPointer,
902
903 RustcNoImplicitAutorefs,
905
906 RustcObjectLifetimeDefault,
908
909 RustcPassIndirectlyInNonRusticAbis(Span),
911
912 RustcScalableVector {
914 element_count: Option<u16>,
917 span: Span,
918 },
919
920 RustcShouldNotBeCalledOnConstItems(Span),
922
923 RustcSimdMonomorphizeLaneLimit(Limit),
925
926 Sanitize {
932 on_set: SanitizerSet,
933 off_set: SanitizerSet,
934 rtsan: Option<RtsanSetting>,
935 span: Span,
936 },
937
938 ShouldPanic { reason: Option<Symbol>, span: Span },
940
941 SkipDuringMethodDispatch { array: bool, boxed_slice: bool, span: Span },
943
944 SpecializationTrait(Span),
946
947 Stability {
949 stability: Stability,
950 span: Span,
952 },
953
954 StdInternalSymbol(Span),
956
957 TargetFeature { features: ThinVec<(Symbol, Span)>, attr_span: Span, was_forced: bool },
960
961 ThreadLocal,
963
964 TrackCaller(Span),
966
967 TypeConst(Span),
969
970 TypeLengthLimit { attr_span: Span, limit_span: Span, limit: Limit },
972
973 UnsafeSpecializationMarker(Span),
975
976 UnstableFeatureBound(ThinVec<(Symbol, Span)>),
978
979 Used { used_by: UsedBy, span: Span },
981
982 WindowsSubsystem(WindowsSubsystemKind, Span),
984 }