@@ -402,7 +402,7 @@ impl LinkSelfContained {
402402 }
403403}
404404
405- /// The different values that `-Z linker-features` can take on the CLI: a list of individually
405+ /// The different values that `-C linker-features` can take on the CLI: a list of individually
406406/// enabled or disabled features used during linking.
407407///
408408/// There is no need to enable or disable them in bulk. Each feature is fine-grained, and can be
@@ -442,6 +442,39 @@ impl LinkerFeaturesCli {
442442 _ => None ,
443443 }
444444 }
445+
446+ /// When *not* using `-Z unstable-options` on the CLI, ensure only stable linker features are
447+ /// used, for the given `TargetTuple`. Returns `Ok` if no unstable variants are used.
448+ /// The caller should ensure that e.g. `nightly_options::is_unstable_enabled()`
449+ /// returns false.
450+ pub ( crate ) fn check_unstable_variants ( & self , target_tuple : & TargetTuple ) -> Result < ( ) , String > {
451+ // `-C linker-features=-lld` is only stable on x64 linux.
452+ let has_minus_lld = self . disabled . is_lld_enabled ( ) ;
453+ if has_minus_lld && target_tuple. tuple ( ) != "x86_64-unknown-linux-gnu" {
454+ return Err ( format ! (
455+ "`-C linker-features=-lld` is unstable on the `{target_tuple}` \
456+ target. The `-Z unstable-options` flag must also be passed to use it on this target",
457+ ) ) ;
458+ }
459+
460+ // Any `+lld` or non-lld feature used is unstable, and that's an error.
461+ let unstable_enabled = self . enabled ;
462+ let unstable_disabled = self . disabled - LinkerFeatures :: LLD ;
463+ if !unstable_enabled. union ( unstable_disabled) . is_empty ( ) {
464+ let unstable_features: Vec < _ > = unstable_enabled
465+ . iter ( )
466+ . map ( |f| format ! ( "+{}" , f. as_str( ) . unwrap( ) ) )
467+ . chain ( unstable_disabled. iter ( ) . map ( |f| format ! ( "-{}" , f. as_str( ) . unwrap( ) ) ) )
468+ . collect ( ) ;
469+ return Err ( format ! (
470+ "`-C linker-features={}` is unstable, and also requires the \
471+ `-Z unstable-options` flag to be used",
472+ unstable_features. join( "," ) ,
473+ ) ) ;
474+ }
475+
476+ Ok ( ( ) )
477+ }
445478}
446479
447480/// Used with `-Z assert-incr-state`.
@@ -2638,9 +2671,8 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
26382671 }
26392672 }
26402673
2641- if !nightly_options:: is_unstable_enabled ( matches)
2642- && cg. force_frame_pointers == FramePointer :: NonLeaf
2643- {
2674+ let unstable_options_enabled = nightly_options:: is_unstable_enabled ( matches) ;
2675+ if !unstable_options_enabled && cg. force_frame_pointers == FramePointer :: NonLeaf {
26442676 early_dcx. early_fatal (
26452677 "`-Cforce-frame-pointers=non-leaf` or `always` also requires `-Zunstable-options` \
26462678 and a nightly compiler",
@@ -2650,7 +2682,7 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
26502682 // For testing purposes, until we have more feedback about these options: ensure `-Z
26512683 // unstable-options` is required when using the unstable `-C link-self-contained` and `-C
26522684 // linker-flavor` options.
2653- if !nightly_options :: is_unstable_enabled ( matches ) {
2685+ if !unstable_options_enabled {
26542686 let uses_unstable_self_contained_option =
26552687 cg. link_self_contained . are_unstable_variants_set ( ) ;
26562688 if uses_unstable_self_contained_option {
@@ -2706,6 +2738,12 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
27062738 let debuginfo = select_debuginfo ( matches, & cg) ;
27072739 let debuginfo_compression = unstable_opts. debuginfo_compression ;
27082740
2741+ if !unstable_options_enabled {
2742+ if let Err ( error) = cg. linker_features . check_unstable_variants ( & target_triple) {
2743+ early_dcx. early_fatal ( error) ;
2744+ }
2745+ }
2746+
27092747 let crate_name = matches. opt_str ( "crate-name" ) ;
27102748 let unstable_features = UnstableFeatures :: from_environment ( crate_name. as_deref ( ) ) ;
27112749 // Parse any `-l` flags, which link to native libraries.
0 commit comments