Skip to content

Commit 1e90a73

Browse files
Auto merge of #149363 - scrabsha:rust/sasha/vkknqylzvzlu, r=<try>
Port the `#![windows_subsystem]` attribute to the new attribute system try-job: x86_64-msvc
2 parents 1be6b13 + 2ab2090 commit 1e90a73

File tree

17 files changed

+203
-137
lines changed

17 files changed

+203
-137
lines changed

compiler/rustc_attr_parsing/src/attributes/crate_level.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use rustc_hir::attrs::WindowsSubsystemKind;
2+
13
use super::prelude::*;
24

35
pub(crate) struct CrateNameParser;
@@ -142,3 +144,34 @@ impl<S: Stage> NoArgsAttributeParser<S> for RustcCoherenceIsCoreParser {
142144
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::CrateLevel;
143145
const CREATE: fn(Span) -> AttributeKind = AttributeKind::RustcCoherenceIsCore;
144146
}
147+
148+
pub(crate) struct WindowsSubsystemParser;
149+
150+
impl<S: Stage> SingleAttributeParser<S> for WindowsSubsystemParser {
151+
const PATH: &[Symbol] = &[sym::windows_subsystem];
152+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
153+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
154+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::CrateLevel;
155+
const TEMPLATE: AttributeTemplate = template!(NameValueStr: ["windows", "console"], "https://doc.rust-lang.org/reference/runtime.html#the-windows_subsystem-attribute");
156+
157+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
158+
let Some(nv) = args.name_value() else {
159+
cx.expected_name_value(
160+
args.span().unwrap_or(cx.inner_span),
161+
Some(sym::windows_subsystem),
162+
);
163+
return None;
164+
};
165+
166+
let kind = match nv.value_as_str() {
167+
Some(sym::console) => WindowsSubsystemKind::Console,
168+
Some(sym::windows) => WindowsSubsystemKind::Windows,
169+
Some(_) | None => {
170+
cx.expected_specific_argument_strings(nv.value_span, &[sym::console, sym::windows]);
171+
return None;
172+
}
173+
};
174+
175+
Some(AttributeKind::WindowsSubsystem(kind, cx.attr_span))
176+
}
177+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use crate::attributes::confusables::ConfusablesParser;
2828
use crate::attributes::crate_level::{
2929
CrateNameParser, MoveSizeLimitParser, NoCoreParser, NoStdParser, PatternComplexityLimitParser,
3030
RecursionLimitParser, RustcCoherenceIsCoreParser, TypeLengthLimitParser,
31+
WindowsSubsystemParser,
3132
};
3233
use crate::attributes::debugger::DebuggerViualizerParser;
3334
use crate::attributes::deprecation::DeprecationParser;
@@ -211,6 +212,7 @@ attribute_parsers!(
211212
Single<SkipDuringMethodDispatchParser>,
212213
Single<TransparencyParser>,
213214
Single<TypeLengthLimitParser>,
215+
Single<WindowsSubsystemParser>,
214216
Single<WithoutArgs<AllowIncoherentImplParser>>,
215217
Single<WithoutArgs<AllowInternalUnsafeParser>>,
216218
Single<WithoutArgs<AsPtrParser>>,

compiler/rustc_codegen_ssa/messages.ftl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,6 @@ codegen_ssa_invalid_monomorphization_unsupported_symbol = invalid monomorphizati
169169
170170
codegen_ssa_invalid_monomorphization_unsupported_symbol_of_size = invalid monomorphization of `{$name}` intrinsic: unsupported {$symbol} from `{$in_ty}` with element `{$in_elem}` of size `{$size}` to `{$ret_ty}`
171171
172-
codegen_ssa_invalid_windows_subsystem = invalid windows subsystem `{$subsystem}`, only `windows` and `console` are allowed
173-
174172
codegen_ssa_ld64_unimplemented_modifier = `as-needed` modifier not implemented yet for ld64
175173
176174
codegen_ssa_lib_def_write_failure = failed to write lib.def file: {$error}

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2558,7 +2558,7 @@ fn add_order_independent_options(
25582558
&& sess.target.is_like_windows
25592559
&& let Some(s) = &codegen_results.crate_info.windows_subsystem
25602560
{
2561-
cmd.subsystem(s);
2561+
cmd.windows_subsystem(*s);
25622562
}
25632563

25642564
// Try to strip as much out of the generated object by removing unused

compiler/rustc_codegen_ssa/src/back/linker.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::path::{Path, PathBuf};
55
use std::{env, io, iter, mem, str};
66

77
use find_msvc_tools;
8+
use rustc_hir::attrs::WindowsSubsystemKind;
89
use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
910
use rustc_metadata::{
1011
find_native_static_library, try_find_native_dynamic_library, try_find_native_static_library,
@@ -345,7 +346,7 @@ pub(crate) trait Linker {
345346
crate_type: CrateType,
346347
symbols: &[(String, SymbolExportKind)],
347348
);
348-
fn subsystem(&mut self, subsystem: &str);
349+
fn windows_subsystem(&mut self, subsystem: WindowsSubsystemKind);
349350
fn linker_plugin_lto(&mut self);
350351
fn add_eh_frame_header(&mut self) {}
351352
fn add_no_exec(&mut self) {}
@@ -884,8 +885,8 @@ impl<'a> Linker for GccLinker<'a> {
884885
}
885886
}
886887

887-
fn subsystem(&mut self, subsystem: &str) {
888-
self.link_args(&["--subsystem", subsystem]);
888+
fn windows_subsystem(&mut self, subsystem: WindowsSubsystemKind) {
889+
self.link_args(&["--subsystem", subsystem.as_str()]);
889890
}
890891

891892
fn reset_per_library_state(&mut self) {
@@ -1159,9 +1160,8 @@ impl<'a> Linker for MsvcLinker<'a> {
11591160
self.link_arg(&arg);
11601161
}
11611162

1162-
fn subsystem(&mut self, subsystem: &str) {
1163-
// Note that previous passes of the compiler validated this subsystem,
1164-
// so we just blindly pass it to the linker.
1163+
fn windows_subsystem(&mut self, subsystem: WindowsSubsystemKind) {
1164+
let subsystem = subsystem.as_str();
11651165
self.link_arg(&format!("/SUBSYSTEM:{subsystem}"));
11661166

11671167
// Windows has two subsystems we're interested in right now, the console
@@ -1307,7 +1307,7 @@ impl<'a> Linker for EmLinker<'a> {
13071307
self.cc_arg(arg);
13081308
}
13091309

1310-
fn subsystem(&mut self, _subsystem: &str) {
1310+
fn windows_subsystem(&mut self, _subsystem: WindowsSubsystemKind) {
13111311
// noop
13121312
}
13131313

@@ -1444,7 +1444,7 @@ impl<'a> Linker for WasmLd<'a> {
14441444
}
14451445
}
14461446

1447-
fn subsystem(&mut self, _subsystem: &str) {}
1447+
fn windows_subsystem(&mut self, _subsystem: WindowsSubsystemKind) {}
14481448

14491449
fn linker_plugin_lto(&mut self) {
14501450
match self.sess.opts.cg.linker_plugin_lto {
@@ -1566,7 +1566,8 @@ impl<'a> Linker for L4Bender<'a> {
15661566
self.sess.dcx().emit_warn(errors::L4BenderExportingSymbolsUnimplemented);
15671567
}
15681568

1569-
fn subsystem(&mut self, subsystem: &str) {
1569+
fn windows_subsystem(&mut self, subsystem: WindowsSubsystemKind) {
1570+
let subsystem = subsystem.as_str();
15701571
self.link_arg(&format!("--subsystem {subsystem}"));
15711572
}
15721573

@@ -1735,7 +1736,7 @@ impl<'a> Linker for AixLinker<'a> {
17351736
self.link_arg(format!("-bE:{}", path.to_str().unwrap()));
17361737
}
17371738

1738-
fn subsystem(&mut self, _subsystem: &str) {}
1739+
fn windows_subsystem(&mut self, _subsystem: WindowsSubsystemKind) {}
17391740

17401741
fn reset_per_library_state(&mut self) {
17411742
self.hint_dynamic();
@@ -1969,7 +1970,7 @@ impl<'a> Linker for PtxLinker<'a> {
19691970
) {
19701971
}
19711972

1972-
fn subsystem(&mut self, _subsystem: &str) {}
1973+
fn windows_subsystem(&mut self, _subsystem: WindowsSubsystemKind) {}
19731974

19741975
fn linker_plugin_lto(&mut self) {}
19751976
}
@@ -2050,7 +2051,7 @@ impl<'a> Linker for LlbcLinker<'a> {
20502051
}
20512052
}
20522053

2053-
fn subsystem(&mut self, _subsystem: &str) {}
2054+
fn windows_subsystem(&mut self, _subsystem: WindowsSubsystemKind) {}
20542055

20552056
fn linker_plugin_lto(&mut self) {}
20562057
}
@@ -2134,7 +2135,7 @@ impl<'a> Linker for BpfLinker<'a> {
21342135
}
21352136
}
21362137

2137-
fn subsystem(&mut self, _subsystem: &str) {}
2138+
fn windows_subsystem(&mut self, _subsystem: WindowsSubsystemKind) {}
21382139

21392140
fn linker_plugin_lto(&mut self) {}
21402141
}

compiler/rustc_codegen_ssa/src/base.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@ use std::time::{Duration, Instant};
55

66
use itertools::Itertools;
77
use rustc_abi::FIRST_VARIANT;
8-
use rustc_ast as ast;
98
use rustc_ast::expand::allocator::{
109
ALLOC_ERROR_HANDLER, ALLOCATOR_METHODS, AllocatorKind, AllocatorMethod, AllocatorTy,
1110
};
1211
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
1312
use rustc_data_structures::profiling::{get_resident_set_size, print_time_passes_entry};
1413
use rustc_data_structures::sync::{IntoDynSyncSend, par_map};
1514
use rustc_data_structures::unord::UnordMap;
16-
use rustc_hir::attrs::{DebuggerVisualizerType, OptimizeAttr};
17-
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
15+
use rustc_hir::attrs::{AttributeKind, DebuggerVisualizerType, OptimizeAttr};
16+
use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LOCAL_CRATE};
1817
use rustc_hir::lang_items::LangItem;
19-
use rustc_hir::{ItemId, Target};
18+
use rustc_hir::{ItemId, Target, find_attr};
2019
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
2120
use rustc_middle::middle::debugger_visualizer::DebuggerVisualizerFile;
2221
use rustc_middle::middle::dependency_format::Dependencies;
@@ -31,7 +30,7 @@ use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
3130
use rustc_middle::{bug, span_bug};
3231
use rustc_session::Session;
3332
use rustc_session::config::{self, CrateType, EntryFnType};
34-
use rustc_span::{DUMMY_SP, Symbol, sym};
33+
use rustc_span::{DUMMY_SP, Symbol};
3534
use rustc_symbol_mangling::mangle_internal_symbol;
3635
use rustc_target::spec::{Arch, Os};
3736
use rustc_trait_selection::infer::{BoundRegionConversionTime, TyCtxtInferExt};
@@ -896,15 +895,7 @@ impl CrateInfo {
896895
let linked_symbols =
897896
crate_types.iter().map(|&c| (c, crate::back::linker::linked_symbols(tcx, c))).collect();
898897
let local_crate_name = tcx.crate_name(LOCAL_CRATE);
899-
let crate_attrs = tcx.hir_attrs(rustc_hir::CRATE_HIR_ID);
900-
let subsystem =
901-
ast::attr::first_attr_value_str_by_name(crate_attrs, sym::windows_subsystem);
902-
let windows_subsystem = subsystem.map(|subsystem| {
903-
if subsystem != sym::windows && subsystem != sym::console {
904-
tcx.dcx().emit_fatal(errors::InvalidWindowsSubsystem { subsystem });
905-
}
906-
subsystem.to_string()
907-
});
898+
let windows_subsystem = find_attr!(tcx.get_all_attrs(CRATE_DEF_ID), AttributeKind::WindowsSubsystem(kind, _) => *kind);
908899

909900
// This list is used when generating the command line to pass through to
910901
// system linker. The linker expects undefined symbols on the left of the

compiler/rustc_codegen_ssa/src/errors.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -749,12 +749,6 @@ pub(crate) struct MultipleMainFunctions {
749749
pub span: Span,
750750
}
751751

752-
#[derive(Diagnostic)]
753-
#[diag(codegen_ssa_invalid_windows_subsystem)]
754-
pub(crate) struct InvalidWindowsSubsystem {
755-
pub subsystem: Symbol,
756-
}
757-
758752
#[derive(Diagnostic)]
759753
#[diag(codegen_ssa_shuffle_indices_evaluation)]
760754
pub(crate) struct ShuffleIndicesEvaluation {

compiler/rustc_codegen_ssa/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use std::sync::Arc;
2424
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
2525
use rustc_data_structures::unord::UnordMap;
2626
use rustc_hir::CRATE_HIR_ID;
27-
use rustc_hir::attrs::{CfgEntry, NativeLibKind};
27+
use rustc_hir::attrs::{CfgEntry, NativeLibKind, WindowsSubsystemKind};
2828
use rustc_hir::def_id::CrateNum;
2929
use rustc_macros::{Decodable, Encodable, HashStable};
3030
use rustc_metadata::EncodedMetadata;
@@ -225,7 +225,7 @@ pub struct CrateInfo {
225225
pub used_crate_source: UnordMap<CrateNum, Arc<CrateSource>>,
226226
pub used_crates: Vec<CrateNum>,
227227
pub dependency_formats: Arc<Dependencies>,
228-
pub windows_subsystem: Option<String>,
228+
pub windows_subsystem: Option<WindowsSubsystemKind>,
229229
pub natvis_debugger_visualizers: BTreeSet<DebuggerVisualizerFile>,
230230
pub lint_levels: CodegenLintLevels,
231231
pub metadata_symbol: String,

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,22 @@ pub enum RtsanSetting {
404404
Caller,
405405
}
406406

407+
#[derive(Eq, PartialEq, Debug, Copy, Clone)]
408+
#[derive(Encodable, Decodable, HashStable_Generic, PrintAttribute)]
409+
pub enum WindowsSubsystemKind {
410+
Console,
411+
Windows,
412+
}
413+
414+
impl WindowsSubsystemKind {
415+
pub fn as_str(&self) -> &'static str {
416+
match self {
417+
WindowsSubsystemKind::Console => "console",
418+
WindowsSubsystemKind::Windows => "windows",
419+
}
420+
}
421+
}
422+
407423
/// Represents parsed *built-in* inert attributes.
408424
///
409425
/// ## Overview
@@ -759,5 +775,8 @@ pub enum AttributeKind {
759775

760776
/// Represents `#[used]`
761777
Used { used_by: UsedBy, span: Span },
778+
779+
/// Represents `#[windows_subsystem]`.
780+
WindowsSubsystem(WindowsSubsystemKind, Span),
762781
// tidy-alphabetical-end
763782
}

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ impl AttributeKind {
106106
UnsafeSpecializationMarker(..) => No,
107107
UnstableFeatureBound(..) => No,
108108
Used { .. } => No,
109+
WindowsSubsystem(..) => No,
109110
// tidy-alphabetical-end
110111
}
111112
}

0 commit comments

Comments
 (0)