Skip to content

Commit f0083c9

Browse files
committed
Port crate_type to attribute parser
1 parent 5bbd5b0 commit f0083c9

41 files changed

Lines changed: 352 additions & 278 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

compiler/rustc_attr_parsing/src/attributes/crate_level.rs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
use rustc_hir::attrs::WindowsSubsystemKind;
1+
use rustc_hir::attrs::{CrateType, WindowsSubsystemKind};
2+
use rustc_hir::lints::AttributeLintKind;
3+
use rustc_session::lint::builtin::UNKNOWN_CRATE_TYPES;
4+
use rustc_span::Symbol;
5+
use rustc_span::edit_distance::find_best_match_for_name;
26

37
use super::prelude::*;
48

@@ -26,6 +30,60 @@ impl<S: Stage> SingleAttributeParser<S> for CrateNameParser {
2630
}
2731
}
2832

33+
pub(crate) struct CrateTypeParser;
34+
35+
impl<S: Stage> CombineAttributeParser<S> for CrateTypeParser {
36+
const PATH: &[Symbol] = &[sym::crate_type];
37+
type Item = CrateType;
38+
const CONVERT: ConvertFn<Self::Item> = |items, _| AttributeKind::CrateType(items);
39+
40+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
41+
42+
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "crate type");
43+
44+
fn extend(
45+
cx: &mut AcceptContext<'_, '_, S>,
46+
args: &ArgParser,
47+
) -> impl IntoIterator<Item = Self::Item> {
48+
// We don't error on malformed `#![crate_type]` when not applied to a crate
49+
let report_errors = cx.shared.target == Target::Crate;
50+
51+
let ArgParser::NameValue(n) = args else {
52+
if report_errors {
53+
cx.expected_name_value(cx.attr_span, None);
54+
}
55+
return None;
56+
};
57+
58+
let Some(crate_type) = n.value_as_str() else {
59+
if report_errors {
60+
cx.expected_string_literal(n.value_span, Some(n.value_as_lit()));
61+
}
62+
return None;
63+
};
64+
let Ok(crate_type) = crate_type.try_into() else {
65+
if report_errors {
66+
let candidate = find_best_match_for_name(
67+
&CrateType::all_stable().iter().map(|(name, _)| *name).collect::<Vec<_>>(),
68+
crate_type,
69+
None,
70+
);
71+
cx.emit_lint(
72+
UNKNOWN_CRATE_TYPES,
73+
AttributeLintKind::CrateTypeUnknown {
74+
span: n.value_span,
75+
suggested: candidate,
76+
},
77+
n.value_span,
78+
);
79+
}
80+
return None;
81+
};
82+
83+
Some(crate_type)
84+
}
85+
}
86+
2987
pub(crate) struct RecursionLimitParser;
3088

3189
impl<S: Stage> SingleAttributeParser<S> for RecursionLimitParser {

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use crate::attributes::codegen_attrs::{
2828
};
2929
use crate::attributes::confusables::ConfusablesParser;
3030
use crate::attributes::crate_level::{
31-
CrateNameParser, MoveSizeLimitParser, NoCoreParser, NoMainParser, NoStdParser,
31+
CrateNameParser, CrateTypeParser, MoveSizeLimitParser, NoCoreParser, NoMainParser, NoStdParser,
3232
PatternComplexityLimitParser, RecursionLimitParser, RustcCoherenceIsCoreParser,
3333
TypeLengthLimitParser, WindowsSubsystemParser,
3434
};
@@ -191,6 +191,7 @@ attribute_parsers!(
191191
// tidy-alphabetical-start
192192
Combine<AllowConstFnUnstableParser>,
193193
Combine<AllowInternalUnstableParser>,
194+
Combine<CrateTypeParser>,
194195
Combine<DebuggerViualizerParser>,
195196
Combine<ForceTargetFeatureParser>,
196197
Combine<LinkParser>,

compiler/rustc_codegen_llvm/src/debuginfo/gdb.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub(crate) fn needs_gdb_debug_scripts_section(cx: &CodegenCx<'_, '_>) -> bool {
9292
CrateType::Executable
9393
| CrateType::Dylib
9494
| CrateType::Cdylib
95-
| CrateType::Staticlib
95+
| CrateType::StaticLib
9696
| CrateType::Sdylib => {
9797
// These are crate types for which we will embed pretty printers since they
9898
// are treated as leaf crates.

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ pub fn link_binary(
136136
)
137137
.build(&out_filename);
138138
}
139-
CrateType::Staticlib => {
139+
CrateType::StaticLib => {
140140
link_staticlib(
141141
sess,
142142
archive_builder_builder,
@@ -474,7 +474,7 @@ fn link_staticlib(
474474

475475
let res = each_linked_rlib(
476476
&codegen_results.crate_info,
477-
Some(CrateType::Staticlib),
477+
Some(CrateType::StaticLib),
478478
&mut |cnum, path| {
479479
let lto = are_upstream_rust_objects_already_included(sess)
480480
&& !ignored_for_lto(sess, &codegen_results.crate_info, cnum);
@@ -532,7 +532,7 @@ fn link_staticlib(
532532
let fmts = codegen_results
533533
.crate_info
534534
.dependency_formats
535-
.get(&CrateType::Staticlib)
535+
.get(&CrateType::StaticLib)
536536
.expect("no dependency formats for staticlib");
537537

538538
let mut all_rust_dylibs = vec![];
@@ -1210,7 +1210,7 @@ fn add_sanitizer_libraries(
12101210
return;
12111211
}
12121212

1213-
if matches!(crate_type, CrateType::Rlib | CrateType::Staticlib) {
1213+
if matches!(crate_type, CrateType::Rlib | CrateType::StaticLib) {
12141214
return;
12151215
}
12161216

compiler/rustc_codegen_ssa/src/back/linker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1857,7 +1857,7 @@ pub(crate) fn linked_symbols(
18571857
| CrateType::Cdylib
18581858
| CrateType::Dylib
18591859
| CrateType::Sdylib => (),
1860-
CrateType::Staticlib | CrateType::Rlib => {
1860+
CrateType::StaticLib | CrateType::Rlib => {
18611861
// These are not linked, so no need to generate symbols.o for them.
18621862
return Vec::new();
18631863
}

compiler/rustc_codegen_ssa/src/back/lto.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ fn crate_type_allows_lto(crate_type: CrateType) -> bool {
6666
match crate_type {
6767
CrateType::Executable
6868
| CrateType::Dylib
69-
| CrateType::Staticlib
69+
| CrateType::StaticLib
7070
| CrateType::Cdylib
7171
| CrateType::ProcMacro
7272
| CrateType::Sdylib => true,

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn threshold(tcx: TyCtxt<'_>) -> SymbolExportLevel {
2828

2929
fn crate_export_threshold(crate_type: CrateType) -> SymbolExportLevel {
3030
match crate_type {
31-
CrateType::Executable | CrateType::Staticlib | CrateType::ProcMacro | CrateType::Cdylib => {
31+
CrateType::Executable | CrateType::StaticLib | CrateType::ProcMacro | CrateType::Cdylib => {
3232
SymbolExportLevel::C
3333
}
3434
CrateType::Rlib | CrateType::Dylib | CrateType::Sdylib => SymbolExportLevel::Rust,

compiler/rustc_codegen_ssa/src/base.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,7 @@ impl CrateInfo {
10111011
info.linked_symbols
10121012
.iter_mut()
10131013
.filter(|(crate_type, _)| {
1014-
!matches!(crate_type, CrateType::Rlib | CrateType::Staticlib)
1014+
!matches!(crate_type, CrateType::Rlib | CrateType::StaticLib)
10151015
})
10161016
.for_each(|(_, linked_symbols)| {
10171017
let mut symbols = missing_weak_lang_items
@@ -1043,7 +1043,7 @@ impl CrateInfo {
10431043
// this is a rare use case and we don't want to slow down the common case.
10441044
false
10451045
}
1046-
CrateType::Staticlib | CrateType::Rlib => {
1046+
CrateType::StaticLib | CrateType::Rlib => {
10471047
// We don't invoke the linker for these, so we don't need to collect the NatVis for
10481048
// them.
10491049
false

compiler/rustc_codegen_ssa/src/traits/backend.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub trait CodegenBackend {
6767
CrateType::Executable,
6868
CrateType::Dylib,
6969
CrateType::Rlib,
70-
CrateType::Staticlib,
70+
CrateType::StaticLib,
7171
CrateType::Cdylib,
7272
CrateType::ProcMacro,
7373
CrateType::Sdylib,

compiler/rustc_driver_impl/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ use rustc_session::config::{
5555
};
5656
use rustc_session::getopts::{self, Matches};
5757
use rustc_session::lint::{Lint, LintId};
58-
use rustc_session::output::{CRATE_TYPES, collect_crate_types, invalid_output_for_target};
58+
use rustc_session::output::{collect_crate_types, invalid_output_for_target};
5959
use rustc_session::{EarlyDiagCtxt, Session, config};
6060
use rustc_span::FileName;
6161
use rustc_span::def_id::LOCAL_CRATE;
@@ -848,7 +848,7 @@ fn print_crate_info(
848848
}
849849
}
850850
SupportedCrateTypes => {
851-
let supported_crate_types = CRATE_TYPES
851+
let supported_crate_types = CrateType::all()
852852
.iter()
853853
.filter(|(_, crate_type)| !invalid_output_for_target(sess, *crate_type))
854854
.filter(|(_, crate_type)| *crate_type != CrateType::Sdylib)

0 commit comments

Comments
 (0)