Skip to content

Commit 105b934

Browse files
Rollup merge of rust-lang#151442 - clubby789:crate-type-port, r=JonathanBrouwer
Port `#![crate_type]` to the attribute parser Tracking issue: rust-lang#131229 ~~Note that the actual parsing that is used in the compiler session is unchanged, as it must happen very early on; this just ports the validation logic.~~ Also added `// tidy-alphabetical-start` to `check_attr.rs` to make it a bit less conflict-prone
2 parents 3287212 + e73dfaa commit 105b934

45 files changed

Lines changed: 627 additions & 466 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: 55 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,56 @@ 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+
let ArgParser::NameValue(n) = args else {
50+
cx.expected_name_value(cx.attr_span, None);
51+
return None;
52+
};
53+
54+
let Some(crate_type) = n.value_as_str() else {
55+
cx.expected_string_literal(n.value_span, Some(n.value_as_lit()));
56+
return None;
57+
};
58+
59+
let Ok(crate_type) = crate_type.try_into() else {
60+
// We don't error on invalid `#![crate_type]` when not applied to a crate
61+
if cx.shared.target == Target::Crate {
62+
let candidate = find_best_match_for_name(
63+
&CrateType::all_stable().iter().map(|(name, _)| *name).collect::<Vec<_>>(),
64+
crate_type,
65+
None,
66+
);
67+
cx.emit_lint(
68+
UNKNOWN_CRATE_TYPES,
69+
AttributeLintKind::CrateTypeUnknown {
70+
span: n.value_span,
71+
suggested: candidate,
72+
},
73+
n.value_span,
74+
);
75+
}
76+
return None;
77+
};
78+
79+
Some(crate_type)
80+
}
81+
}
82+
2983
pub(crate) struct RecursionLimitParser;
3084

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

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ use crate::attributes::codegen_attrs::{
2828
};
2929
use crate::attributes::confusables::ConfusablesParser;
3030
use crate::attributes::crate_level::{
31-
CrateNameParser, MoveSizeLimitParser, NeedsPanicRuntimeParser, NoBuiltinsParser, NoCoreParser,
32-
NoMainParser, NoStdParser, PanicRuntimeParser, PatternComplexityLimitParser,
33-
ProfilerRuntimeParser, RecursionLimitParser, RustcCoherenceIsCoreParser, TypeLengthLimitParser,
34-
WindowsSubsystemParser,
31+
CrateNameParser, CrateTypeParser, MoveSizeLimitParser, NeedsPanicRuntimeParser,
32+
NoBuiltinsParser, NoCoreParser, NoMainParser, NoStdParser, PanicRuntimeParser,
33+
PatternComplexityLimitParser, ProfilerRuntimeParser, RecursionLimitParser,
34+
RustcCoherenceIsCoreParser, TypeLengthLimitParser, WindowsSubsystemParser,
3535
};
3636
use crate::attributes::debugger::DebuggerViualizerParser;
3737
use crate::attributes::deprecation::DeprecationParser;
@@ -193,6 +193,7 @@ attribute_parsers!(
193193
// tidy-alphabetical-start
194194
Combine<AllowConstFnUnstableParser>,
195195
Combine<AllowInternalUnstableParser>,
196+
Combine<CrateTypeParser>,
196197
Combine<DebuggerViualizerParser>,
197198
Combine<ForceTargetFeatureParser>,
198199
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: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ use rustc_feature::find_gated_cfg;
4242
// `rust_index` isn't used in this crate's code, but it must be named in the
4343
// `Cargo.toml` for the `rustc_randomized_layouts` feature.
4444
use rustc_index as _;
45+
use rustc_interface::passes::collect_crate_types;
4546
use rustc_interface::util::{self, get_codegen_backend};
4647
use rustc_interface::{Linker, create_and_enter_global_ctxt, interface, passes};
4748
use rustc_lint::unerased_lint_store;
@@ -56,10 +57,10 @@ use rustc_session::config::{
5657
};
5758
use rustc_session::getopts::{self, Matches};
5859
use rustc_session::lint::{Lint, LintId};
59-
use rustc_session::output::{CRATE_TYPES, collect_crate_types, invalid_output_for_target};
60+
use rustc_session::output::invalid_output_for_target;
6061
use rustc_session::{EarlyDiagCtxt, Session, config};
61-
use rustc_span::FileName;
6262
use rustc_span::def_id::LOCAL_CRATE;
63+
use rustc_span::{DUMMY_SP, FileName};
6364
use rustc_target::json::ToJson;
6465
use rustc_target::spec::{Target, TargetTuple};
6566
use tracing::trace;
@@ -698,6 +699,7 @@ fn print_crate_info(
698699
&codegen_backend.supported_crate_types(sess),
699700
codegen_backend.name(),
700701
attrs,
702+
DUMMY_SP,
701703
);
702704
for &style in &crate_types {
703705
let fname = rustc_session::output::filename_for_input(
@@ -849,7 +851,7 @@ fn print_crate_info(
849851
}
850852
}
851853
SupportedCrateTypes => {
852-
let supported_crate_types = CRATE_TYPES
854+
let supported_crate_types = CrateType::all()
853855
.iter()
854856
.filter(|(_, crate_type)| !invalid_output_for_target(sess, *crate_type))
855857
.filter(|(_, crate_type)| *crate_type != CrateType::Sdylib)

0 commit comments

Comments
 (0)