Skip to content

Commit 1c820c0

Browse files
committed
Port crate_type to attribute parser
1 parent 02ab26a commit 1c820c0

41 files changed

Lines changed: 354 additions & 275 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: 60 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,61 @@ 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 =
43+
template!(NameValueStr: "crate type", "https://doc.rust-lang.org/reference/linkage.html");
44+
45+
fn extend(
46+
cx: &mut AcceptContext<'_, '_, S>,
47+
args: &ArgParser,
48+
) -> impl IntoIterator<Item = Self::Item> {
49+
// We don't error on malformed `#![crate_type]` when not applied to a crate
50+
let report_errors = cx.shared.target == Target::Crate;
51+
52+
let ArgParser::NameValue(n) = args else {
53+
if report_errors {
54+
cx.expected_name_value(cx.attr_span, None);
55+
}
56+
return None;
57+
};
58+
59+
let Some(crate_type) = n.value_as_str() else {
60+
if report_errors {
61+
cx.expected_string_literal(n.value_span, Some(n.value_as_lit()));
62+
}
63+
return None;
64+
};
65+
let Ok(crate_type) = crate_type.try_into() else {
66+
if report_errors {
67+
let candidate = find_best_match_for_name(
68+
&CrateType::all_stable().iter().map(|(name, _)| *name).collect::<Vec<_>>(),
69+
crate_type,
70+
None,
71+
);
72+
cx.emit_lint(
73+
UNKNOWN_CRATE_TYPES,
74+
AttributeLintKind::CrateTypeUnknown {
75+
span: n.value_span,
76+
suggested: candidate,
77+
},
78+
n.value_span,
79+
);
80+
}
81+
return None;
82+
};
83+
84+
Some(crate_type)
85+
}
86+
}
87+
2988
pub(crate) struct RecursionLimitParser;
3089

3190
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
@@ -67,7 +67,7 @@ fn crate_type_allows_lto(crate_type: CrateType) -> bool {
6767
match crate_type {
6868
CrateType::Executable
6969
| CrateType::Dylib
70-
| CrateType::Staticlib
70+
| CrateType::StaticLib
7171
| CrateType::Cdylib
7272
| CrateType::ProcMacro
7373
| 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
@@ -1009,7 +1009,7 @@ impl CrateInfo {
10091009
info.linked_symbols
10101010
.iter_mut()
10111011
.filter(|(crate_type, _)| {
1012-
!matches!(crate_type, CrateType::Rlib | CrateType::Staticlib)
1012+
!matches!(crate_type, CrateType::Rlib | CrateType::StaticLib)
10131013
})
10141014
.for_each(|(_, linked_symbols)| {
10151015
let mut symbols = missing_weak_lang_items
@@ -1041,7 +1041,7 @@ impl CrateInfo {
10411041
// this is a rare use case and we don't want to slow down the common case.
10421042
false
10431043
}
1044-
CrateType::Staticlib | CrateType::Rlib => {
1044+
CrateType::StaticLib | CrateType::Rlib => {
10451045
// We don't invoke the linker for these, so we don't need to collect the NatVis for
10461046
// them.
10471047
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
@@ -56,7 +56,7 @@ use rustc_session::config::{
5656
};
5757
use rustc_session::getopts::{self, Matches};
5858
use rustc_session::lint::{Lint, LintId};
59-
use rustc_session::output::{CRATE_TYPES, collect_crate_types, invalid_output_for_target};
59+
use rustc_session::output::{collect_crate_types, invalid_output_for_target};
6060
use rustc_session::{EarlyDiagCtxt, Session, config};
6161
use rustc_span::FileName;
6262
use rustc_span::def_id::LOCAL_CRATE;
@@ -849,7 +849,7 @@ fn print_crate_info(
849849
}
850850
}
851851
SupportedCrateTypes => {
852-
let supported_crate_types = CRATE_TYPES
852+
let supported_crate_types = CrateType::all()
853853
.iter()
854854
.filter(|(_, crate_type)| !invalid_output_for_target(sess, *crate_type))
855855
.filter(|(_, crate_type)| *crate_type != CrateType::Sdylib)

0 commit comments

Comments
 (0)