From d5c756acf38aed410b885466559e9ba7d6850cf6 Mon Sep 17 00:00:00 2001 From: CoCo-Japan-pan <115922543+CoCo-Japan-pan@users.noreply.github.com> Date: Sun, 22 Mar 2026 13:38:34 +0900 Subject: [PATCH 01/25] Lower `impl` restriction to HIR --- compiler/rustc_ast_lowering/src/item.rs | 37 +++++++++++++++++-- compiler/rustc_ast_lowering/src/path.rs | 17 +++++++++ compiler/rustc_hir/src/hir.rs | 27 ++++++++++++-- compiler/rustc_hir/src/intravisit.rs | 1 + .../src/collect/predicates_of.rs | 4 +- .../src/collect/resolve_bound_vars.rs | 2 +- compiler/rustc_hir_pretty/src/lib.rs | 16 ++++++++ .../rustc_hir_typeck/src/method/suggest.rs | 4 +- .../src/multiple_supertrait_upcastable.rs | 2 +- compiler/rustc_middle/src/hir/map.rs | 2 +- compiler/rustc_passes/src/check_attr.rs | 4 +- .../src/error_reporting/traits/mod.rs | 2 +- .../src/error_reporting/traits/suggestions.rs | 10 ++--- compiler/rustc_trait_selection/src/errors.rs | 2 +- src/librustdoc/clean/mod.rs | 2 +- .../src/arbitrary_source_item_ordering.rs | 2 +- .../clippy_lints/src/item_name_repetitions.rs | 2 +- .../clippy_lints/src/len_without_is_empty.rs | 2 +- .../clippy/clippy_lints/src/missing_doc.rs | 2 +- .../clippy/clippy_lints/src/trait_bounds.rs | 4 +- .../clippy_lints/src/upper_case_acronyms.rs | 2 +- 21 files changed, 115 insertions(+), 31 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index fa103099e643f..4c8d34f1bd779 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -536,14 +536,14 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> { constness, is_auto, safety, - // FIXME(impl_restrictions): lower to HIR - impl_restriction: _, + impl_restriction, ident, generics, bounds, items, }) => { let constness = self.lower_constness(*constness); + let impl_restriction = self.lower_impl_restriction(impl_restriction); let ident = self.lower_ident(*ident); let (generics, (safety, items, bounds)) = self.lower_generics( generics, @@ -562,7 +562,16 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> { (safety, items, bounds) }, ); - hir::ItemKind::Trait(constness, *is_auto, safety, ident, generics, bounds, items) + hir::ItemKind::Trait( + constness, + *is_auto, + safety, + impl_restriction, + ident, + generics, + bounds, + items, + ) } ItemKind::TraitAlias(box TraitAlias { constness, ident, generics, bounds }) => { let constness = self.lower_constness(*constness); @@ -1827,6 +1836,28 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> { } } + pub(super) fn lower_impl_restriction( + &mut self, + r: &ImplRestriction, + ) -> &'hir hir::ImplRestriction<'hir> { + let kind = match &r.kind { + RestrictionKind::Unrestricted => hir::RestrictionKind::Unrestricted, + RestrictionKind::Restricted { path, id, shorthand } => { + let res = self.resolver.get_partial_res(*id); + if let Some(did) = res.and_then(|res| res.expect_full_res().opt_def_id()) { + hir::RestrictionKind::Restricted { + path: self.lower_mod_path(did, path), + shorthand: *shorthand, + } + } else { + self.dcx().span_delayed_bug(path.span, "should have errored in resolve"); + hir::RestrictionKind::Unrestricted + } + } + }; + self.arena.alloc(hir::ImplRestriction { kind, span: self.lower_span(r.span) }) + } + /// Return the pair of the lowered `generics` as `hir::Generics` and the evaluation of `f` with /// the carried impl trait definitions and bounds. #[instrument(level = "debug", skip(self, f))] diff --git a/compiler/rustc_ast_lowering/src/path.rs b/compiler/rustc_ast_lowering/src/path.rs index 139140af3e033..cf0ef34a40515 100644 --- a/compiler/rustc_ast_lowering/src/path.rs +++ b/compiler/rustc_ast_lowering/src/path.rs @@ -250,6 +250,23 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> { }) } + pub(crate) fn lower_mod_path(&mut self, res: DefId, p: &Path) -> &'hir hir::ModPath<'hir> { + self.arena.alloc(hir::ModPath { + res, + segments: self.arena.alloc_from_iter(p.segments.iter().map(|segment| { + self.lower_path_segment( + p.span, + segment, + ParamMode::Explicit, + GenericArgsMode::Err, + ImplTraitContext::Disallowed(ImplTraitPosition::Path), + None, + ) + })), + span: self.lower_span(p.span), + }) + } + pub(crate) fn lower_path_segment( &mut self, path_span: Span, diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 57cf42cc54794..d612bdacec7d4 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -356,6 +356,9 @@ pub struct Path<'hir, R = Res> { /// Up to three resolutions for type, value and macro namespaces. pub type UsePath<'hir> = Path<'hir, PerNS>>; +/// Module paths. Used for restrictions. +pub type ModPath<'hir> = Path<'hir, DefId>; + impl Path<'_> { pub fn is_global(&self) -> bool { self.segments.first().is_some_and(|segment| segment.ident.name == kw::PathRoot) @@ -4326,13 +4329,14 @@ impl<'hir> Item<'hir> { Constness, IsAuto, Safety, + &'hir ImplRestriction<'hir>, Ident, &'hir Generics<'hir>, GenericBounds<'hir>, &'hir [TraitItemId] ), - ItemKind::Trait(constness, is_auto, safety, ident, generics, bounds, items), - (*constness, *is_auto, *safety, *ident, generics, bounds, items); + ItemKind::Trait(constness, is_auto, safety, impl_restriction, ident, generics, bounds, items), + (*constness, *is_auto, *safety, impl_restriction, *ident, generics, bounds, items); expect_trait_alias, (Constness, Ident, &'hir Generics<'hir>, GenericBounds<'hir>), ItemKind::TraitAlias(constness, ident, generics, bounds), (*constness, *ident, generics, bounds); @@ -4401,6 +4405,20 @@ impl fmt::Display for Constness { } } +#[derive(Debug, Clone, Copy, HashStable_Generic)] +pub struct ImplRestriction<'hir> { + pub kind: RestrictionKind<'hir>, + pub span: Span, +} + +#[derive(Debug, Clone, Copy, HashStable_Generic)] +pub enum RestrictionKind<'hir> { + /// The restriction does not affect the item. + Unrestricted, + /// The restriction only applies outside of this path. + Restricted { path: &'hir ModPath<'hir>, shorthand: bool }, +} + /// The actual safety specified in syntax. We may treat /// its safety different within the type system to create a /// "sound by default" system that needs checking this enum @@ -4513,6 +4531,7 @@ pub enum ItemKind<'hir> { Constness, IsAuto, Safety, + &'hir ImplRestriction<'hir>, Ident, &'hir Generics<'hir>, GenericBounds<'hir>, @@ -4563,7 +4582,7 @@ impl ItemKind<'_> { | ItemKind::Enum(ident, ..) | ItemKind::Struct(ident, ..) | ItemKind::Union(ident, ..) - | ItemKind::Trait(_, _, _, ident, ..) + | ItemKind::Trait(_, _, _, _, ident, ..) | ItemKind::TraitAlias(_, ident, ..) => Some(ident), ItemKind::Use(_, UseKind::Glob | UseKind::ListStem) @@ -4581,7 +4600,7 @@ impl ItemKind<'_> { | ItemKind::Enum(_, generics, _) | ItemKind::Struct(_, generics, _) | ItemKind::Union(_, generics, _) - | ItemKind::Trait(_, _, _, _, generics, _, _) + | ItemKind::Trait(_, _, _, _, _, generics, _, _) | ItemKind::TraitAlias(_, _, generics, _) | ItemKind::Impl(Impl { generics, .. }) => generics, _ => return None, diff --git a/compiler/rustc_hir/src/intravisit.rs b/compiler/rustc_hir/src/intravisit.rs index 25ef56f8b0f2c..ae99ca7002248 100644 --- a/compiler/rustc_hir/src/intravisit.rs +++ b/compiler/rustc_hir/src/intravisit.rs @@ -622,6 +622,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) -> V:: _constness, _is_auto, _safety, + _impl_restriction, ident, ref generics, bounds, diff --git a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs index 8fd3d631962c8..33d5151236be0 100644 --- a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs @@ -165,7 +165,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen Some(ty::Binder::dummy(tcx.impl_trait_ref(def_id).instantiate_identity())); } } - ItemKind::Trait(_, _, _, _, _, self_bounds, ..) + ItemKind::Trait(_, _, _, _, _, _, self_bounds, ..) | ItemKind::TraitAlias(_, _, _, self_bounds) => { is_trait = Some((self_bounds, item.span)); } @@ -1033,7 +1033,7 @@ pub(super) fn const_conditions<'tcx>( Node::Item(item) => match item.kind { hir::ItemKind::Impl(impl_) => (impl_.generics, None, false), hir::ItemKind::Fn { generics, .. } => (generics, None, false), - hir::ItemKind::Trait(_, _, _, _, generics, supertraits, _) => { + hir::ItemKind::Trait(_, _, _, _, _, generics, supertraits, _) => { (generics, Some((Some(item.owner_id.def_id), supertraits)), false) } hir::ItemKind::TraitAlias(_, _, generics, supertraits) => { diff --git a/compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs b/compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs index 5bb4166bf6cb3..e5dae6ea2157c 100644 --- a/compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs +++ b/compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs @@ -645,7 +645,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> { | hir::ItemKind::Enum(_, generics, _) | hir::ItemKind::Struct(_, generics, _) | hir::ItemKind::Union(_, generics, _) - | hir::ItemKind::Trait(_, _, _, _, generics, ..) + | hir::ItemKind::Trait(_, _, _, _, _, generics, ..) | hir::ItemKind::TraitAlias(_, _, generics, ..) | hir::ItemKind::Impl(hir::Impl { generics, .. }) => { // These kinds of items have only early-bound lifetime parameters. diff --git a/compiler/rustc_hir_pretty/src/lib.rs b/compiler/rustc_hir_pretty/src/lib.rs index 82540a9327410..07ee1118390eb 100644 --- a/compiler/rustc_hir_pretty/src/lib.rs +++ b/compiler/rustc_hir_pretty/src/lib.rs @@ -761,6 +761,7 @@ impl<'a> State<'a> { constness, is_auto, safety, + impl_restriction, ident, generics, bounds, @@ -770,6 +771,7 @@ impl<'a> State<'a> { self.print_constness(constness); self.print_is_auto(is_auto); self.print_safety(safety); + self.print_impl_restriction(impl_restriction); self.word_nbsp("trait"); self.print_ident(ident); self.print_generic_params(generics.params); @@ -2645,6 +2647,20 @@ impl<'a> State<'a> { hir::IsAuto::No => {} } } + + fn print_impl_restriction(&mut self, r: &hir::ImplRestriction<'_>) { + match r.kind { + hir::RestrictionKind::Unrestricted => {} + hir::RestrictionKind::Restricted { path, shorthand } => { + self.word("impl("); + if shorthand { + self.word_nbsp("in"); + } + self.print_path(path, false); + self.word(")"); + } + } + } } /// Does this expression require a semicolon to be treated diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index c5b3d7065fa92..dc0a55649039d 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -1889,7 +1889,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { Some( Node::Item(hir::Item { kind: - hir::ItemKind::Trait(_, _, _, ident, ..) + hir::ItemKind::Trait(_, _, _, _, ident, ..) | hir::ItemKind::TraitAlias(_, ident, ..), .. }) @@ -4533,7 +4533,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { return; } Node::Item(hir::Item { - kind: hir::ItemKind::Trait(_, _, _, ident, _, bounds, _), + kind: hir::ItemKind::Trait(_, _, _, _, ident, _, bounds, _), .. }) => { let (sp, sep, article) = if bounds.is_empty() { diff --git a/compiler/rustc_lint/src/multiple_supertrait_upcastable.rs b/compiler/rustc_lint/src/multiple_supertrait_upcastable.rs index 93f067d09833a..9177e00d902c8 100644 --- a/compiler/rustc_lint/src/multiple_supertrait_upcastable.rs +++ b/compiler/rustc_lint/src/multiple_supertrait_upcastable.rs @@ -39,7 +39,7 @@ impl<'tcx> LateLintPass<'tcx> for MultipleSupertraitUpcastable { let def_id = item.owner_id.to_def_id(); // NOTE(nbdd0121): use `dyn_compatibility_violations` instead of `is_dyn_compatible` because // the latter will report `where_clause_object_safety` lint. - if let hir::ItemKind::Trait(_, _, _, ident, ..) = item.kind + if let hir::ItemKind::Trait(_, _, _, _, ident, ..) = item.kind && cx.tcx.is_dyn_compatible(def_id) { let direct_super_traits_iter = cx diff --git a/compiler/rustc_middle/src/hir/map.rs b/compiler/rustc_middle/src/hir/map.rs index 499c6dae060bf..20aa0a809006f 100644 --- a/compiler/rustc_middle/src/hir/map.rs +++ b/compiler/rustc_middle/src/hir/map.rs @@ -945,7 +945,7 @@ impl<'tcx> TyCtxt<'tcx> { }) => until_within(*outer_span, ty.span), // With generics and bounds. Node::Item(Item { - kind: ItemKind::Trait(_, _, _, _, generics, bounds, _), + kind: ItemKind::Trait(_, _, _, _, _, generics, bounds, _), span: outer_span, .. }) diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index 6aeb0ae57e752..c201390fc5354 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -607,7 +607,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> { if let Some(directive) = directive { if let Node::Item(Item { - kind: ItemKind::Trait(_, _, _, trait_name, generics, _, _), + kind: ItemKind::Trait(_, _, _, _, trait_name, generics, _, _), .. }) = self.tcx.hir_node(hir_id) { @@ -1047,7 +1047,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> { match item.kind { ItemKind::Enum(_, generics, _) | ItemKind::Struct(_, generics, _) if generics.params.len() != 0 => {} - ItemKind::Trait(_, _, _, _, generics, _, items) + ItemKind::Trait(_, _, _, _, _, generics, _, items) if generics.params.len() != 0 || items.iter().any(|item| { matches!(self.tcx.def_kind(item.owner_id), DefKind::AssocTy) diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs index bda0c4fa2c6f6..d4d8607fe9288 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs @@ -574,7 +574,7 @@ pub fn report_dyn_incompatibility<'tcx>( let trait_str = tcx.def_path_str(trait_def_id); let trait_span = tcx.hir_get_if_local(trait_def_id).and_then(|node| match node { hir::Node::Item(item) => match item.kind { - hir::ItemKind::Trait(_, _, _, ident, ..) + hir::ItemKind::Trait(_, _, _, _, ident, ..) | hir::ItemKind::TraitAlias(_, ident, _, _) => Some(ident.span), _ => unreachable!(), }, diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs index 2d9574ea8c546..e55f8274351cf 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs @@ -271,7 +271,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { let node = self.tcx.hir_node_by_def_id(body_id); match node { hir::Node::Item(hir::Item { - kind: hir::ItemKind::Trait(_, _, _, ident, generics, bounds, _), + kind: hir::ItemKind::Trait(_, _, _, _, ident, generics, bounds, _), .. }) if self_ty == self.tcx.types.self_param => { assert!(param_ty); @@ -334,7 +334,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { } hir::Node::Item(hir::Item { kind: - hir::ItemKind::Trait(_, _, _, _, generics, ..) + hir::ItemKind::Trait(_, _, _, _, _, generics, ..) | hir::ItemKind::Impl(hir::Impl { generics, .. }), .. }) if projection.is_some() => { @@ -358,7 +358,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { hir::ItemKind::Struct(_, generics, _) | hir::ItemKind::Enum(_, generics, _) | hir::ItemKind::Union(_, generics, _) - | hir::ItemKind::Trait(_, _, _, _, generics, ..) + | hir::ItemKind::Trait(_, _, _, _, _, generics, ..) | hir::ItemKind::Impl(hir::Impl { generics, .. }) | hir::ItemKind::Fn { generics, .. } | hir::ItemKind::TyAlias(_, generics, _) @@ -438,7 +438,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { hir::ItemKind::Struct(_, generics, _) | hir::ItemKind::Enum(_, generics, _) | hir::ItemKind::Union(_, generics, _) - | hir::ItemKind::Trait(_, _, _, _, generics, ..) + | hir::ItemKind::Trait(_, _, _, _, _, generics, ..) | hir::ItemKind::Impl(hir::Impl { generics, .. }) | hir::ItemKind::Fn { generics, .. } | hir::ItemKind::TyAlias(_, generics, _) @@ -3618,7 +3618,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { let mut is_auto_trait = false; match tcx.hir_get_if_local(data.impl_or_alias_def_id) { Some(Node::Item(hir::Item { - kind: hir::ItemKind::Trait(_, is_auto, _, ident, ..), + kind: hir::ItemKind::Trait(_, is_auto, _, _, ident, _, _, _), .. })) => { // FIXME: we should do something else so that it works even on crate foreign diff --git a/compiler/rustc_trait_selection/src/errors.rs b/compiler/rustc_trait_selection/src/errors.rs index 34b9c9988f35c..32f1e894b3897 100644 --- a/compiler/rustc_trait_selection/src/errors.rs +++ b/compiler/rustc_trait_selection/src/errors.rs @@ -607,7 +607,7 @@ impl Subdiagnostic for AddLifetimeParamsSuggestion<'_> { match self.tcx.parent_hir_node(self.tcx.local_def_id_to_hir_id(anon_reg.scope)) { hir::Node::Item(hir::Item { - kind: hir::ItemKind::Trait(_, _, _, _, generics, ..), + kind: hir::ItemKind::Trait(_, _, _, _, _, generics, ..), .. }) | hir::Node::Item(hir::Item { diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index f54339429fa58..5ec1d352b7e20 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -2875,7 +2875,7 @@ fn clean_maybe_renamed_item<'tcx>( ItemKind::Fn { ref sig, generics, body: body_id, .. } => { clean_fn_or_proc_macro(item, sig, generics, body_id, &mut name, cx) } - ItemKind::Trait(_, _, _, _, generics, bounds, item_ids) => { + ItemKind::Trait(_, _, _, _, _, generics, bounds, item_ids) => { let items = item_ids .iter() .map(|&ti| clean_trait_item(cx.tcx.hir_trait_item(ti), cx)) diff --git a/src/tools/clippy/clippy_lints/src/arbitrary_source_item_ordering.rs b/src/tools/clippy/clippy_lints/src/arbitrary_source_item_ordering.rs index 454026e80ab30..7f0f0a0245f4c 100644 --- a/src/tools/clippy/clippy_lints/src/arbitrary_source_item_ordering.rs +++ b/src/tools/clippy/clippy_lints/src/arbitrary_source_item_ordering.rs @@ -306,7 +306,7 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering { cur_f = Some(field); } }, - ItemKind::Trait(_constness, is_auto, _safety, _ident, _generics, _generic_bounds, item_ref) + ItemKind::Trait(_constness, is_auto, _safety, _impl_restriction, _ident, _generics, _generic_bounds, item_ref) if self.enable_ordering_for_trait && *is_auto == IsAuto::No => { let mut cur_t: Option<(TraitItemId, Ident)> = None; diff --git a/src/tools/clippy/clippy_lints/src/item_name_repetitions.rs b/src/tools/clippy/clippy_lints/src/item_name_repetitions.rs index 70df856570b26..06bea4ba1ffd8 100644 --- a/src/tools/clippy/clippy_lints/src/item_name_repetitions.rs +++ b/src/tools/clippy/clippy_lints/src/item_name_repetitions.rs @@ -528,7 +528,7 @@ impl LateLintPass<'_> for ItemNameRepetitions { | ItemKind::Fn { ident, .. } | ItemKind::Macro(ident, ..) | ItemKind::Static(_, ident, ..) - | ItemKind::Trait(_, _, _, ident, ..) + | ItemKind::Trait(_, _, _, _, ident, ..) | ItemKind::TraitAlias(_, ident, ..) | ItemKind::TyAlias(ident, ..) | ItemKind::Union(ident, ..) diff --git a/src/tools/clippy/clippy_lints/src/len_without_is_empty.rs b/src/tools/clippy/clippy_lints/src/len_without_is_empty.rs index 1d219d7c3b74d..a3dc32dc77186 100644 --- a/src/tools/clippy/clippy_lints/src/len_without_is_empty.rs +++ b/src/tools/clippy/clippy_lints/src/len_without_is_empty.rs @@ -44,7 +44,7 @@ declare_lint_pass!(LenWithoutIsEmpty => [LEN_WITHOUT_IS_EMPTY]); impl<'tcx> LateLintPass<'tcx> for LenWithoutIsEmpty { fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) { - if let ItemKind::Trait(_, _, _, ident, _, _, trait_items) = item.kind + if let ItemKind::Trait(_, _, _, _, ident, _, _, trait_items) = item.kind && !item.span.from_expansion() { check_trait_items(cx, item, ident, trait_items); diff --git a/src/tools/clippy/clippy_lints/src/missing_doc.rs b/src/tools/clippy/clippy_lints/src/missing_doc.rs index 89a61298bf19c..35e75d34a5d3a 100644 --- a/src/tools/clippy/clippy_lints/src/missing_doc.rs +++ b/src/tools/clippy/clippy_lints/src/missing_doc.rs @@ -159,7 +159,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc { | ItemKind::Macro(ident, ..) | ItemKind::Static(_, ident, ..) | ItemKind::Struct(ident, ..) - | ItemKind::Trait(_, _, _, ident, ..) + | ItemKind::Trait(_, _, _, _, ident, ..) | ItemKind::TraitAlias(_, ident, ..) | ItemKind::TyAlias(ident, ..) | ItemKind::Union(ident, ..) => ident.span, diff --git a/src/tools/clippy/clippy_lints/src/trait_bounds.rs b/src/tools/clippy/clippy_lints/src/trait_bounds.rs index e7a785289f39f..4cd3707854c48 100644 --- a/src/tools/clippy/clippy_lints/src/trait_bounds.rs +++ b/src/tools/clippy/clippy_lints/src/trait_bounds.rs @@ -115,7 +115,7 @@ impl<'tcx> LateLintPass<'tcx> for TraitBounds { // special handling for self trait bounds as these are not considered generics // i.e. trait Foo: Display {} if let Item { - kind: ItemKind::Trait(_, _, _, _, _, bounds, ..), + kind: ItemKind::Trait(_, _, _, _, _, _, bounds, ..), .. } = item { @@ -136,7 +136,7 @@ impl<'tcx> LateLintPass<'tcx> for TraitBounds { .. }) = segments.first() && let Some(Node::Item(Item { - kind: ItemKind::Trait(_, _, _, _, _, self_bounds, _), + kind: ItemKind::Trait(_, _, _, _, _, _, self_bounds, _), .. })) = cx.tcx.hir_get_if_local(*def_id) { diff --git a/src/tools/clippy/clippy_lints/src/upper_case_acronyms.rs b/src/tools/clippy/clippy_lints/src/upper_case_acronyms.rs index 6ea6a0ad85aa4..52dfcab363dbd 100644 --- a/src/tools/clippy/clippy_lints/src/upper_case_acronyms.rs +++ b/src/tools/clippy/clippy_lints/src/upper_case_acronyms.rs @@ -131,7 +131,7 @@ impl LateLintPass<'_> for UpperCaseAcronyms { return; } match it.kind { - ItemKind::TyAlias(ident, ..) | ItemKind::Struct(ident, ..) | ItemKind::Trait(_, _, _, ident, ..) => { + ItemKind::TyAlias(ident, ..) | ItemKind::Struct(ident, ..) | ItemKind::Trait(_, _, _, _, ident, ..) => { check_ident(cx, &ident, it.hir_id(), self.upper_case_acronyms_aggressive); }, ItemKind::Enum(ident, _, ref enumdef) => { From 01793db93cec6ad447ad558dd2a0dcb8495af727 Mon Sep 17 00:00:00 2001 From: CoCo-Japan-pan <115922543+CoCo-Japan-pan@users.noreply.github.com> Date: Sun, 22 Mar 2026 16:13:42 +0900 Subject: [PATCH 02/25] Intravisit `hir::ImplRestriction` --- compiler/rustc_hir/src/intravisit.rs | 31 +++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_hir/src/intravisit.rs b/compiler/rustc_hir/src/intravisit.rs index ae99ca7002248..1de0547bdb107 100644 --- a/compiler/rustc_hir/src/intravisit.rs +++ b/compiler/rustc_hir/src/intravisit.rs @@ -433,6 +433,21 @@ pub trait Visitor<'v>: Sized { fn visit_use(&mut self, path: &'v UsePath<'v>, hir_id: HirId) -> Self::Result { walk_use(self, path, hir_id) } + fn visit_impl_restriction( + &mut self, + impl_restriction: &'v ImplRestriction<'v>, + ) -> Self::Result { + walk_impl_restriction(self, impl_restriction) + } + fn visit_restriction_kind( + &mut self, + restriction_kind: &'v RestrictionKind<'v>, + ) -> Self::Result { + match restriction_kind { + RestrictionKind::Unrestricted => Self::Result::output(), + RestrictionKind::Restricted { path, shorthand: _ } => walk_mod_path(self, path), + } + } fn visit_trait_item(&mut self, ti: &'v TraitItem<'v>) -> Self::Result { walk_trait_item(self, ti) } @@ -622,12 +637,13 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) -> V:: _constness, _is_auto, _safety, - _impl_restriction, + ref impl_restriction, ident, ref generics, bounds, trait_item_refs, ) => { + try_visit!(visitor.visit_restriction_kind(&impl_restriction.kind)); try_visit!(visitor.visit_ident(ident)); try_visit!(visitor.visit_generics(generics)); walk_list!(visitor, visit_param_bound, bounds); @@ -1260,6 +1276,19 @@ pub fn walk_use<'v, V: Visitor<'v>>( V::Result::output() } +pub fn walk_impl_restriction<'v, V: Visitor<'v>>( + visitor: &mut V, + impl_restriction: &'v ImplRestriction<'v>, +) -> V::Result { + visitor.visit_restriction_kind(&impl_restriction.kind) +} + +pub fn walk_mod_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v ModPath<'v>) -> V::Result { + let ModPath { segments, res: _, span: _ } = path; + walk_list!(visitor, visit_path_segment, *segments); + V::Result::output() +} + pub fn walk_trait_item<'v, V: Visitor<'v>>( visitor: &mut V, trait_item: &'v TraitItem<'v>, From bda0fcea83a9fadf28ca59b5a4e95bafbce46d0d Mon Sep 17 00:00:00 2001 From: CoCo-Japan-pan <115922543+CoCo-Japan-pan@users.noreply.github.com> Date: Wed, 25 Mar 2026 23:47:20 +0900 Subject: [PATCH 03/25] Lower `impl` restriction information to `TraitDef` --- compiler/rustc_hir_analysis/src/collect.rs | 25 ++++++++--- compiler/rustc_middle/src/ty/trait_def.rs | 49 ++++++++++++++++++++++ 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/collect.rs b/compiler/rustc_hir_analysis/src/collect.rs index 80ef2001cc72e..426761f980293 100644 --- a/compiler/rustc_hir_analysis/src/collect.rs +++ b/compiler/rustc_hir_analysis/src/collect.rs @@ -893,11 +893,25 @@ fn adt_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::AdtDef<'_> { fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef { let item = tcx.hir_expect_item(def_id); - let (constness, is_alias, is_auto, safety) = match item.kind { - hir::ItemKind::Trait(constness, is_auto, safety, ..) => { - (constness, false, is_auto == hir::IsAuto::Yes, safety) - } - hir::ItemKind::TraitAlias(constness, ..) => (constness, true, false, hir::Safety::Safe), + let (constness, is_alias, is_auto, safety, impl_restriction) = match item.kind { + hir::ItemKind::Trait(constness, is_auto, safety, impl_restriction, ..) => ( + constness, + false, + is_auto == hir::IsAuto::Yes, + safety, + if let hir::RestrictionKind::Restricted { path, shorthand: _ } = impl_restriction.kind { + ty::trait_def::ImplRestrictionKind::Restricted(path.res, impl_restriction.span) + } else { + ty::trait_def::ImplRestrictionKind::Unrestricted + }, + ), + hir::ItemKind::TraitAlias(constness, ..) => ( + constness, + true, + false, + hir::Safety::Safe, + ty::trait_def::ImplRestrictionKind::Unrestricted, + ), _ => span_bug!(item.span, "trait_def_of_item invoked on non-trait"), }; @@ -946,6 +960,7 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef { def_id: def_id.to_def_id(), safety, constness, + impl_restriction, paren_sugar, has_auto_impl: is_auto, is_marker, diff --git a/compiler/rustc_middle/src/ty/trait_def.rs b/compiler/rustc_middle/src/ty/trait_def.rs index 3e69eda11ca63..d9c6bf9393fcf 100644 --- a/compiler/rustc_middle/src/ty/trait_def.rs +++ b/compiler/rustc_middle/src/ty/trait_def.rs @@ -24,6 +24,9 @@ pub struct TraitDef { /// Whether this trait is `const`. pub constness: hir::Constness, + /// Restrictions on trait implementations. + pub impl_restriction: ImplRestrictionKind, + /// If `true`, then this trait had the `#[rustc_paren_sugar]` /// attribute, indicating that it should be used with `Foo()` /// sugar. This is a temporary thing -- eventually any trait will @@ -97,6 +100,52 @@ pub enum TraitSpecializationKind { AlwaysApplicable, } +/// Whether the trait implementation is unrestricted or restricted within a specific module. +#[derive(HashStable, PartialEq, Clone, Copy, Encodable, Decodable)] +pub enum ImplRestrictionKind { + /// The restriction does not affect this trait, and it can be implemented anywhere. + Unrestricted, + /// This trait can only be implemented within the specified module. + Restricted(DefId, Span), +} + +impl ImplRestrictionKind { + /// Returns `true` if the behavior is allowed/unrestricted in the given module. + /// A value of `false` indicates that the behavior is prohibited. + pub fn is_allowed_in(self, module: DefId, tcx: TyCtxt<'_>) -> bool { + match self { + ImplRestrictionKind::Unrestricted => true, + ImplRestrictionKind::Restricted(restricted_to, _) => { + tcx.is_descendant_of(module, restricted_to) + } + } + } + + /// Obtain the [`Span`] of the restriction. Panics if the restriction is unrestricted. + pub fn expect_span(self) -> Span { + match self { + ImplRestrictionKind::Unrestricted => { + bug!("called `expect_span` on an unrestricted item") + } + ImplRestrictionKind::Restricted(_, span) => span, + } + } + + /// Obtain the path of the restriction. If unrestricted, an empty string is returned. + pub fn restriction_path(self, tcx: TyCtxt<'_>, krate: rustc_span::def_id::CrateNum) -> String { + match self { + ImplRestrictionKind::Unrestricted => String::new(), + ImplRestrictionKind::Restricted(restricted_to, _) => { + if restricted_to.krate == krate { + tcx.def_path_str(restricted_to) + } else { + tcx.crate_name(restricted_to.krate).to_ident_string() + } + } + } + } +} + #[derive(Default, Debug, HashStable)] pub struct TraitImpls { blanket_impls: Vec, From cdbccf7275e77fafd18edce5cb2422ea5d1d92e2 Mon Sep 17 00:00:00 2001 From: CoCo-Japan-pan <115922543+CoCo-Japan-pan@users.noreply.github.com> Date: Wed, 25 Mar 2026 23:48:11 +0900 Subject: [PATCH 04/25] Emit error in implementation of restricted trait --- compiler/rustc_hir_analysis/src/coherence/mod.rs | 10 ++++++++++ compiler/rustc_hir_analysis/src/errors.rs | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/compiler/rustc_hir_analysis/src/coherence/mod.rs b/compiler/rustc_hir_analysis/src/coherence/mod.rs index 8f83761518bda..c60b40c61231f 100644 --- a/compiler/rustc_hir_analysis/src/coherence/mod.rs +++ b/compiler/rustc_hir_analysis/src/coherence/mod.rs @@ -100,6 +100,16 @@ fn enforce_trait_manually_implementable( return Err(tcx.dcx().emit_err(errors::SpecializationTrait { span: impl_header_span })); } } + + if !trait_def.impl_restriction.is_allowed_in(impl_def_id.to_def_id(), tcx) { + return Err(tcx.dcx().emit_err(errors::ImplOfRestrictedTrait { + impl_span: impl_header_span, + restriction_span: trait_def.impl_restriction.expect_span(), + restriction_path: trait_def + .impl_restriction + .restriction_path(tcx, rustc_hir::def_id::LOCAL_CRATE), + })); + } Ok(()) } diff --git a/compiler/rustc_hir_analysis/src/errors.rs b/compiler/rustc_hir_analysis/src/errors.rs index fcd4cb938bf73..2e6a0e5ab6f67 100644 --- a/compiler/rustc_hir_analysis/src/errors.rs +++ b/compiler/rustc_hir_analysis/src/errors.rs @@ -1027,6 +1027,16 @@ pub(crate) struct SpecializationTrait { pub span: Span, } +#[derive(Diagnostic)] +#[diag("trait cannot be implemented outside `{$restriction_path}`")] +pub(crate) struct ImplOfRestrictedTrait { + #[primary_span] + pub impl_span: Span, + #[note("trait restricted here")] + pub restriction_span: Span, + pub restriction_path: String, +} + #[derive(Diagnostic)] #[diag("implicit types in closure signatures are forbidden when `for<...>` is present")] pub(crate) struct ClosureImplicitHrtb { From 222292a95121cb702d7678c1285fc6dfe7874c5b Mon Sep 17 00:00:00 2001 From: CoCo-Japan-pan <115922543+CoCo-Japan-pan@users.noreply.github.com> Date: Fri, 27 Mar 2026 10:21:59 +0900 Subject: [PATCH 05/25] Add UI tests for semantic checks of `impl` restrictions --- .../auxiliary/external-impl-restriction.rs | 8 ++ .../impl-restriction-check.rs | 40 ++++++++ .../impl-restriction-check.stderr | 98 +++++++++++++++++++ 3 files changed, 146 insertions(+) create mode 100644 tests/ui/impl-restriction/auxiliary/external-impl-restriction.rs create mode 100644 tests/ui/impl-restriction/impl-restriction-check.rs create mode 100644 tests/ui/impl-restriction/impl-restriction-check.stderr diff --git a/tests/ui/impl-restriction/auxiliary/external-impl-restriction.rs b/tests/ui/impl-restriction/auxiliary/external-impl-restriction.rs new file mode 100644 index 0000000000000..785aeedf56d55 --- /dev/null +++ b/tests/ui/impl-restriction/auxiliary/external-impl-restriction.rs @@ -0,0 +1,8 @@ +#![feature(impl_restriction)] +#![expect(incomplete_features)] + +pub impl(crate) trait TopLevel {} + +pub mod inner { + pub impl(self) trait Inner {} +} diff --git a/tests/ui/impl-restriction/impl-restriction-check.rs b/tests/ui/impl-restriction/impl-restriction-check.rs new file mode 100644 index 0000000000000..c41c818ac7af8 --- /dev/null +++ b/tests/ui/impl-restriction/impl-restriction-check.rs @@ -0,0 +1,40 @@ +//@ aux-build: external-impl-restriction.rs +#![feature(impl_restriction)] +#![expect(incomplete_features)] + +extern crate external_impl_restriction as external; + +struct LocalType; // needed to avoid orphan rule errors + +impl external::TopLevel for LocalType {} //~ ERROR trait cannot be implemented outside `external_impl_restriction` +impl external::inner::Inner for LocalType {} //~ ERROR trait cannot be implemented outside `external_impl_restriction` + +pub mod foo { + pub mod bar { + pub(crate) impl(self) trait Foo {} + pub(crate) impl(super) trait Bar {} + pub impl(crate) trait Baz {} + pub(crate) impl(in crate::foo::bar) trait Qux {} + pub(crate) impl(in crate::foo) trait FooBar {} + + impl Foo for i16 {} // OK + impl Bar for i16 {} // OK + impl Baz for i16 {} // OK + impl Qux for i16 {} // OK + impl FooBar for i16 {} // OK + } + + impl bar::Foo for i8 {} //~ ERROR trait cannot be implemented outside `bar` + impl bar::Bar for i8 {} // OK + impl bar::Baz for i8 {} // OK + impl bar::Qux for i8 {} //~ ERROR trait cannot be implemented outside `bar` + impl bar::FooBar for i8 {} // OK +} + +impl foo::bar::Foo for u8 {} //~ ERROR trait cannot be implemented outside `bar` +impl foo::bar::Bar for u8 {} //~ ERROR trait cannot be implemented outside `foo` +impl foo::bar::Baz for u8 {} // OK +impl foo::bar::Qux for u8 {} //~ ERROR trait cannot be implemented outside `bar` +impl foo::bar::FooBar for u8 {} //~ ERROR trait cannot be implemented outside `foo` + +fn main() {} diff --git a/tests/ui/impl-restriction/impl-restriction-check.stderr b/tests/ui/impl-restriction/impl-restriction-check.stderr new file mode 100644 index 0000000000000..e8bc96fae3f96 --- /dev/null +++ b/tests/ui/impl-restriction/impl-restriction-check.stderr @@ -0,0 +1,98 @@ +error: trait cannot be implemented outside `external_impl_restriction` + --> $DIR/impl-restriction-check.rs:9:1 + | +LL | impl external::TopLevel for LocalType {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +note: trait restricted here + --> $DIR/auxiliary/external-impl-restriction.rs:4:5 + | +LL | pub impl(crate) trait TopLevel {} + | ^^^^^^^^^^^ + +error: trait cannot be implemented outside `external_impl_restriction` + --> $DIR/impl-restriction-check.rs:10:1 + | +LL | impl external::inner::Inner for LocalType {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +note: trait restricted here + --> $DIR/auxiliary/external-impl-restriction.rs:7:9 + | +LL | pub impl(self) trait Inner {} + | ^^^^^^^^^^ + +error: trait cannot be implemented outside `bar` + --> $DIR/impl-restriction-check.rs:27:5 + | +LL | impl bar::Foo for i8 {} + | ^^^^^^^^^^^^^^^^^^^^ + | +note: trait restricted here + --> $DIR/impl-restriction-check.rs:14:20 + | +LL | pub(crate) impl(self) trait Foo {} + | ^^^^^^^^^^ + +error: trait cannot be implemented outside `bar` + --> $DIR/impl-restriction-check.rs:34:1 + | +LL | impl foo::bar::Foo for u8 {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | +note: trait restricted here + --> $DIR/impl-restriction-check.rs:14:20 + | +LL | pub(crate) impl(self) trait Foo {} + | ^^^^^^^^^^ + +error: trait cannot be implemented outside `foo` + --> $DIR/impl-restriction-check.rs:35:1 + | +LL | impl foo::bar::Bar for u8 {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | +note: trait restricted here + --> $DIR/impl-restriction-check.rs:15:20 + | +LL | pub(crate) impl(super) trait Bar {} + | ^^^^^^^^^^^ + +error: trait cannot be implemented outside `bar` + --> $DIR/impl-restriction-check.rs:30:5 + | +LL | impl bar::Qux for i8 {} + | ^^^^^^^^^^^^^^^^^^^^ + | +note: trait restricted here + --> $DIR/impl-restriction-check.rs:17:20 + | +LL | pub(crate) impl(in crate::foo::bar) trait Qux {} + | ^^^^^^^^^^^^^^^^^^^^^^^^ + +error: trait cannot be implemented outside `bar` + --> $DIR/impl-restriction-check.rs:37:1 + | +LL | impl foo::bar::Qux for u8 {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | +note: trait restricted here + --> $DIR/impl-restriction-check.rs:17:20 + | +LL | pub(crate) impl(in crate::foo::bar) trait Qux {} + | ^^^^^^^^^^^^^^^^^^^^^^^^ + +error: trait cannot be implemented outside `foo` + --> $DIR/impl-restriction-check.rs:38:1 + | +LL | impl foo::bar::FooBar for u8 {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +note: trait restricted here + --> $DIR/impl-restriction-check.rs:18:20 + | +LL | pub(crate) impl(in crate::foo) trait FooBar {} + | ^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 8 previous errors + From 159f1f24fa52fe9ca03ce30fc34e6b7ce62ca7b4 Mon Sep 17 00:00:00 2001 From: CoCo-Japan-pan <115922543+CoCo-Japan-pan@users.noreply.github.com> Date: Sat, 4 Apr 2026 16:41:57 +0900 Subject: [PATCH 06/25] Call `visit_path_segment` directly --- compiler/rustc_hir/src/intravisit.rs | 32 +++------------------------- 1 file changed, 3 insertions(+), 29 deletions(-) diff --git a/compiler/rustc_hir/src/intravisit.rs b/compiler/rustc_hir/src/intravisit.rs index 1de0547bdb107..03b254cf7e352 100644 --- a/compiler/rustc_hir/src/intravisit.rs +++ b/compiler/rustc_hir/src/intravisit.rs @@ -433,21 +433,6 @@ pub trait Visitor<'v>: Sized { fn visit_use(&mut self, path: &'v UsePath<'v>, hir_id: HirId) -> Self::Result { walk_use(self, path, hir_id) } - fn visit_impl_restriction( - &mut self, - impl_restriction: &'v ImplRestriction<'v>, - ) -> Self::Result { - walk_impl_restriction(self, impl_restriction) - } - fn visit_restriction_kind( - &mut self, - restriction_kind: &'v RestrictionKind<'v>, - ) -> Self::Result { - match restriction_kind { - RestrictionKind::Unrestricted => Self::Result::output(), - RestrictionKind::Restricted { path, shorthand: _ } => walk_mod_path(self, path), - } - } fn visit_trait_item(&mut self, ti: &'v TraitItem<'v>) -> Self::Result { walk_trait_item(self, ti) } @@ -643,7 +628,9 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) -> V:: bounds, trait_item_refs, ) => { - try_visit!(visitor.visit_restriction_kind(&impl_restriction.kind)); + if let RestrictionKind::Restricted { path, shorthand: _ } = &impl_restriction.kind { + walk_list!(visitor, visit_path_segment, path.segments); + } try_visit!(visitor.visit_ident(ident)); try_visit!(visitor.visit_generics(generics)); walk_list!(visitor, visit_param_bound, bounds); @@ -1276,19 +1263,6 @@ pub fn walk_use<'v, V: Visitor<'v>>( V::Result::output() } -pub fn walk_impl_restriction<'v, V: Visitor<'v>>( - visitor: &mut V, - impl_restriction: &'v ImplRestriction<'v>, -) -> V::Result { - visitor.visit_restriction_kind(&impl_restriction.kind) -} - -pub fn walk_mod_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v ModPath<'v>) -> V::Result { - let ModPath { segments, res: _, span: _ } = path; - walk_list!(visitor, visit_path_segment, *segments); - V::Result::output() -} - pub fn walk_trait_item<'v, V: Visitor<'v>>( visitor: &mut V, trait_item: &'v TraitItem<'v>, From 5863660b0d808f7336ebc09b70fff85b895448a0 Mon Sep 17 00:00:00 2001 From: CoCo-Japan-pan <115922543+CoCo-Japan-pan@users.noreply.github.com> Date: Sat, 4 Apr 2026 16:53:30 +0900 Subject: [PATCH 07/25] Remove `shorthand` from HIR --- compiler/rustc_ast_lowering/src/item.rs | 7 ++----- compiler/rustc_hir/src/hir.rs | 2 +- compiler/rustc_hir/src/intravisit.rs | 2 +- compiler/rustc_hir_analysis/src/collect.rs | 2 +- compiler/rustc_hir_pretty/src/lib.rs | 6 ++---- 5 files changed, 7 insertions(+), 12 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index 4c8d34f1bd779..dae093683b7c1 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -1842,13 +1842,10 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> { ) -> &'hir hir::ImplRestriction<'hir> { let kind = match &r.kind { RestrictionKind::Unrestricted => hir::RestrictionKind::Unrestricted, - RestrictionKind::Restricted { path, id, shorthand } => { + RestrictionKind::Restricted { path, id, shorthand: _ } => { let res = self.resolver.get_partial_res(*id); if let Some(did) = res.and_then(|res| res.expect_full_res().opt_def_id()) { - hir::RestrictionKind::Restricted { - path: self.lower_mod_path(did, path), - shorthand: *shorthand, - } + hir::RestrictionKind::Restricted(self.lower_mod_path(did, path)) } else { self.dcx().span_delayed_bug(path.span, "should have errored in resolve"); hir::RestrictionKind::Unrestricted diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index d612bdacec7d4..9d686ea39d098 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -4416,7 +4416,7 @@ pub enum RestrictionKind<'hir> { /// The restriction does not affect the item. Unrestricted, /// The restriction only applies outside of this path. - Restricted { path: &'hir ModPath<'hir>, shorthand: bool }, + Restricted(&'hir ModPath<'hir>), } /// The actual safety specified in syntax. We may treat diff --git a/compiler/rustc_hir/src/intravisit.rs b/compiler/rustc_hir/src/intravisit.rs index 03b254cf7e352..99511189e9283 100644 --- a/compiler/rustc_hir/src/intravisit.rs +++ b/compiler/rustc_hir/src/intravisit.rs @@ -628,7 +628,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) -> V:: bounds, trait_item_refs, ) => { - if let RestrictionKind::Restricted { path, shorthand: _ } = &impl_restriction.kind { + if let RestrictionKind::Restricted(path) = &impl_restriction.kind { walk_list!(visitor, visit_path_segment, path.segments); } try_visit!(visitor.visit_ident(ident)); diff --git a/compiler/rustc_hir_analysis/src/collect.rs b/compiler/rustc_hir_analysis/src/collect.rs index 426761f980293..14da2fff4609e 100644 --- a/compiler/rustc_hir_analysis/src/collect.rs +++ b/compiler/rustc_hir_analysis/src/collect.rs @@ -899,7 +899,7 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef { false, is_auto == hir::IsAuto::Yes, safety, - if let hir::RestrictionKind::Restricted { path, shorthand: _ } = impl_restriction.kind { + if let hir::RestrictionKind::Restricted(path) = impl_restriction.kind { ty::trait_def::ImplRestrictionKind::Restricted(path.res, impl_restriction.span) } else { ty::trait_def::ImplRestrictionKind::Unrestricted diff --git a/compiler/rustc_hir_pretty/src/lib.rs b/compiler/rustc_hir_pretty/src/lib.rs index 07ee1118390eb..6f64d07b01d66 100644 --- a/compiler/rustc_hir_pretty/src/lib.rs +++ b/compiler/rustc_hir_pretty/src/lib.rs @@ -2651,11 +2651,9 @@ impl<'a> State<'a> { fn print_impl_restriction(&mut self, r: &hir::ImplRestriction<'_>) { match r.kind { hir::RestrictionKind::Unrestricted => {} - hir::RestrictionKind::Restricted { path, shorthand } => { + hir::RestrictionKind::Restricted(path) => { self.word("impl("); - if shorthand { - self.word_nbsp("in"); - } + self.word_nbsp("in"); self.print_path(path, false); self.word(")"); } From 4f1260910a5fb54e3eb3e5ba8f4587c01c685479 Mon Sep 17 00:00:00 2001 From: CoCo-Japan-pan <115922543+CoCo-Japan-pan@users.noreply.github.com> Date: Sat, 4 Apr 2026 17:16:14 +0900 Subject: [PATCH 08/25] Remove the type alias for `Path<'hir, DefId>` --- compiler/rustc_ast_lowering/src/item.rs | 19 ++++++++++++++++--- compiler/rustc_ast_lowering/src/path.rs | 17 ----------------- compiler/rustc_hir/src/hir.rs | 5 +---- 3 files changed, 17 insertions(+), 24 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index dae093683b7c1..a721f41a6afef 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -24,8 +24,8 @@ use tracing::instrument; use super::errors::{InvalidAbi, InvalidAbiSuggestion, TupleStructWithDefault, UnionWithDefault}; use super::stability::{enabled_names, gate_unstable_abi}; use super::{ - AstOwner, FnDeclKind, ImplTraitContext, ImplTraitPosition, LoweringContext, ParamMode, - RelaxedBoundForbiddenReason, RelaxedBoundPolicy, ResolverAstLoweringExt, + AstOwner, FnDeclKind, GenericArgsMode, ImplTraitContext, ImplTraitPosition, LoweringContext, + ParamMode, RelaxedBoundForbiddenReason, RelaxedBoundPolicy, ResolverAstLoweringExt, }; /// Wraps either IndexVec (during `hir_crate`), which acts like a primary @@ -1845,7 +1845,20 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> { RestrictionKind::Restricted { path, id, shorthand: _ } => { let res = self.resolver.get_partial_res(*id); if let Some(did) = res.and_then(|res| res.expect_full_res().opt_def_id()) { - hir::RestrictionKind::Restricted(self.lower_mod_path(did, path)) + hir::RestrictionKind::Restricted(self.arena.alloc(hir::Path { + res: did, + segments: self.arena.alloc_from_iter(path.segments.iter().map(|segment| { + self.lower_path_segment( + path.span, + segment, + ParamMode::Explicit, + GenericArgsMode::Err, + ImplTraitContext::Disallowed(ImplTraitPosition::Path), + None, + ) + })), + span: self.lower_span(path.span), + })) } else { self.dcx().span_delayed_bug(path.span, "should have errored in resolve"); hir::RestrictionKind::Unrestricted diff --git a/compiler/rustc_ast_lowering/src/path.rs b/compiler/rustc_ast_lowering/src/path.rs index cf0ef34a40515..139140af3e033 100644 --- a/compiler/rustc_ast_lowering/src/path.rs +++ b/compiler/rustc_ast_lowering/src/path.rs @@ -250,23 +250,6 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> { }) } - pub(crate) fn lower_mod_path(&mut self, res: DefId, p: &Path) -> &'hir hir::ModPath<'hir> { - self.arena.alloc(hir::ModPath { - res, - segments: self.arena.alloc_from_iter(p.segments.iter().map(|segment| { - self.lower_path_segment( - p.span, - segment, - ParamMode::Explicit, - GenericArgsMode::Err, - ImplTraitContext::Disallowed(ImplTraitPosition::Path), - None, - ) - })), - span: self.lower_span(p.span), - }) - } - pub(crate) fn lower_path_segment( &mut self, path_span: Span, diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 9d686ea39d098..e4e6642981d1a 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -356,9 +356,6 @@ pub struct Path<'hir, R = Res> { /// Up to three resolutions for type, value and macro namespaces. pub type UsePath<'hir> = Path<'hir, PerNS>>; -/// Module paths. Used for restrictions. -pub type ModPath<'hir> = Path<'hir, DefId>; - impl Path<'_> { pub fn is_global(&self) -> bool { self.segments.first().is_some_and(|segment| segment.ident.name == kw::PathRoot) @@ -4416,7 +4413,7 @@ pub enum RestrictionKind<'hir> { /// The restriction does not affect the item. Unrestricted, /// The restriction only applies outside of this path. - Restricted(&'hir ModPath<'hir>), + Restricted(&'hir Path<'hir, DefId>), } /// The actual safety specified in syntax. We may treat From 65effbf5d6ab29b21b209856381b78ec98e8a0f6 Mon Sep 17 00:00:00 2001 From: CoCo-Japan-pan <115922543+CoCo-Japan-pan@users.noreply.github.com> Date: Sat, 4 Apr 2026 21:42:52 +0900 Subject: [PATCH 09/25] Add a FIXME for rustdoc handling `impl` restrictions --- src/librustdoc/clean/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 5ec1d352b7e20..a7133c6993e0e 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -2875,7 +2875,8 @@ fn clean_maybe_renamed_item<'tcx>( ItemKind::Fn { ref sig, generics, body: body_id, .. } => { clean_fn_or_proc_macro(item, sig, generics, body_id, &mut name, cx) } - ItemKind::Trait(_, _, _, _, _, generics, bounds, item_ids) => { + // FIXME: rustdoc will need to handle `impl` restrictions at some point + ItemKind::Trait(_, _, _, _impl_restriction, _, generics, bounds, item_ids) => { let items = item_ids .iter() .map(|&ti| clean_trait_item(cx.tcx.hir_trait_item(ti), cx)) From 2f400deab37048251c78bca2e9201aef6978d9fb Mon Sep 17 00:00:00 2001 From: CoCo-Japan-pan <115922543+CoCo-Japan-pan@users.noreply.github.com> Date: Sun, 5 Apr 2026 13:15:21 +0900 Subject: [PATCH 10/25] Modify `restriction_path` --- compiler/rustc_middle/src/ty/trait_def.rs | 5 +- ...rr => impl-restriction-check.e2015.stderr} | 40 ++++---- .../impl-restriction-check.e2018.stderr | 98 +++++++++++++++++++ .../impl-restriction-check.rs | 25 +++-- 4 files changed, 138 insertions(+), 30 deletions(-) rename tests/ui/impl-restriction/{impl-restriction-check.stderr => impl-restriction-check.e2015.stderr} (72%) create mode 100644 tests/ui/impl-restriction/impl-restriction-check.e2018.stderr diff --git a/compiler/rustc_middle/src/ty/trait_def.rs b/compiler/rustc_middle/src/ty/trait_def.rs index d9c6bf9393fcf..70eba8308da47 100644 --- a/compiler/rustc_middle/src/ty/trait_def.rs +++ b/compiler/rustc_middle/src/ty/trait_def.rs @@ -12,6 +12,7 @@ use tracing::debug; use crate::query::LocalCrate; use crate::traits::specialization_graph; use crate::ty::fast_reject::{self, SimplifiedType, TreatParams}; +use crate::ty::print::{with_crate_prefix, with_no_trimmed_paths}; use crate::ty::{Ident, Ty, TyCtxt}; /// A trait's definition with type information. @@ -137,9 +138,9 @@ impl ImplRestrictionKind { ImplRestrictionKind::Unrestricted => String::new(), ImplRestrictionKind::Restricted(restricted_to, _) => { if restricted_to.krate == krate { - tcx.def_path_str(restricted_to) + with_crate_prefix!(with_no_trimmed_paths!(tcx.def_path_str(restricted_to))) } else { - tcx.crate_name(restricted_to.krate).to_ident_string() + with_no_trimmed_paths!(tcx.def_path_str(restricted_to)) } } } diff --git a/tests/ui/impl-restriction/impl-restriction-check.stderr b/tests/ui/impl-restriction/impl-restriction-check.e2015.stderr similarity index 72% rename from tests/ui/impl-restriction/impl-restriction-check.stderr rename to tests/ui/impl-restriction/impl-restriction-check.e2015.stderr index e8bc96fae3f96..0534705835f4c 100644 --- a/tests/ui/impl-restriction/impl-restriction-check.stderr +++ b/tests/ui/impl-restriction/impl-restriction-check.e2015.stderr @@ -1,5 +1,5 @@ -error: trait cannot be implemented outside `external_impl_restriction` - --> $DIR/impl-restriction-check.rs:9:1 +error: trait cannot be implemented outside `external` + --> $DIR/impl-restriction-check.rs:12:1 | LL | impl external::TopLevel for LocalType {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -10,8 +10,8 @@ note: trait restricted here LL | pub impl(crate) trait TopLevel {} | ^^^^^^^^^^^ -error: trait cannot be implemented outside `external_impl_restriction` - --> $DIR/impl-restriction-check.rs:10:1 +error: trait cannot be implemented outside `external::inner` + --> $DIR/impl-restriction-check.rs:13:1 | LL | impl external::inner::Inner for LocalType {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -22,74 +22,74 @@ note: trait restricted here LL | pub impl(self) trait Inner {} | ^^^^^^^^^^ -error: trait cannot be implemented outside `bar` - --> $DIR/impl-restriction-check.rs:27:5 +error: trait cannot be implemented outside `foo::bar` + --> $DIR/impl-restriction-check.rs:30:5 | LL | impl bar::Foo for i8 {} | ^^^^^^^^^^^^^^^^^^^^ | note: trait restricted here - --> $DIR/impl-restriction-check.rs:14:20 + --> $DIR/impl-restriction-check.rs:17:20 | LL | pub(crate) impl(self) trait Foo {} | ^^^^^^^^^^ -error: trait cannot be implemented outside `bar` - --> $DIR/impl-restriction-check.rs:34:1 +error: trait cannot be implemented outside `foo::bar` + --> $DIR/impl-restriction-check.rs:39:1 | LL | impl foo::bar::Foo for u8 {} | ^^^^^^^^^^^^^^^^^^^^^^^^^ | note: trait restricted here - --> $DIR/impl-restriction-check.rs:14:20 + --> $DIR/impl-restriction-check.rs:17:20 | LL | pub(crate) impl(self) trait Foo {} | ^^^^^^^^^^ error: trait cannot be implemented outside `foo` - --> $DIR/impl-restriction-check.rs:35:1 + --> $DIR/impl-restriction-check.rs:41:1 | LL | impl foo::bar::Bar for u8 {} | ^^^^^^^^^^^^^^^^^^^^^^^^^ | note: trait restricted here - --> $DIR/impl-restriction-check.rs:15:20 + --> $DIR/impl-restriction-check.rs:18:20 | LL | pub(crate) impl(super) trait Bar {} | ^^^^^^^^^^^ -error: trait cannot be implemented outside `bar` - --> $DIR/impl-restriction-check.rs:30:5 +error: trait cannot be implemented outside `foo::bar` + --> $DIR/impl-restriction-check.rs:34:5 | LL | impl bar::Qux for i8 {} | ^^^^^^^^^^^^^^^^^^^^ | note: trait restricted here - --> $DIR/impl-restriction-check.rs:17:20 + --> $DIR/impl-restriction-check.rs:20:20 | LL | pub(crate) impl(in crate::foo::bar) trait Qux {} | ^^^^^^^^^^^^^^^^^^^^^^^^ -error: trait cannot be implemented outside `bar` - --> $DIR/impl-restriction-check.rs:37:1 +error: trait cannot be implemented outside `foo::bar` + --> $DIR/impl-restriction-check.rs:44:1 | LL | impl foo::bar::Qux for u8 {} | ^^^^^^^^^^^^^^^^^^^^^^^^^ | note: trait restricted here - --> $DIR/impl-restriction-check.rs:17:20 + --> $DIR/impl-restriction-check.rs:20:20 | LL | pub(crate) impl(in crate::foo::bar) trait Qux {} | ^^^^^^^^^^^^^^^^^^^^^^^^ error: trait cannot be implemented outside `foo` - --> $DIR/impl-restriction-check.rs:38:1 + --> $DIR/impl-restriction-check.rs:46:1 | LL | impl foo::bar::FooBar for u8 {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | note: trait restricted here - --> $DIR/impl-restriction-check.rs:18:20 + --> $DIR/impl-restriction-check.rs:21:20 | LL | pub(crate) impl(in crate::foo) trait FooBar {} | ^^^^^^^^^^^^^^^^^^^ diff --git a/tests/ui/impl-restriction/impl-restriction-check.e2018.stderr b/tests/ui/impl-restriction/impl-restriction-check.e2018.stderr new file mode 100644 index 0000000000000..d350c7f514142 --- /dev/null +++ b/tests/ui/impl-restriction/impl-restriction-check.e2018.stderr @@ -0,0 +1,98 @@ +error: trait cannot be implemented outside `external` + --> $DIR/impl-restriction-check.rs:12:1 + | +LL | impl external::TopLevel for LocalType {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +note: trait restricted here + --> $DIR/auxiliary/external-impl-restriction.rs:4:5 + | +LL | pub impl(crate) trait TopLevel {} + | ^^^^^^^^^^^ + +error: trait cannot be implemented outside `external::inner` + --> $DIR/impl-restriction-check.rs:13:1 + | +LL | impl external::inner::Inner for LocalType {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +note: trait restricted here + --> $DIR/auxiliary/external-impl-restriction.rs:7:9 + | +LL | pub impl(self) trait Inner {} + | ^^^^^^^^^^ + +error: trait cannot be implemented outside `crate::foo::bar` + --> $DIR/impl-restriction-check.rs:30:5 + | +LL | impl bar::Foo for i8 {} + | ^^^^^^^^^^^^^^^^^^^^ + | +note: trait restricted here + --> $DIR/impl-restriction-check.rs:17:20 + | +LL | pub(crate) impl(self) trait Foo {} + | ^^^^^^^^^^ + +error: trait cannot be implemented outside `crate::foo::bar` + --> $DIR/impl-restriction-check.rs:39:1 + | +LL | impl foo::bar::Foo for u8 {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | +note: trait restricted here + --> $DIR/impl-restriction-check.rs:17:20 + | +LL | pub(crate) impl(self) trait Foo {} + | ^^^^^^^^^^ + +error: trait cannot be implemented outside `crate::foo` + --> $DIR/impl-restriction-check.rs:41:1 + | +LL | impl foo::bar::Bar for u8 {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | +note: trait restricted here + --> $DIR/impl-restriction-check.rs:18:20 + | +LL | pub(crate) impl(super) trait Bar {} + | ^^^^^^^^^^^ + +error: trait cannot be implemented outside `crate::foo::bar` + --> $DIR/impl-restriction-check.rs:34:5 + | +LL | impl bar::Qux for i8 {} + | ^^^^^^^^^^^^^^^^^^^^ + | +note: trait restricted here + --> $DIR/impl-restriction-check.rs:20:20 + | +LL | pub(crate) impl(in crate::foo::bar) trait Qux {} + | ^^^^^^^^^^^^^^^^^^^^^^^^ + +error: trait cannot be implemented outside `crate::foo::bar` + --> $DIR/impl-restriction-check.rs:44:1 + | +LL | impl foo::bar::Qux for u8 {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | +note: trait restricted here + --> $DIR/impl-restriction-check.rs:20:20 + | +LL | pub(crate) impl(in crate::foo::bar) trait Qux {} + | ^^^^^^^^^^^^^^^^^^^^^^^^ + +error: trait cannot be implemented outside `crate::foo` + --> $DIR/impl-restriction-check.rs:46:1 + | +LL | impl foo::bar::FooBar for u8 {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +note: trait restricted here + --> $DIR/impl-restriction-check.rs:21:20 + | +LL | pub(crate) impl(in crate::foo) trait FooBar {} + | ^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 8 previous errors + diff --git a/tests/ui/impl-restriction/impl-restriction-check.rs b/tests/ui/impl-restriction/impl-restriction-check.rs index c41c818ac7af8..b4dd64de0c054 100644 --- a/tests/ui/impl-restriction/impl-restriction-check.rs +++ b/tests/ui/impl-restriction/impl-restriction-check.rs @@ -1,4 +1,7 @@ //@ aux-build: external-impl-restriction.rs +//@ revisions: e2015 e2018 +//@ [e2015] edition: 2015 +//@ [e2018] edition: 2018.. #![feature(impl_restriction)] #![expect(incomplete_features)] @@ -6,8 +9,8 @@ extern crate external_impl_restriction as external; struct LocalType; // needed to avoid orphan rule errors -impl external::TopLevel for LocalType {} //~ ERROR trait cannot be implemented outside `external_impl_restriction` -impl external::inner::Inner for LocalType {} //~ ERROR trait cannot be implemented outside `external_impl_restriction` +impl external::TopLevel for LocalType {} //~ ERROR trait cannot be implemented outside `external` +impl external::inner::Inner for LocalType {} //~ ERROR trait cannot be implemented outside `external::inner` pub mod foo { pub mod bar { @@ -24,17 +27,23 @@ pub mod foo { impl FooBar for i16 {} // OK } - impl bar::Foo for i8 {} //~ ERROR trait cannot be implemented outside `bar` + impl bar::Foo for i8 {} //[e2015]~ ERROR trait cannot be implemented outside `foo::bar` + //[e2018]~^ ERROR trait cannot be implemented outside `crate::foo::bar` impl bar::Bar for i8 {} // OK impl bar::Baz for i8 {} // OK - impl bar::Qux for i8 {} //~ ERROR trait cannot be implemented outside `bar` + impl bar::Qux for i8 {} //[e2015]~ ERROR trait cannot be implemented outside `foo::bar` + //[e2018]~^ ERROR trait cannot be implemented outside `crate::foo::bar` impl bar::FooBar for i8 {} // OK } -impl foo::bar::Foo for u8 {} //~ ERROR trait cannot be implemented outside `bar` -impl foo::bar::Bar for u8 {} //~ ERROR trait cannot be implemented outside `foo` +impl foo::bar::Foo for u8 {} //[e2015]~ ERROR trait cannot be implemented outside `foo::bar` +//[e2018]~^ ERROR trait cannot be implemented outside `crate::foo::bar` +impl foo::bar::Bar for u8 {} //[e2015]~ ERROR trait cannot be implemented outside `foo` +//[e2018]~^ ERROR trait cannot be implemented outside `crate::foo` impl foo::bar::Baz for u8 {} // OK -impl foo::bar::Qux for u8 {} //~ ERROR trait cannot be implemented outside `bar` -impl foo::bar::FooBar for u8 {} //~ ERROR trait cannot be implemented outside `foo` +impl foo::bar::Qux for u8 {} //[e2015]~ ERROR trait cannot be implemented outside `foo::bar` +//[e2018]~^ ERROR trait cannot be implemented outside `crate::foo::bar` +impl foo::bar::FooBar for u8 {} //[e2015]~ ERROR trait cannot be implemented outside `foo` +//[e2018]~^ ERROR trait cannot be implemented outside `crate::foo` fn main() {} From 6f9727342f6e4c3ebe6b2934999e2ee6b646c404 Mon Sep 17 00:00:00 2001 From: CoCo-Japan-pan <115922543+CoCo-Japan-pan@users.noreply.github.com> Date: Sun, 5 Apr 2026 13:18:00 +0900 Subject: [PATCH 11/25] Add non-ancestor errors involving restrictions to other crates --- .../restriction_resolution_errors.rs | 5 +++ .../restriction_resolution_errors.stderr | 44 +++++++++++-------- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/tests/ui/impl-restriction/restriction_resolution_errors.rs b/tests/ui/impl-restriction/restriction_resolution_errors.rs index b36f2cf9bdfba..01173c111ae76 100644 --- a/tests/ui/impl-restriction/restriction_resolution_errors.rs +++ b/tests/ui/impl-restriction/restriction_resolution_errors.rs @@ -1,6 +1,9 @@ +//@ aux-build: external-impl-restriction.rs #![feature(impl_restriction)] #![expect(incomplete_features)] +extern crate external_impl_restriction as external; + pub mod a { pub enum E {} pub mod d {} @@ -53,6 +56,8 @@ pub impl(self) trait T16 {} pub impl(super) trait T17 {} //~ ERROR too many leading `super` keywords [E0433] +pub impl(in external) trait T18 {} //~ ERROR trait implementation can only be restricted to ancestor modules + // Check if we can resolve paths referring to modules declared later. pub impl(in crate::j) trait L4 {} //~ ERROR trait implementation can only be restricted to ancestor modules diff --git a/tests/ui/impl-restriction/restriction_resolution_errors.stderr b/tests/ui/impl-restriction/restriction_resolution_errors.stderr index 540803285c1b5..48bfab2bc1eb7 100644 --- a/tests/ui/impl-restriction/restriction_resolution_errors.stderr +++ b/tests/ui/impl-restriction/restriction_resolution_errors.stderr @@ -1,71 +1,77 @@ error: trait implementation can only be restricted to ancestor modules - --> $DIR/restriction_resolution_errors.rs:14:21 + --> $DIR/restriction_resolution_errors.rs:17:21 | LL | pub impl(in ::std) trait T2 {} | ^^^^^ error: trait implementation can only be restricted to ancestor modules - --> $DIR/restriction_resolution_errors.rs:16:21 + --> $DIR/restriction_resolution_errors.rs:19:21 | LL | pub impl(in self::c) trait T3 {} | ^^^^^^^ error: trait implementation can only be restricted to ancestor modules - --> $DIR/restriction_resolution_errors.rs:18:21 + --> $DIR/restriction_resolution_errors.rs:21:21 | LL | pub impl(in super::d) trait T4 {} | ^^^^^^^^ error[E0433]: too many leading `super` keywords - --> $DIR/restriction_resolution_errors.rs:24:35 + --> $DIR/restriction_resolution_errors.rs:27:35 | LL | pub impl(in super::super::super) trait T7 {} | ^^^^^ there are too many leading `super` keywords error: trait implementation can only be restricted to ancestor modules - --> $DIR/restriction_resolution_errors.rs:34:21 + --> $DIR/restriction_resolution_errors.rs:37:21 | LL | pub impl(in self::f) trait L1 {} | ^^^^^^^ error: trait implementation can only be restricted to ancestor modules - --> $DIR/restriction_resolution_errors.rs:38:21 + --> $DIR/restriction_resolution_errors.rs:41:21 | LL | pub impl(in super::h) trait L3 {} | ^^^^^^^^ error: trait implementation can only be restricted to ancestor modules - --> $DIR/restriction_resolution_errors.rs:47:13 + --> $DIR/restriction_resolution_errors.rs:50:13 | LL | pub impl(in crate::a) trait T13 {} | ^^^^^^^^ error[E0433]: too many leading `super` keywords - --> $DIR/restriction_resolution_errors.rs:54:10 + --> $DIR/restriction_resolution_errors.rs:57:10 | LL | pub impl(super) trait T17 {} | ^^^^^ there are too many leading `super` keywords error: trait implementation can only be restricted to ancestor modules - --> $DIR/restriction_resolution_errors.rs:57:13 + --> $DIR/restriction_resolution_errors.rs:59:13 + | +LL | pub impl(in external) trait T18 {} + | ^^^^^^^^ + +error: trait implementation can only be restricted to ancestor modules + --> $DIR/restriction_resolution_errors.rs:62:13 | LL | pub impl(in crate::j) trait L4 {} | ^^^^^^^^ error: trait implementation can only be restricted to ancestor modules - --> $DIR/restriction_resolution_errors.rs:73:21 + --> $DIR/restriction_resolution_errors.rs:78:21 | LL | pub impl(in crate::m2) trait U2 {} | ^^^^^^^^^ error: trait implementation can only be restricted to ancestor modules - --> $DIR/restriction_resolution_errors.rs:75:21 + --> $DIR/restriction_resolution_errors.rs:80:21 | LL | pub impl(in m6::m5) trait U4 {} | ^^^^^^ error[E0433]: cannot find module or crate `a` in this scope - --> $DIR/restriction_resolution_errors.rs:12:21 + --> $DIR/restriction_resolution_errors.rs:15:21 | LL | pub impl(in a::b) trait T1 {} | ^ use of unresolved module or unlinked crate `a` @@ -81,25 +87,25 @@ LL + use a; | error[E0433]: cannot find module `c` in the crate root - --> $DIR/restriction_resolution_errors.rs:20:28 + --> $DIR/restriction_resolution_errors.rs:23:28 | LL | pub impl(in crate::c) trait T5 {} | ^ not found in the crate root error[E0577]: expected module, found enum `super::E` - --> $DIR/restriction_resolution_errors.rs:22:21 + --> $DIR/restriction_resolution_errors.rs:25:21 | LL | pub impl(in super::E) trait T6 {} | ^^^^^^^^ not a module error[E0577]: expected module, found enum `super::G` - --> $DIR/restriction_resolution_errors.rs:36:21 + --> $DIR/restriction_resolution_errors.rs:39:21 | LL | pub impl(in super::G) trait L2 {} | ^^^^^^^^ not a module error[E0577]: expected module, found enum `crate::a::E` - --> $DIR/restriction_resolution_errors.rs:49:13 + --> $DIR/restriction_resolution_errors.rs:52:13 | LL | pub mod b { | --------- similarly named module `b` defined here @@ -114,7 +120,7 @@ LL + pub impl(in crate::a::b) trait T14 {} | error[E0577]: expected module, found enum `crate::I` - --> $DIR/restriction_resolution_errors.rs:59:13 + --> $DIR/restriction_resolution_errors.rs:64:13 | LL | pub mod a { | --------- similarly named module `a` defined here @@ -129,12 +135,12 @@ LL + pub impl(in crate::a) trait L5 {} | error[E0577]: expected module, found enum `m7` - --> $DIR/restriction_resolution_errors.rs:76:21 + --> $DIR/restriction_resolution_errors.rs:81:21 | LL | pub impl(in m7) trait U5 {} | ^^ not a module -error: aborting due to 18 previous errors +error: aborting due to 19 previous errors Some errors have detailed explanations: E0433, E0577. For more information about an error, try `rustc --explain E0433`. From 72c1d65037573c0a9004ff036898a8a56577735b Mon Sep 17 00:00:00 2001 From: CoCo-Japan-pan <115922543+CoCo-Japan-pan@users.noreply.github.com> Date: Sun, 5 Apr 2026 20:54:36 +0900 Subject: [PATCH 12/25] Inline `krate` --- compiler/rustc_hir_analysis/src/coherence/mod.rs | 4 +--- compiler/rustc_middle/src/ty/trait_def.rs | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/coherence/mod.rs b/compiler/rustc_hir_analysis/src/coherence/mod.rs index c60b40c61231f..261be884025f1 100644 --- a/compiler/rustc_hir_analysis/src/coherence/mod.rs +++ b/compiler/rustc_hir_analysis/src/coherence/mod.rs @@ -105,9 +105,7 @@ fn enforce_trait_manually_implementable( return Err(tcx.dcx().emit_err(errors::ImplOfRestrictedTrait { impl_span: impl_header_span, restriction_span: trait_def.impl_restriction.expect_span(), - restriction_path: trait_def - .impl_restriction - .restriction_path(tcx, rustc_hir::def_id::LOCAL_CRATE), + restriction_path: trait_def.impl_restriction.restriction_path(tcx), })); } Ok(()) diff --git a/compiler/rustc_middle/src/ty/trait_def.rs b/compiler/rustc_middle/src/ty/trait_def.rs index 70eba8308da47..a58a9c9d75792 100644 --- a/compiler/rustc_middle/src/ty/trait_def.rs +++ b/compiler/rustc_middle/src/ty/trait_def.rs @@ -133,11 +133,11 @@ impl ImplRestrictionKind { } /// Obtain the path of the restriction. If unrestricted, an empty string is returned. - pub fn restriction_path(self, tcx: TyCtxt<'_>, krate: rustc_span::def_id::CrateNum) -> String { + pub fn restriction_path(self, tcx: TyCtxt<'_>) -> String { match self { ImplRestrictionKind::Unrestricted => String::new(), ImplRestrictionKind::Restricted(restricted_to, _) => { - if restricted_to.krate == krate { + if restricted_to.krate == rustc_hir::def_id::LOCAL_CRATE { with_crate_prefix!(with_no_trimmed_paths!(tcx.def_path_str(restricted_to))) } else { with_no_trimmed_paths!(tcx.def_path_str(restricted_to)) From de2408aeedea7ca8c9d0792c329b15aef7c2f5e7 Mon Sep 17 00:00:00 2001 From: Ariel Ben-Yehuda Date: Mon, 24 Nov 2025 13:56:38 +0000 Subject: [PATCH 13/25] Implement `-Z allow-partial-mitigations` (RFC 3855) This implements `-Z allow-partial-mitigations` as an unstable option, currently with support for control-flow-guard and stack-protector. As a difference from the RFC, we have `-Z allow-partial-mitigations=!foo` rather than `-Z deny-partial-mitigations=foo`, since I couldn't find an easy way to have an allow/deny pair of flags where the latter flag wins. To allow for stabilization, this is only enabled starting from the next edition. Maybe a better policy is possible (bikeshed). --- compiler/rustc_interface/src/passes.rs | 3 +- compiler/rustc_metadata/src/creader.rs | 51 +++++- compiler/rustc_metadata/src/errors.rs | 18 ++ compiler/rustc_metadata/src/rmeta/decoder.rs | 18 +- compiler/rustc_metadata/src/rmeta/encoder.rs | 11 +- compiler/rustc_metadata/src/rmeta/mod.rs | 3 +- .../rustc_metadata/src/rmeta/parameterized.rs | 1 + compiler/rustc_session/src/config.rs | 12 ++ compiler/rustc_session/src/options.rs | 173 +++++++++++++++++- compiler/rustc_target/src/spec/mod.rs | 1 + tests/ui/README.md | 6 + .../err-allow-partial-mitigations.both.stderr | 92 ++++++++++ ...r-allow-partial-mitigations.disable.stderr | 47 +++++ ...-partial-mitigations.enable-disable.stderr | 47 +++++ .../err-allow-partial-mitigations.rs | 43 +++++ .../err-allow-partial-mitigations.sp.stderr | 47 +++++ ...ow-partial-mitigations.wrong-enable.stderr | 47 +++++ .../ok-allow-partial-mitigations-minicore.rs | 18 ++ .../ok-allow-partial-mitigations.rs | 11 ++ 19 files changed, 639 insertions(+), 10 deletions(-) create mode 100644 tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.both.stderr create mode 100644 tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.disable.stderr create mode 100644 tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.enable-disable.stderr create mode 100644 tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.rs create mode 100644 tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.sp.stderr create mode 100644 tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.wrong-enable.stderr create mode 100644 tests/ui/allow-partial-mitigations/ok-allow-partial-mitigations-minicore.rs create mode 100644 tests/ui/allow-partial-mitigations/ok-allow-partial-mitigations.rs diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index 43efce545fc28..1f4b6bee0394b 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -303,8 +303,7 @@ fn configure_and_expand( resolver.resolve_crate(&krate); - CStore::from_tcx(tcx).report_incompatible_target_modifiers(tcx, &krate); - CStore::from_tcx(tcx).report_incompatible_async_drop_feature(tcx, &krate); + CStore::from_tcx(tcx).report_session_incompatibilities(tcx, &krate); krate } diff --git a/compiler/rustc_metadata/src/creader.rs b/compiler/rustc_metadata/src/creader.rs index 3c8ea1a9f43d4..7434a3f9e29bc 100644 --- a/compiler/rustc_metadata/src/creader.rs +++ b/compiler/rustc_metadata/src/creader.rs @@ -1,5 +1,6 @@ //! Validates all used crates and extern libraries and loads their metadata +use std::collections::BTreeMap; use std::error::Error; use std::path::Path; use std::str::FromStr; @@ -24,8 +25,8 @@ use rustc_middle::ty::data_structures::IndexSet; use rustc_middle::ty::{TyCtxt, TyCtxtFeed}; use rustc_proc_macro::bridge::client::ProcMacro; use rustc_session::config::{ - CrateType, ExtendedTargetModifierInfo, ExternLocation, Externs, OptionsTargetModifiers, - TargetModifier, + CrateType, EnforcedMitigationLevel, ExtendedTargetModifierInfo, ExternLocation, Externs, + OptionsTargetModifiers, TargetModifier, }; use rustc_session::cstore::{CrateDepKind, CrateSource, ExternCrate, ExternCrateSource}; use rustc_session::output::validate_crate_name; @@ -463,6 +464,12 @@ impl CStore { } } + pub fn report_session_incompatibilities(&self, tcx: TyCtxt<'_>, krate: &Crate) { + self.report_incompatible_target_modifiers(tcx, krate); + self.report_incompatible_enforced_mitigations(tcx, krate); + self.report_incompatible_async_drop_feature(tcx, krate); + } + pub fn report_incompatible_target_modifiers(&self, tcx: TyCtxt<'_>, krate: &Crate) { for flag_name in &tcx.sess.opts.cg.unsafe_allow_abi_mismatch { if !OptionsTargetModifiers::is_target_modifier(flag_name) { @@ -484,6 +491,46 @@ impl CStore { } } + pub fn report_incompatible_enforced_mitigations(&self, tcx: TyCtxt<'_>, krate: &Crate) { + let my_mitigations = tcx.sess.gather_enabled_enforced_mitigations(); + let mut my_mitigations: BTreeMap<_, _> = my_mitigations + .iter() + .filter(|mitigation| mitigation.kind.enforced_since() <= tcx.sess.edition()) + .map(|mitigation| (mitigation.kind, mitigation)) + .collect(); + for skipped_mitigation in tcx.sess.opts.allowed_partial_mitigations() { + my_mitigations.remove(&skipped_mitigation); + } + const MAX_ERRORS_PER_MITIGATION: usize = 5; + let mut errors_per_mitigation = BTreeMap::new(); + for (_cnum, data) in self.iter_crate_data() { + if data.is_proc_macro_crate() { + continue; + } + let their_mitigations = data.enforced_mitigations(); + for my_mitigation in my_mitigations.values() { + let their_mitigation = their_mitigations + .iter() + .find(|mitigation| mitigation.kind == my_mitigation.kind) + .map_or(EnforcedMitigationLevel::Enabled(false), |m| m.level); + if their_mitigation < my_mitigation.level { + let errors = errors_per_mitigation.entry(my_mitigation.kind).or_insert(0); + if *errors >= MAX_ERRORS_PER_MITIGATION { + continue; + } + *errors += 1; + + tcx.dcx().emit_err(errors::MitigationLessStrictInDependency { + span: krate.spans.inner_span.shrink_to_lo(), + mitigation_name: my_mitigation.kind.to_string(), + mitigation_level: my_mitigation.level.level_str().to_string(), + extern_crate: data.name(), + }); + } + } + } + } + // Report about async drop types in dependency if async drop feature is disabled pub fn report_incompatible_async_drop_feature(&self, tcx: TyCtxt<'_>, krate: &Crate) { if tcx.features().async_drop() { diff --git a/compiler/rustc_metadata/src/errors.rs b/compiler/rustc_metadata/src/errors.rs index 8b2895d70004a..79568dcec81ec 100644 --- a/compiler/rustc_metadata/src/errors.rs +++ b/compiler/rustc_metadata/src/errors.rs @@ -696,3 +696,21 @@ pub(crate) struct UnusedCrateDependency { pub extern_crate: Symbol, pub local_crate: Symbol, } + +#[derive(Diagnostic)] +#[diag( + "your program uses the crate `{$extern_crate}`, that is not compiled with `{$mitigation_name}{$mitigation_level}` enabled" +)] +#[note( + "recompile `{$extern_crate}` with `{$mitigation_name}{$mitigation_level}` enabled, or use `-Z allow-partial-mitigations={$mitigation_name}` to allow creating an artifact that has the mitigation only partially enabled " +)] +#[help( + "it is possible to disable `-Z allow-partial-mitigations={$mitigation_name}` via `-Z deny-partial-mitigations={$mitigation_name}`" +)] +pub struct MitigationLessStrictInDependency { + #[primary_span] + pub span: Span, + pub mitigation_name: String, + pub mitigation_level: String, + pub extern_crate: Symbol, +} diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index fc541f952d229..613501805f729 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -29,7 +29,7 @@ use rustc_middle::{bug, implement_ty_decoder}; use rustc_proc_macro::bridge::client::ProcMacro; use rustc_serialize::opaque::MemDecoder; use rustc_serialize::{Decodable, Decoder}; -use rustc_session::config::TargetModifier; +use rustc_session::config::{EnforcedMitigation, TargetModifier}; use rustc_session::cstore::{CrateSource, ExternCrate}; use rustc_span::hygiene::HygieneDecodeContext; use rustc_span::{ @@ -78,9 +78,12 @@ impl MetadataBlob { /// own crate numbers. pub(crate) type CrateNumMap = IndexVec; -/// Target modifiers - abi or exploit mitigations flags +/// Target modifiers - abi or exploit mitigations flags that cause unsoundness when mixed pub(crate) type TargetModifiers = Vec; +/// Enforced Mitigations +pub(crate) type EnforcedMitigations = Vec; + pub(crate) struct CrateMetadata { /// The primary crate data - binary metadata blob. blob: MetadataBlob, @@ -959,6 +962,13 @@ impl CrateRoot { ) -> impl ExactSizeIterator { self.target_modifiers.decode(metadata) } + + pub(crate) fn decode_enforced_mitigations<'a>( + &self, + metadata: &'a MetadataBlob, + ) -> impl ExactSizeIterator { + self.enforced_mitigations.decode(metadata) + } } impl<'a> CrateMetadataRef<'a> { @@ -1941,6 +1951,10 @@ impl CrateMetadata { self.root.decode_target_modifiers(&self.blob).collect() } + pub(crate) fn enforced_mitigations(&self) -> EnforcedMitigations { + self.root.decode_enforced_mitigations(&self.blob).collect() + } + /// Keep `new_extern_crate` if it looks better in diagnostics pub(crate) fn update_extern_crate_diagnostics( &mut self, diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 8bf919dab8e79..ae7c3310f7a9c 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -27,7 +27,7 @@ use rustc_middle::ty::codec::TyEncoder; use rustc_middle::ty::fast_reject::{self, TreatParams}; use rustc_middle::{bug, span_bug}; use rustc_serialize::{Decodable, Decoder, Encodable, Encoder, opaque}; -use rustc_session::config::{CrateType, OptLevel, TargetModifier}; +use rustc_session::config::{CrateType, EnforcedMitigation, OptLevel, TargetModifier}; use rustc_span::hygiene::HygieneEncodeContext; use rustc_span::{ ByteSymbol, ExternalSource, FileName, SourceFile, SpanData, SpanEncoder, StableSourceFileId, @@ -715,6 +715,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { // `SourceFiles` we actually need to encode. let source_map = stat!("source-map", || self.encode_source_map()); let target_modifiers = stat!("target-modifiers", || self.encode_target_modifiers()); + let enforced_mitigations = + stat!("enforced-mitigations", || self.encode_enforced_mitigations()); let root = stat!("final", || { let attrs = tcx.hir_krate_attrs(); @@ -758,6 +760,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { foreign_modules, source_map, target_modifiers, + enforced_mitigations, traits, impls, incoherent_impls, @@ -2104,6 +2107,12 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { self.lazy_array(tcx.sess.opts.gather_target_modifiers()) } + fn encode_enforced_mitigations(&mut self) -> LazyArray { + empty_proc_macro!(self); + let tcx = self.tcx; + self.lazy_array(tcx.sess.gather_enabled_enforced_mitigations()) + } + fn encode_lib_features(&mut self) -> LazyArray<(Symbol, FeatureStability)> { empty_proc_macro!(self); let tcx = self.tcx; diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index 9dee913e8389c..9128a52c90fd2 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -36,7 +36,7 @@ use rustc_middle::ty::fast_reject::SimplifiedType; use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_middle::util::Providers; use rustc_serialize::opaque::FileEncoder; -use rustc_session::config::{SymbolManglingVersion, TargetModifier}; +use rustc_session::config::{EnforcedMitigation, SymbolManglingVersion, TargetModifier}; use rustc_session::cstore::{CrateDepKind, ForeignModule, LinkagePreference, NativeLib}; use rustc_span::edition::Edition; use rustc_span::hygiene::{ExpnIndex, MacroKind, SyntaxContextKey}; @@ -286,6 +286,7 @@ pub(crate) struct CrateRoot { source_map: LazyTable>>, target_modifiers: LazyArray, + enforced_mitigations: LazyArray, compiler_builtins: bool, needs_allocator: bool, diff --git a/compiler/rustc_metadata/src/rmeta/parameterized.rs b/compiler/rustc_metadata/src/rmeta/parameterized.rs index 8a9de07836db4..ee22a08850bca 100644 --- a/compiler/rustc_metadata/src/rmeta/parameterized.rs +++ b/compiler/rustc_metadata/src/rmeta/parameterized.rs @@ -119,6 +119,7 @@ trivially_parameterized_over_tcx! { rustc_middle::ty::Visibility, rustc_middle::ty::adjustment::CoerceUnsizedInfo, rustc_middle::ty::fast_reject::SimplifiedType, + rustc_session::config::EnforcedMitigation, rustc_session::config::TargetModifier, rustc_session::cstore::ForeignModule, rustc_session::cstore::LinkagePreference, diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index e4ef1d40d72d5..bdc07c5ad528e 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -1494,6 +1494,18 @@ impl Options { pub fn autodiff_enabled(&self) -> bool { self.unstable_opts.autodiff.contains(&AutoDiff::Enable) } + + pub fn allowed_partial_mitigations(&self) -> impl Iterator { + let mut result = BTreeSet::default(); + for mitigation in &self.unstable_opts.allow_partial_mitigations { + if mitigation.enabled { + result.insert(mitigation.kind); + } else { + result.remove(&mitigation.kind); + } + } + result.into_iter() + } } impl UnstableOptions { diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index 027a7045791e4..2fac722473762 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -1,7 +1,7 @@ use std::collections::BTreeMap; use std::num::{IntErrorKind, NonZero}; use std::path::PathBuf; -use std::str; +use std::str::{self, FromStr}; use rustc_abi::Align; use rustc_data_structures::fx::FxIndexMap; @@ -11,7 +11,7 @@ use rustc_errors::{ColorConfig, TerminalUrl}; use rustc_feature::UnstableFeatures; use rustc_hashes::Hash64; use rustc_hir::attrs::CollapseMacroDebuginfo; -use rustc_macros::{BlobDecodable, Encodable}; +use rustc_macros::{BlobDecodable, Decodable, Encodable}; use rustc_span::edition::Edition; use rustc_span::{RealFileName, RemapPathScopeComponents, SourceFileHashAlgorithm}; use rustc_target::spec::{ @@ -84,6 +84,151 @@ pub struct TargetModifier { pub value_name: String, } +#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Encodable, BlobDecodable)] +pub enum EnforcedMitigationLevel { + // Enabled(false) should be the bottom of the Ord hierarchy + Enabled(bool), + StackProtector(StackProtector), +} + +impl EnforcedMitigationLevel { + pub fn level_str(&self) -> &'static str { + match self { + EnforcedMitigationLevel::StackProtector(StackProtector::All) => "=all", + EnforcedMitigationLevel::StackProtector(StackProtector::Basic) => "=basic", + EnforcedMitigationLevel::StackProtector(StackProtector::Strong) => "=strong", + // currently `=disabled` should not appear + EnforcedMitigationLevel::Enabled(false) => "=disabled", + EnforcedMitigationLevel::StackProtector(StackProtector::None) + | EnforcedMitigationLevel::Enabled(true) => "", + } + } +} + +impl std::fmt::Display for EnforcedMitigationLevel { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + EnforcedMitigationLevel::StackProtector(StackProtector::All) => { + write!(f, "all") + } + EnforcedMitigationLevel::StackProtector(StackProtector::Basic) => { + write!(f, "basic") + } + EnforcedMitigationLevel::StackProtector(StackProtector::Strong) => { + write!(f, "strong") + } + EnforcedMitigationLevel::Enabled(true) => { + write!(f, "enabled") + } + EnforcedMitigationLevel::StackProtector(StackProtector::None) + | EnforcedMitigationLevel::Enabled(false) => { + write!(f, "disabled") + } + } + } +} + +impl From for EnforcedMitigationLevel { + fn from(value: bool) -> Self { + EnforcedMitigationLevel::Enabled(value) + } +} + +impl From for EnforcedMitigationLevel { + fn from(value: StackProtector) -> Self { + EnforcedMitigationLevel::StackProtector(value) + } +} + +pub struct EnforcedMitigationKindParseError; + +#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Encodable, Decodable)] +pub struct MitigationEnablement { + pub kind: EnforcedMitigationKind, + pub enabled: bool, +} + +macro_rules! intersperse { + ($sep:expr, ($first:expr $(, $rest:expr)* $(,)?)) => { + concat!($first $(, $sep, $rest)*) + }; +} + +macro_rules! enforced_mitigations { + ([$self:ident] enum $kind:ident {$(($name:ident, $text:expr, $since:ident, $code:expr)),*}) => { + #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Encodable, BlobDecodable)] + pub enum EnforcedMitigationKind { + $($name),* + } + + impl std::fmt::Display for EnforcedMitigationKind { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + $(EnforcedMitigationKind::$name => write!(f, $text)),* + } + } + } + + impl EnforcedMitigationKind { + const KINDS: &'static str = concat!("comma-separated list of mitigation kinds (available: ", + intersperse!(", ", ($(concat!("`", $text, "`")),*)), ")"); + } + + impl FromStr for EnforcedMitigationKind { + type Err = EnforcedMitigationKindParseError; + + fn from_str(v: &str) -> Result { + match v { + $($text => Ok(EnforcedMitigationKind::$name)),* + , + _ => Err(EnforcedMitigationKindParseError), + } + } + } + + #[allow(unused)] + impl EnforcedMitigationKind { + pub fn enforced_since(&self) -> Edition { + match self { + // Should change the enforced-since edition of StackProtector to 2015 + // (all editions) when `-C stack-protector` is stabilized. + $(EnforcedMitigationKind::$name => Edition::$since),* + } + } + } + + impl Session { + pub fn gather_enabled_enforced_mitigations(&$self) -> Vec { + let mut mitigations = [ + $( + EnforcedMitigation { + kind: EnforcedMitigationKind::$name, + level: From::from($code), + } + ),* + ]; + mitigations.sort(); + mitigations.into_iter().collect() + } + } + } +} + +enforced_mitigations! { + [self] + enum EnforcedMitigationKind { + (StackProtector, "stack-protector", EditionFuture, self.stack_protector()), + (ControlFlowGuard, "control-flow-guard", EditionFuture, self.opts.cg.control_flow_guard == CFGuard::Checks) + } +} + +/// Enforced mitigations, see [RFC 3855](https://github.com/rust-lang/rfcs/pull/3855) +#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Encodable, BlobDecodable)] +pub struct EnforcedMitigation { + pub kind: EnforcedMitigationKind, + pub level: EnforcedMitigationLevel, +} + mod target_modifier_consistency_check { use super::*; pub(super) fn sanitizer(l: &TargetModifier, r: Option<&TargetModifier>) -> bool { @@ -889,6 +1034,7 @@ mod desc { "either a boolean (`yes`, `no`, `on`, `off`, etc), or `nll` (default: `nll`)"; pub(crate) const parse_align: &str = "a number that is a power of 2 between 1 and 2^29"; pub(crate) const parse_assert_incr_state: &str = "one of: `loaded`, `not-loaded`"; + pub(crate) const parse_allow_partial_mitigations: &str = super::EnforcedMitigationKind::KINDS; } pub mod parse { @@ -2062,6 +2208,7 @@ pub mod parse { true } +<<<<<<< HEAD pub(crate) fn parse_assert_incr_state( slot: &mut Option, v: Option<&str>, @@ -2072,6 +2219,26 @@ pub mod parse { _ => return false, }; true +======= + pub(crate) fn parse_allow_partial_mitigations( + slot: &mut Vec, + v: Option<&str>, + ) -> bool { + match v { + Some(s) => { + for sub in s.split(',') { + let (sub, enabled) = + if sub.starts_with('!') { (&sub[1..], false) } else { (sub, true) }; + match sub.parse() { + Ok(kind) => slot.push(MitigationEnablement { kind, enabled }), + Err(_) => return false, + } + } + true + } + None => false, + } +>>>>>>> 615d0911bd2 (Implement `-Z allow-partial-mitigations` (RFC 3855)) } } @@ -2237,6 +2404,8 @@ options! { // tidy-alphabetical-start allow_features: Option> = (None, parse_opt_comma_list, [TRACKED], "only allow the listed language features to be enabled in code (comma separated)"), + allow_partial_mitigations: Vec = (Vec::new(), parse_allow_partial_mitigations, [UNTRACKED], + "Allow mitigations not enabled for all dependency crates (comma separated list)"), always_encode_mir: bool = (false, parse_bool, [TRACKED], "encode MIR of all functions into the crate metadata (default: no)"), annotate_moves: AnnotateMoves = (AnnotateMoves::Disabled, parse_annotate_moves, [TRACKED], diff --git a/compiler/rustc_target/src/spec/mod.rs b/compiler/rustc_target/src/spec/mod.rs index 68d6162bd590e..a533220197d05 100644 --- a/compiler/rustc_target/src/spec/mod.rs +++ b/compiler/rustc_target/src/spec/mod.rs @@ -1347,6 +1347,7 @@ impl FramePointer { crate::target_spec_enum! { /// Controls use of stack canaries. + #[derive(Encodable, BlobDecodable, HashStable_Generic)] pub enum StackProtector { /// Disable stack canary generation. None = "none", diff --git a/tests/ui/README.md b/tests/ui/README.md index a9e7f022c2b60..ab9b58aa4981f 100644 --- a/tests/ui/README.md +++ b/tests/ui/README.md @@ -22,6 +22,12 @@ These tests exercise `#![feature(allocator_api)]` and the `#[global_allocator]` See [Allocator traits and `std::heap` #32838](https://github.com/rust-lang/rust/issues/32838). +## `tests/ui/allow-partial-mitigations` + +These tests exercise the check against partial mitigation enforcement. + +See [the mitigation enforcement RFC](https://github.com/rust-lang/rfcs/pull/3855). + ## `tests/ui/annotate-moves` These tests exercise the `annotate-moves` feature. diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.both.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.both.stderr new file mode 100644 index 0000000000000..35801653495ef --- /dev/null +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.both.stderr @@ -0,0 +1,92 @@ +error: your program uses the crate `std`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations.rs:13:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `std` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `std`, that is not compiled with `control-flow-guard` enabled + --> $DIR/err-allow-partial-mitigations.rs:13:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `std` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` + +error: your program uses the crate `core`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations.rs:13:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `core` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `core`, that is not compiled with `control-flow-guard` enabled + --> $DIR/err-allow-partial-mitigations.rs:13:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `core` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` + +error: your program uses the crate `alloc`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations.rs:13:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `alloc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `alloc`, that is not compiled with `control-flow-guard` enabled + --> $DIR/err-allow-partial-mitigations.rs:13:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `alloc` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` + +error: your program uses the crate `compiler_builtins`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations.rs:13:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `compiler_builtins` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `compiler_builtins`, that is not compiled with `control-flow-guard` enabled + --> $DIR/err-allow-partial-mitigations.rs:13:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `compiler_builtins` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` + +error: your program uses the crate `libc`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations.rs:13:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `libc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `libc`, that is not compiled with `control-flow-guard` enabled + --> $DIR/err-allow-partial-mitigations.rs:13:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `libc` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` + +error: aborting due to 10 previous errors + diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.disable.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.disable.stderr new file mode 100644 index 0000000000000..f0e48e02347b3 --- /dev/null +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.disable.stderr @@ -0,0 +1,47 @@ +error: your program uses the crate `std`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations.rs:13:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `std` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `core`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations.rs:13:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `core` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `alloc`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations.rs:13:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `alloc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `compiler_builtins`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations.rs:13:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `compiler_builtins` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `libc`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations.rs:13:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `libc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: aborting due to 5 previous errors + diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.enable-disable.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.enable-disable.stderr new file mode 100644 index 0000000000000..f0e48e02347b3 --- /dev/null +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.enable-disable.stderr @@ -0,0 +1,47 @@ +error: your program uses the crate `std`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations.rs:13:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `std` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `core`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations.rs:13:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `core` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `alloc`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations.rs:13:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `alloc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `compiler_builtins`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations.rs:13:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `compiler_builtins` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `libc`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations.rs:13:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `libc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: aborting due to 5 previous errors + diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.rs b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.rs new file mode 100644 index 0000000000000..0aa1c5933ea53 --- /dev/null +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.rs @@ -0,0 +1,43 @@ +// ignore-tidy-linelength +//@ revisions: sp both disable enable-disable wrong-enable +//@ check-fail +//@ ignore-nvptx64 stack protector is not supported +//@ ignore-wasm32-unknown-unknown stack protector is not supported +//@ edition:future +//@ [both] compile-flags: -Z unstable-options -C control-flow-guard=on -Z stack-protector=all +//@ [sp] compile-flags: -Z unstable-options -Z stack-protector=all +//@ [disable] compile-flags: -Z unstable-options -Z allow-partial-mitigations=!stack-protector -Z stack-protector=all +//@ [enable-disable] compile-flags: -Z unstable-options -Z allow-partial-mitigations=stack-protector -Z allow-partial-mitigations=!stack-protector -Z stack-protector=all +//@ [wrong-enable] compile-flags: -Z unstable-options -Z allow-partial-mitigations=control-flow-guard -Z stack-protector=all + +fn main() {} +//[both]~^ ERROR that is not compiled with +//[both]~| ERROR that is not compiled with +//[both]~| ERROR that is not compiled with +//[both]~| ERROR that is not compiled with +//[both]~| ERROR that is not compiled with +//[both]~| ERROR that is not compiled with +//[both]~| ERROR that is not compiled with +//[both]~| ERROR that is not compiled with +//[both]~| ERROR that is not compiled with +//[both]~| ERROR that is not compiled with +//[sp]~^^^^^^^^^^^ ERROR that is not compiled with +//[sp]~| ERROR that is not compiled with +//[sp]~| ERROR that is not compiled with +//[sp]~| ERROR that is not compiled with +//[sp]~| ERROR that is not compiled with +//[disable]~^^^^^^^^^^^^^^^^ ERROR that is not compiled with +//[disable]~| ERROR that is not compiled with +//[disable]~| ERROR that is not compiled with +//[disable]~| ERROR that is not compiled with +//[disable]~| ERROR that is not compiled with +//[enable-disable]~^^^^^^^^^^^^^^^^^^^^^ ERROR that is not compiled with +//[enable-disable]~| ERROR that is not compiled with +//[enable-disable]~| ERROR that is not compiled with +//[enable-disable]~| ERROR that is not compiled with +//[enable-disable]~| ERROR that is not compiled with +//[wrong-enable]~^^^^^^^^^^^^^^^^^^^^^^^^^^ ERROR that is not compiled with +//[wrong-enable]~| ERROR that is not compiled with +//[wrong-enable]~| ERROR that is not compiled with +//[wrong-enable]~| ERROR that is not compiled with +//[wrong-enable]~| ERROR that is not compiled with diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.sp.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.sp.stderr new file mode 100644 index 0000000000000..f0e48e02347b3 --- /dev/null +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.sp.stderr @@ -0,0 +1,47 @@ +error: your program uses the crate `std`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations.rs:13:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `std` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `core`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations.rs:13:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `core` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `alloc`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations.rs:13:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `alloc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `compiler_builtins`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations.rs:13:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `compiler_builtins` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `libc`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations.rs:13:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `libc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: aborting due to 5 previous errors + diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.wrong-enable.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.wrong-enable.stderr new file mode 100644 index 0000000000000..f0e48e02347b3 --- /dev/null +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.wrong-enable.stderr @@ -0,0 +1,47 @@ +error: your program uses the crate `std`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations.rs:13:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `std` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `core`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations.rs:13:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `core` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `alloc`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations.rs:13:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `alloc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `compiler_builtins`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations.rs:13:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `compiler_builtins` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `libc`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations.rs:13:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `libc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: aborting due to 5 previous errors + diff --git a/tests/ui/allow-partial-mitigations/ok-allow-partial-mitigations-minicore.rs b/tests/ui/allow-partial-mitigations/ok-allow-partial-mitigations-minicore.rs new file mode 100644 index 0000000000000..16eecd455dbe0 --- /dev/null +++ b/tests/ui/allow-partial-mitigations/ok-allow-partial-mitigations-minicore.rs @@ -0,0 +1,18 @@ +// ignore-tidy-linelength +//@ check-pass +//@ add-minicore +//@ edition:future +//@ revisions: default deny +//@[default] compile-flags: -Z unstable-options -Z stack-protector=all +//@[deny] compile-flags: -Z allow-partial-mitigations=!stack-protector -Z unstable-options -Z stack-protector=all + +// ^ enables stack-protector for both minicore and this crate + +#![crate_type = "lib"] +#![feature(no_core)] +#![no_std] +#![no_core] + +extern crate minicore; + +pub fn foo() {} diff --git a/tests/ui/allow-partial-mitigations/ok-allow-partial-mitigations.rs b/tests/ui/allow-partial-mitigations/ok-allow-partial-mitigations.rs new file mode 100644 index 0000000000000..c0ec555305841 --- /dev/null +++ b/tests/ui/allow-partial-mitigations/ok-allow-partial-mitigations.rs @@ -0,0 +1,11 @@ +// ignore-tidy-linelength +//@ revisions: sp both disable-enable +//@ check-pass +//@ edition:future +//@ ignore-nvptx64 stack protector is not supported +//@ ignore-wasm32-unknown-unknown stack protector is not supported +//@ [both] compile-flags: -Z unstable-options -Z allow-partial-mitigations=stack-protector,control-flow-guard -C control-flow-guard=on -Z stack-protector=all +//@ [sp] compile-flags: -Z unstable-options -Z allow-partial-mitigations=stack-protector -Z stack-protector=all +//@ [disable-enable] compile-flags: -Z unstable-options -Z allow-partial-mitigations=!stack-protector -Z allow-partial-mitigations=stack-protector -Z stack-protector=all + +fn main() {} From daedc77e8431d712a4f2351eb61b84e278bdfaea Mon Sep 17 00:00:00 2001 From: Ariel Ben-Yehuda Date: Wed, 26 Nov 2025 20:14:39 +0000 Subject: [PATCH 14/25] address review comments --- compiler/rustc_metadata/src/creader.rs | 6 +- compiler/rustc_metadata/src/rmeta/decoder.rs | 3 +- compiler/rustc_metadata/src/rmeta/encoder.rs | 3 +- compiler/rustc_metadata/src/rmeta/mod.rs | 3 +- .../rustc_metadata/src/rmeta/parameterized.rs | 2 +- compiler/rustc_session/src/config.rs | 1 + compiler/rustc_session/src/options.rs | 161 ++---------------- .../src/options/enforced_mitigations.rs | 153 +++++++++++++++++ 8 files changed, 177 insertions(+), 155 deletions(-) create mode 100644 compiler/rustc_session/src/options/enforced_mitigations.rs diff --git a/compiler/rustc_metadata/src/creader.rs b/compiler/rustc_metadata/src/creader.rs index 7434a3f9e29bc..685aeb125c34c 100644 --- a/compiler/rustc_metadata/src/creader.rs +++ b/compiler/rustc_metadata/src/creader.rs @@ -24,9 +24,11 @@ use rustc_middle::bug; use rustc_middle::ty::data_structures::IndexSet; use rustc_middle::ty::{TyCtxt, TyCtxtFeed}; use rustc_proc_macro::bridge::client::ProcMacro; +use rustc_session::Session; +use rustc_session::config::enforced_mitigations::EnforcedMitigationLevel; use rustc_session::config::{ - CrateType, EnforcedMitigationLevel, ExtendedTargetModifierInfo, ExternLocation, Externs, - OptionsTargetModifiers, TargetModifier, + CrateType, ExtendedTargetModifierInfo, ExternLocation, Externs, OptionsTargetModifiers, + TargetModifier, }; use rustc_session::cstore::{CrateDepKind, CrateSource, ExternCrate, ExternCrateSource}; use rustc_session::output::validate_crate_name; diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 613501805f729..36bada1e59b1b 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -29,7 +29,8 @@ use rustc_middle::{bug, implement_ty_decoder}; use rustc_proc_macro::bridge::client::ProcMacro; use rustc_serialize::opaque::MemDecoder; use rustc_serialize::{Decodable, Decoder}; -use rustc_session::config::{EnforcedMitigation, TargetModifier}; +use rustc_session::config::TargetModifier; +use rustc_session::config::enforced_mitigations::EnforcedMitigation; use rustc_session::cstore::{CrateSource, ExternCrate}; use rustc_span::hygiene::HygieneDecodeContext; use rustc_span::{ diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index ae7c3310f7a9c..839b41cb4450d 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -27,7 +27,8 @@ use rustc_middle::ty::codec::TyEncoder; use rustc_middle::ty::fast_reject::{self, TreatParams}; use rustc_middle::{bug, span_bug}; use rustc_serialize::{Decodable, Decoder, Encodable, Encoder, opaque}; -use rustc_session::config::{CrateType, EnforcedMitigation, OptLevel, TargetModifier}; +use rustc_session::config::enforced_mitigations::EnforcedMitigation; +use rustc_session::config::{CrateType, OptLevel, TargetModifier}; use rustc_span::hygiene::HygieneEncodeContext; use rustc_span::{ ByteSymbol, ExternalSource, FileName, SourceFile, SpanData, SpanEncoder, StableSourceFileId, diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index 9128a52c90fd2..ce423f325b5c9 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -36,7 +36,8 @@ use rustc_middle::ty::fast_reject::SimplifiedType; use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_middle::util::Providers; use rustc_serialize::opaque::FileEncoder; -use rustc_session::config::{EnforcedMitigation, SymbolManglingVersion, TargetModifier}; +use rustc_session::config::enforced_mitigations::EnforcedMitigation; +use rustc_session::config::{SymbolManglingVersion, TargetModifier}; use rustc_session::cstore::{CrateDepKind, ForeignModule, LinkagePreference, NativeLib}; use rustc_span::edition::Edition; use rustc_span::hygiene::{ExpnIndex, MacroKind, SyntaxContextKey}; diff --git a/compiler/rustc_metadata/src/rmeta/parameterized.rs b/compiler/rustc_metadata/src/rmeta/parameterized.rs index ee22a08850bca..8debefd21a070 100644 --- a/compiler/rustc_metadata/src/rmeta/parameterized.rs +++ b/compiler/rustc_metadata/src/rmeta/parameterized.rs @@ -119,8 +119,8 @@ trivially_parameterized_over_tcx! { rustc_middle::ty::Visibility, rustc_middle::ty::adjustment::CoerceUnsizedInfo, rustc_middle::ty::fast_reject::SimplifiedType, - rustc_session::config::EnforcedMitigation, rustc_session::config::TargetModifier, + rustc_session::config::enforced_mitigations::EnforcedMitigation, rustc_session::cstore::ForeignModule, rustc_session::cstore::LinkagePreference, rustc_session::cstore::NativeLib, diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index bdc07c5ad528e..1a7b38c9f5189 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -33,6 +33,7 @@ use rustc_target::spec::{ use tracing::debug; pub use crate::config::cfg::{Cfg, CheckCfg, ExpectedValues}; +use crate::config::enforced_mitigations::EnforcedMitigationKind; use crate::config::native_libs::parse_native_libs; pub use crate::config::print_request::{PrintKind, PrintRequest}; use crate::errors::FileWriteFail; diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index 2fac722473762..09710a13b6c5a 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -1,7 +1,7 @@ use std::collections::BTreeMap; use std::num::{IntErrorKind, NonZero}; use std::path::PathBuf; -use std::str::{self, FromStr}; +use std::str; use rustc_abi::Align; use rustc_data_structures::fx::FxIndexMap; @@ -11,7 +11,7 @@ use rustc_errors::{ColorConfig, TerminalUrl}; use rustc_feature::UnstableFeatures; use rustc_hashes::Hash64; use rustc_hir::attrs::CollapseMacroDebuginfo; -use rustc_macros::{BlobDecodable, Decodable, Encodable}; +use rustc_macros::{BlobDecodable, Encodable}; use rustc_span::edition::Edition; use rustc_span::{RealFileName, RemapPathScopeComponents, SourceFileHashAlgorithm}; use rustc_target::spec::{ @@ -20,6 +20,7 @@ use rustc_target::spec::{ TargetTuple, TlsModel, }; +use crate::config::enforced_mitigations::MitigationEnablement; use crate::config::*; use crate::search_paths::SearchPath; use crate::utils::NativeLib; @@ -84,150 +85,7 @@ pub struct TargetModifier { pub value_name: String, } -#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Encodable, BlobDecodable)] -pub enum EnforcedMitigationLevel { - // Enabled(false) should be the bottom of the Ord hierarchy - Enabled(bool), - StackProtector(StackProtector), -} - -impl EnforcedMitigationLevel { - pub fn level_str(&self) -> &'static str { - match self { - EnforcedMitigationLevel::StackProtector(StackProtector::All) => "=all", - EnforcedMitigationLevel::StackProtector(StackProtector::Basic) => "=basic", - EnforcedMitigationLevel::StackProtector(StackProtector::Strong) => "=strong", - // currently `=disabled` should not appear - EnforcedMitigationLevel::Enabled(false) => "=disabled", - EnforcedMitigationLevel::StackProtector(StackProtector::None) - | EnforcedMitigationLevel::Enabled(true) => "", - } - } -} - -impl std::fmt::Display for EnforcedMitigationLevel { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - EnforcedMitigationLevel::StackProtector(StackProtector::All) => { - write!(f, "all") - } - EnforcedMitigationLevel::StackProtector(StackProtector::Basic) => { - write!(f, "basic") - } - EnforcedMitigationLevel::StackProtector(StackProtector::Strong) => { - write!(f, "strong") - } - EnforcedMitigationLevel::Enabled(true) => { - write!(f, "enabled") - } - EnforcedMitigationLevel::StackProtector(StackProtector::None) - | EnforcedMitigationLevel::Enabled(false) => { - write!(f, "disabled") - } - } - } -} - -impl From for EnforcedMitigationLevel { - fn from(value: bool) -> Self { - EnforcedMitigationLevel::Enabled(value) - } -} - -impl From for EnforcedMitigationLevel { - fn from(value: StackProtector) -> Self { - EnforcedMitigationLevel::StackProtector(value) - } -} - -pub struct EnforcedMitigationKindParseError; - -#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Encodable, Decodable)] -pub struct MitigationEnablement { - pub kind: EnforcedMitigationKind, - pub enabled: bool, -} - -macro_rules! intersperse { - ($sep:expr, ($first:expr $(, $rest:expr)* $(,)?)) => { - concat!($first $(, $sep, $rest)*) - }; -} - -macro_rules! enforced_mitigations { - ([$self:ident] enum $kind:ident {$(($name:ident, $text:expr, $since:ident, $code:expr)),*}) => { - #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Encodable, BlobDecodable)] - pub enum EnforcedMitigationKind { - $($name),* - } - - impl std::fmt::Display for EnforcedMitigationKind { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - $(EnforcedMitigationKind::$name => write!(f, $text)),* - } - } - } - - impl EnforcedMitigationKind { - const KINDS: &'static str = concat!("comma-separated list of mitigation kinds (available: ", - intersperse!(", ", ($(concat!("`", $text, "`")),*)), ")"); - } - - impl FromStr for EnforcedMitigationKind { - type Err = EnforcedMitigationKindParseError; - - fn from_str(v: &str) -> Result { - match v { - $($text => Ok(EnforcedMitigationKind::$name)),* - , - _ => Err(EnforcedMitigationKindParseError), - } - } - } - - #[allow(unused)] - impl EnforcedMitigationKind { - pub fn enforced_since(&self) -> Edition { - match self { - // Should change the enforced-since edition of StackProtector to 2015 - // (all editions) when `-C stack-protector` is stabilized. - $(EnforcedMitigationKind::$name => Edition::$since),* - } - } - } - - impl Session { - pub fn gather_enabled_enforced_mitigations(&$self) -> Vec { - let mut mitigations = [ - $( - EnforcedMitigation { - kind: EnforcedMitigationKind::$name, - level: From::from($code), - } - ),* - ]; - mitigations.sort(); - mitigations.into_iter().collect() - } - } - } -} - -enforced_mitigations! { - [self] - enum EnforcedMitigationKind { - (StackProtector, "stack-protector", EditionFuture, self.stack_protector()), - (ControlFlowGuard, "control-flow-guard", EditionFuture, self.opts.cg.control_flow_guard == CFGuard::Checks) - } -} - -/// Enforced mitigations, see [RFC 3855](https://github.com/rust-lang/rfcs/pull/3855) -#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Encodable, BlobDecodable)] -pub struct EnforcedMitigation { - pub kind: EnforcedMitigationKind, - pub level: EnforcedMitigationLevel, -} +pub mod enforced_mitigations; mod target_modifier_consistency_check { use super::*; @@ -1033,14 +891,20 @@ mod desc { pub(crate) const parse_mir_include_spans: &str = "either a boolean (`yes`, `no`, `on`, `off`, etc), or `nll` (default: `nll`)"; pub(crate) const parse_align: &str = "a number that is a power of 2 between 1 and 2^29"; +<<<<<<< HEAD pub(crate) const parse_assert_incr_state: &str = "one of: `loaded`, `not-loaded`"; pub(crate) const parse_allow_partial_mitigations: &str = super::EnforcedMitigationKind::KINDS; +======= + pub(crate) const parse_allow_partial_mitigations: &str = + super::enforced_mitigations::EnforcedMitigationKind::KINDS; +>>>>>>> be3a932517e (address review comments) } pub mod parse { use std::str::FromStr; pub(crate) use super::*; + use crate::config::enforced_mitigations::MitigationEnablement; pub(crate) const MAX_THREADS_CAP: usize = 256; /// Ignore the value. Used for removed options where we don't actually want to store @@ -2208,7 +2072,6 @@ pub mod parse { true } -<<<<<<< HEAD pub(crate) fn parse_assert_incr_state( slot: &mut Option, v: Option<&str>, @@ -2219,7 +2082,8 @@ pub mod parse { _ => return false, }; true -======= + } + pub(crate) fn parse_allow_partial_mitigations( slot: &mut Vec, v: Option<&str>, @@ -2238,7 +2102,6 @@ pub mod parse { } None => false, } ->>>>>>> 615d0911bd2 (Implement `-Z allow-partial-mitigations` (RFC 3855)) } } diff --git a/compiler/rustc_session/src/options/enforced_mitigations.rs b/compiler/rustc_session/src/options/enforced_mitigations.rs new file mode 100644 index 0000000000000..7b965595fbdde --- /dev/null +++ b/compiler/rustc_session/src/options/enforced_mitigations.rs @@ -0,0 +1,153 @@ +use std::str::FromStr; + +use rustc_macros::{BlobDecodable, Encodable}; +use rustc_span::edition::Edition; +use rustc_target::spec::StackProtector; + +use crate::Session; +use crate::options::CFGuard; + +#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Encodable, BlobDecodable)] +pub enum EnforcedMitigationLevel { + // Enabled(false) should be the bottom of the Ord hierarchy + Enabled(bool), + StackProtector(StackProtector), +} + +impl EnforcedMitigationLevel { + pub fn level_str(&self) -> &'static str { + match self { + EnforcedMitigationLevel::StackProtector(StackProtector::All) => "=all", + EnforcedMitigationLevel::StackProtector(StackProtector::Basic) => "=basic", + EnforcedMitigationLevel::StackProtector(StackProtector::Strong) => "=strong", + // currently `=disabled` should not appear + EnforcedMitigationLevel::Enabled(false) => "=disabled", + EnforcedMitigationLevel::StackProtector(StackProtector::None) + | EnforcedMitigationLevel::Enabled(true) => "", + } + } +} + +impl std::fmt::Display for EnforcedMitigationLevel { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + EnforcedMitigationLevel::StackProtector(StackProtector::All) => { + write!(f, "all") + } + EnforcedMitigationLevel::StackProtector(StackProtector::Basic) => { + write!(f, "basic") + } + EnforcedMitigationLevel::StackProtector(StackProtector::Strong) => { + write!(f, "strong") + } + EnforcedMitigationLevel::Enabled(true) => { + write!(f, "enabled") + } + EnforcedMitigationLevel::StackProtector(StackProtector::None) + | EnforcedMitigationLevel::Enabled(false) => { + write!(f, "disabled") + } + } + } +} + +impl From for EnforcedMitigationLevel { + fn from(value: bool) -> Self { + EnforcedMitigationLevel::Enabled(value) + } +} + +impl From for EnforcedMitigationLevel { + fn from(value: StackProtector) -> Self { + EnforcedMitigationLevel::StackProtector(value) + } +} + +pub struct EnforcedMitigationKindParseError; + +#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Encodable, BlobDecodable)] +pub struct MitigationEnablement { + pub kind: EnforcedMitigationKind, + pub enabled: bool, +} + +macro_rules! intersperse { + ($sep:expr, ($first:expr $(, $rest:expr)* $(,)?)) => { + concat!($first $(, $sep, $rest)*) + }; +} + +macro_rules! enforced_mitigations { + ([$self:ident] enum $kind:ident {$(($name:ident, $text:expr, $since:ident, $code:expr)),*}) => { + #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Encodable, BlobDecodable)] + pub enum EnforcedMitigationKind { + $($name),* + } + + impl std::fmt::Display for EnforcedMitigationKind { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + $(EnforcedMitigationKind::$name => write!(f, $text)),* + } + } + } + + impl EnforcedMitigationKind { + pub(crate) const KINDS: &'static str = concat!("comma-separated list of mitigation kinds (available: ", + intersperse!(", ", ($(concat!("`", $text, "`")),*)), ")"); + } + + impl FromStr for EnforcedMitigationKind { + type Err = EnforcedMitigationKindParseError; + + fn from_str(v: &str) -> Result { + match v { + $($text => Ok(EnforcedMitigationKind::$name)),* + , + _ => Err(EnforcedMitigationKindParseError), + } + } + } + + #[allow(unused)] + impl EnforcedMitigationKind { + pub fn enforced_since(&self) -> Edition { + match self { + // Should change the enforced-since edition of StackProtector to 2015 + // (all editions) when `-C stack-protector` is stabilized. + $(EnforcedMitigationKind::$name => Edition::$since),* + } + } + } + + impl Session { + pub fn gather_enabled_enforced_mitigations(&$self) -> Vec { + let mut mitigations = [ + $( + EnforcedMitigation { + kind: EnforcedMitigationKind::$name, + level: From::from($code), + } + ),* + ]; + mitigations.sort(); + mitigations.into_iter().collect() + } + } + } +} + +enforced_mitigations! { + [self] + enum EnforcedMitigationKind { + (StackProtector, "stack-protector", EditionFuture, self.stack_protector()), + (ControlFlowGuard, "control-flow-guard", EditionFuture, self.opts.cg.control_flow_guard == CFGuard::Checks) + } +} + +/// Enforced mitigations, see [RFC 3855](https://github.com/rust-lang/rfcs/pull/3855) +#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Encodable, BlobDecodable)] +pub struct EnforcedMitigation { + pub kind: EnforcedMitigationKind, + pub level: EnforcedMitigationLevel, +} From 51b2b93239a99970db1bbdc41f9765e12a184b03 Mon Sep 17 00:00:00 2001 From: Ariel Ben-Yehuda Date: Thu, 27 Nov 2025 15:00:09 +0000 Subject: [PATCH 15/25] allow denying mitigations in earlier editions --- compiler/rustc_metadata/src/creader.rs | 9 ++-- compiler/rustc_session/src/config.rs | 13 ----- .../src/options/enforced_mitigations.rs | 29 ++++++++++++ ...low-partial-mitigations-current-edition.rs | 17 +++++++ ...partial-mitigations-current-edition.stderr | 47 +++++++++++++++++++ 5 files changed, 96 insertions(+), 19 deletions(-) create mode 100644 tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-current-edition.rs create mode 100644 tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-current-edition.stderr diff --git a/compiler/rustc_metadata/src/creader.rs b/compiler/rustc_metadata/src/creader.rs index 685aeb125c34c..d1ee527eeea42 100644 --- a/compiler/rustc_metadata/src/creader.rs +++ b/compiler/rustc_metadata/src/creader.rs @@ -495,12 +495,9 @@ impl CStore { pub fn report_incompatible_enforced_mitigations(&self, tcx: TyCtxt<'_>, krate: &Crate) { let my_mitigations = tcx.sess.gather_enabled_enforced_mitigations(); - let mut my_mitigations: BTreeMap<_, _> = my_mitigations - .iter() - .filter(|mitigation| mitigation.kind.enforced_since() <= tcx.sess.edition()) - .map(|mitigation| (mitigation.kind, mitigation)) - .collect(); - for skipped_mitigation in tcx.sess.opts.allowed_partial_mitigations() { + let mut my_mitigations: BTreeMap<_, _> = + my_mitigations.iter().map(|mitigation| (mitigation.kind, mitigation)).collect(); + for skipped_mitigation in tcx.sess.opts.allowed_partial_mitigations(tcx.sess.edition()) { my_mitigations.remove(&skipped_mitigation); } const MAX_ERRORS_PER_MITIGATION: usize = 5; diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index 1a7b38c9f5189..e4ef1d40d72d5 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -33,7 +33,6 @@ use rustc_target::spec::{ use tracing::debug; pub use crate::config::cfg::{Cfg, CheckCfg, ExpectedValues}; -use crate::config::enforced_mitigations::EnforcedMitigationKind; use crate::config::native_libs::parse_native_libs; pub use crate::config::print_request::{PrintKind, PrintRequest}; use crate::errors::FileWriteFail; @@ -1495,18 +1494,6 @@ impl Options { pub fn autodiff_enabled(&self) -> bool { self.unstable_opts.autodiff.contains(&AutoDiff::Enable) } - - pub fn allowed_partial_mitigations(&self) -> impl Iterator { - let mut result = BTreeSet::default(); - for mitigation in &self.unstable_opts.allow_partial_mitigations { - if mitigation.enabled { - result.insert(mitigation.kind); - } else { - result.remove(&mitigation.kind); - } - } - result.into_iter() - } } impl UnstableOptions { diff --git a/compiler/rustc_session/src/options/enforced_mitigations.rs b/compiler/rustc_session/src/options/enforced_mitigations.rs index 7b965595fbdde..731b519eda58e 100644 --- a/compiler/rustc_session/src/options/enforced_mitigations.rs +++ b/compiler/rustc_session/src/options/enforced_mitigations.rs @@ -1,3 +1,4 @@ +use std::collections::BTreeSet; use std::str::FromStr; use rustc_macros::{BlobDecodable, Encodable}; @@ -5,6 +6,7 @@ use rustc_span::edition::Edition; use rustc_target::spec::StackProtector; use crate::Session; +use crate::config::Options; use crate::options::CFGuard; #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Encodable, BlobDecodable)] @@ -120,6 +122,12 @@ macro_rules! enforced_mitigations { } } + impl Options { + pub fn all_enforced_mitigations(&self) -> impl Iterator { + [$(EnforcedMitigationKind::$name),*].into_iter() + } + } + impl Session { pub fn gather_enabled_enforced_mitigations(&$self) -> Vec { let mut mitigations = [ @@ -151,3 +159,24 @@ pub struct EnforcedMitigation { pub kind: EnforcedMitigationKind, pub level: EnforcedMitigationLevel, } + +impl Options { + // Return the list of mitigations that are allowed to be partial + pub fn allowed_partial_mitigations( + &self, + edition: Edition, + ) -> impl Iterator { + let mut result: BTreeSet<_> = self + .all_enforced_mitigations() + .filter(|mitigation| mitigation.enforced_since() > edition) + .collect(); + for mitigation in &self.unstable_opts.allow_partial_mitigations { + if mitigation.enabled { + result.insert(mitigation.kind); + } else { + result.remove(&mitigation.kind); + } + } + result.into_iter() + } +} diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-current-edition.rs b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-current-edition.rs new file mode 100644 index 0000000000000..1299e5e003cac --- /dev/null +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-current-edition.rs @@ -0,0 +1,17 @@ +// ignore-tidy-linelength +//@ check-fail +//@ ignore-nvptx64 stack protector is not supported +//@ ignore-wasm32-unknown-unknown stack protector is not supported +//@ edition: 2024 +//@ compile-flags: -Z allow-partial-mitigations=!control-flow-guard -C control-flow-guard=on + +// check that in edition 2024, it is still possible to explicitly +// disallow partial mitigations (in edition=future, they are +// disallowed by default) + +fn main() {} +//~^ ERROR that is not compiled with +//~| ERROR that is not compiled with +//~| ERROR that is not compiled with +//~| ERROR that is not compiled with +//~| ERROR that is not compiled with diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-current-edition.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-current-edition.stderr new file mode 100644 index 0000000000000..02e9154986791 --- /dev/null +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-current-edition.stderr @@ -0,0 +1,47 @@ +error: your program uses the crate `std`, that is not compiled with `control-flow-guard` enabled + --> $DIR/err-allow-partial-mitigations-current-edition.rs:12:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `std` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` + +error: your program uses the crate `core`, that is not compiled with `control-flow-guard` enabled + --> $DIR/err-allow-partial-mitigations-current-edition.rs:12:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `core` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` + +error: your program uses the crate `alloc`, that is not compiled with `control-flow-guard` enabled + --> $DIR/err-allow-partial-mitigations-current-edition.rs:12:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `alloc` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` + +error: your program uses the crate `compiler_builtins`, that is not compiled with `control-flow-guard` enabled + --> $DIR/err-allow-partial-mitigations-current-edition.rs:12:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `compiler_builtins` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` + +error: your program uses the crate `libc`, that is not compiled with `control-flow-guard` enabled + --> $DIR/err-allow-partial-mitigations-current-edition.rs:12:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `libc` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` + +error: aborting due to 5 previous errors + From eb89ca9b776fe4f55e1541c2fb7aba4d9ae38b7e Mon Sep 17 00:00:00 2001 From: Ariel Ben-Yehuda Date: Sun, 7 Dec 2025 12:14:23 +0000 Subject: [PATCH 16/25] enforced => enforcable mitigation --- compiler/rustc_metadata/src/creader.rs | 12 +-- compiler/rustc_metadata/src/rmeta/decoder.rs | 16 ++-- compiler/rustc_metadata/src/rmeta/encoder.rs | 12 +-- compiler/rustc_metadata/src/rmeta/mod.rs | 4 +- .../rustc_metadata/src/rmeta/parameterized.rs | 2 +- compiler/rustc_session/src/options.rs | 12 +-- ...tigations.rs => enforcable_mitigations.rs} | 84 +++++++++---------- 7 files changed, 69 insertions(+), 73 deletions(-) rename compiler/rustc_session/src/options/{enforced_mitigations.rs => enforcable_mitigations.rs} (60%) diff --git a/compiler/rustc_metadata/src/creader.rs b/compiler/rustc_metadata/src/creader.rs index d1ee527eeea42..972c1e2df9323 100644 --- a/compiler/rustc_metadata/src/creader.rs +++ b/compiler/rustc_metadata/src/creader.rs @@ -25,7 +25,7 @@ use rustc_middle::ty::data_structures::IndexSet; use rustc_middle::ty::{TyCtxt, TyCtxtFeed}; use rustc_proc_macro::bridge::client::ProcMacro; use rustc_session::Session; -use rustc_session::config::enforced_mitigations::EnforcedMitigationLevel; +use rustc_session::config::enforcable_mitigations::EnforcableMitigationLevel; use rustc_session::config::{ CrateType, ExtendedTargetModifierInfo, ExternLocation, Externs, OptionsTargetModifiers, TargetModifier, @@ -468,7 +468,7 @@ impl CStore { pub fn report_session_incompatibilities(&self, tcx: TyCtxt<'_>, krate: &Crate) { self.report_incompatible_target_modifiers(tcx, krate); - self.report_incompatible_enforced_mitigations(tcx, krate); + self.report_incompatible_enforcable_mitigations(tcx, krate); self.report_incompatible_async_drop_feature(tcx, krate); } @@ -493,8 +493,8 @@ impl CStore { } } - pub fn report_incompatible_enforced_mitigations(&self, tcx: TyCtxt<'_>, krate: &Crate) { - let my_mitigations = tcx.sess.gather_enabled_enforced_mitigations(); + pub fn report_incompatible_enforcable_mitigations(&self, tcx: TyCtxt<'_>, krate: &Crate) { + let my_mitigations = tcx.sess.gather_enabled_enforcable_mitigations(); let mut my_mitigations: BTreeMap<_, _> = my_mitigations.iter().map(|mitigation| (mitigation.kind, mitigation)).collect(); for skipped_mitigation in tcx.sess.opts.allowed_partial_mitigations(tcx.sess.edition()) { @@ -506,12 +506,12 @@ impl CStore { if data.is_proc_macro_crate() { continue; } - let their_mitigations = data.enforced_mitigations(); + let their_mitigations = data.enabled_enforcable_mitigations(); for my_mitigation in my_mitigations.values() { let their_mitigation = their_mitigations .iter() .find(|mitigation| mitigation.kind == my_mitigation.kind) - .map_or(EnforcedMitigationLevel::Enabled(false), |m| m.level); + .map_or(EnforcableMitigationLevel::Enabled(false), |m| m.level); if their_mitigation < my_mitigation.level { let errors = errors_per_mitigation.entry(my_mitigation.kind).or_insert(0); if *errors >= MAX_ERRORS_PER_MITIGATION { diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 36bada1e59b1b..7344be2cb3178 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -30,7 +30,7 @@ use rustc_proc_macro::bridge::client::ProcMacro; use rustc_serialize::opaque::MemDecoder; use rustc_serialize::{Decodable, Decoder}; use rustc_session::config::TargetModifier; -use rustc_session::config::enforced_mitigations::EnforcedMitigation; +use rustc_session::config::enforcable_mitigations::EnforcableMitigation; use rustc_session::cstore::{CrateSource, ExternCrate}; use rustc_span::hygiene::HygieneDecodeContext; use rustc_span::{ @@ -82,8 +82,8 @@ pub(crate) type CrateNumMap = IndexVec; /// Target modifiers - abi or exploit mitigations flags that cause unsoundness when mixed pub(crate) type TargetModifiers = Vec; -/// Enforced Mitigations -pub(crate) type EnforcedMitigations = Vec; +/// The set of enforcable mitigations (RFC 3855) that are currently enabled for this crate +pub(crate) type EnforcableMitigations = Vec; pub(crate) struct CrateMetadata { /// The primary crate data - binary metadata blob. @@ -964,11 +964,11 @@ impl CrateRoot { self.target_modifiers.decode(metadata) } - pub(crate) fn decode_enforced_mitigations<'a>( + pub(crate) fn decode_enforcable_mitigations<'a>( &self, metadata: &'a MetadataBlob, - ) -> impl ExactSizeIterator { - self.enforced_mitigations.decode(metadata) + ) -> impl ExactSizeIterator { + self.enforcable_mitigations.decode(metadata) } } @@ -1952,8 +1952,8 @@ impl CrateMetadata { self.root.decode_target_modifiers(&self.blob).collect() } - pub(crate) fn enforced_mitigations(&self) -> EnforcedMitigations { - self.root.decode_enforced_mitigations(&self.blob).collect() + pub(crate) fn enabled_enforcable_mitigations(&self) -> EnforcableMitigations { + self.root.decode_enforcable_mitigations(&self.blob).collect() } /// Keep `new_extern_crate` if it looks better in diagnostics diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 839b41cb4450d..712c5aa0e2b83 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -27,7 +27,7 @@ use rustc_middle::ty::codec::TyEncoder; use rustc_middle::ty::fast_reject::{self, TreatParams}; use rustc_middle::{bug, span_bug}; use rustc_serialize::{Decodable, Decoder, Encodable, Encoder, opaque}; -use rustc_session::config::enforced_mitigations::EnforcedMitigation; +use rustc_session::config::enforcable_mitigations::EnforcableMitigation; use rustc_session::config::{CrateType, OptLevel, TargetModifier}; use rustc_span::hygiene::HygieneEncodeContext; use rustc_span::{ @@ -716,8 +716,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { // `SourceFiles` we actually need to encode. let source_map = stat!("source-map", || self.encode_source_map()); let target_modifiers = stat!("target-modifiers", || self.encode_target_modifiers()); - let enforced_mitigations = - stat!("enforced-mitigations", || self.encode_enforced_mitigations()); + let enforcable_mitigations = + stat!("enforced-mitigations", || self.encode_enabled_enforcable_mitigations()); let root = stat!("final", || { let attrs = tcx.hir_krate_attrs(); @@ -761,7 +761,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { foreign_modules, source_map, target_modifiers, - enforced_mitigations, + enforcable_mitigations, traits, impls, incoherent_impls, @@ -2108,10 +2108,10 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { self.lazy_array(tcx.sess.opts.gather_target_modifiers()) } - fn encode_enforced_mitigations(&mut self) -> LazyArray { + fn encode_enabled_enforcable_mitigations(&mut self) -> LazyArray { empty_proc_macro!(self); let tcx = self.tcx; - self.lazy_array(tcx.sess.gather_enabled_enforced_mitigations()) + self.lazy_array(tcx.sess.gather_enabled_enforcable_mitigations()) } fn encode_lib_features(&mut self) -> LazyArray<(Symbol, FeatureStability)> { diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index ce423f325b5c9..6d9627b9528d9 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -36,7 +36,7 @@ use rustc_middle::ty::fast_reject::SimplifiedType; use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_middle::util::Providers; use rustc_serialize::opaque::FileEncoder; -use rustc_session::config::enforced_mitigations::EnforcedMitigation; +use rustc_session::config::enforcable_mitigations::EnforcableMitigation; use rustc_session::config::{SymbolManglingVersion, TargetModifier}; use rustc_session::cstore::{CrateDepKind, ForeignModule, LinkagePreference, NativeLib}; use rustc_span::edition::Edition; @@ -287,7 +287,7 @@ pub(crate) struct CrateRoot { source_map: LazyTable>>, target_modifiers: LazyArray, - enforced_mitigations: LazyArray, + enforcable_mitigations: LazyArray, compiler_builtins: bool, needs_allocator: bool, diff --git a/compiler/rustc_metadata/src/rmeta/parameterized.rs b/compiler/rustc_metadata/src/rmeta/parameterized.rs index 8debefd21a070..b993932eb70dc 100644 --- a/compiler/rustc_metadata/src/rmeta/parameterized.rs +++ b/compiler/rustc_metadata/src/rmeta/parameterized.rs @@ -120,7 +120,7 @@ trivially_parameterized_over_tcx! { rustc_middle::ty::adjustment::CoerceUnsizedInfo, rustc_middle::ty::fast_reject::SimplifiedType, rustc_session::config::TargetModifier, - rustc_session::config::enforced_mitigations::EnforcedMitigation, + rustc_session::config::enforcable_mitigations::EnforcableMitigation, rustc_session::cstore::ForeignModule, rustc_session::cstore::LinkagePreference, rustc_session::cstore::NativeLib, diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index 09710a13b6c5a..4bf92c3304f89 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -20,7 +20,7 @@ use rustc_target::spec::{ TargetTuple, TlsModel, }; -use crate::config::enforced_mitigations::MitigationEnablement; +use crate::config::enforcable_mitigations::MitigationEnablement; use crate::config::*; use crate::search_paths::SearchPath; use crate::utils::NativeLib; @@ -85,7 +85,7 @@ pub struct TargetModifier { pub value_name: String, } -pub mod enforced_mitigations; +pub mod enforcable_mitigations; mod target_modifier_consistency_check { use super::*; @@ -891,20 +891,16 @@ mod desc { pub(crate) const parse_mir_include_spans: &str = "either a boolean (`yes`, `no`, `on`, `off`, etc), or `nll` (default: `nll`)"; pub(crate) const parse_align: &str = "a number that is a power of 2 between 1 and 2^29"; -<<<<<<< HEAD pub(crate) const parse_assert_incr_state: &str = "one of: `loaded`, `not-loaded`"; - pub(crate) const parse_allow_partial_mitigations: &str = super::EnforcedMitigationKind::KINDS; -======= pub(crate) const parse_allow_partial_mitigations: &str = - super::enforced_mitigations::EnforcedMitigationKind::KINDS; ->>>>>>> be3a932517e (address review comments) + super::enforcable_mitigations::EnforcableMitigationKind::KINDS; } pub mod parse { use std::str::FromStr; pub(crate) use super::*; - use crate::config::enforced_mitigations::MitigationEnablement; + use crate::config::enforcable_mitigations::MitigationEnablement; pub(crate) const MAX_THREADS_CAP: usize = 256; /// Ignore the value. Used for removed options where we don't actually want to store diff --git a/compiler/rustc_session/src/options/enforced_mitigations.rs b/compiler/rustc_session/src/options/enforcable_mitigations.rs similarity index 60% rename from compiler/rustc_session/src/options/enforced_mitigations.rs rename to compiler/rustc_session/src/options/enforcable_mitigations.rs index 731b519eda58e..667dd0b8aadc7 100644 --- a/compiler/rustc_session/src/options/enforced_mitigations.rs +++ b/compiler/rustc_session/src/options/enforcable_mitigations.rs @@ -10,66 +10,66 @@ use crate::config::Options; use crate::options::CFGuard; #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Encodable, BlobDecodable)] -pub enum EnforcedMitigationLevel { +pub enum EnforcableMitigationLevel { // Enabled(false) should be the bottom of the Ord hierarchy Enabled(bool), StackProtector(StackProtector), } -impl EnforcedMitigationLevel { +impl EnforcableMitigationLevel { pub fn level_str(&self) -> &'static str { match self { - EnforcedMitigationLevel::StackProtector(StackProtector::All) => "=all", - EnforcedMitigationLevel::StackProtector(StackProtector::Basic) => "=basic", - EnforcedMitigationLevel::StackProtector(StackProtector::Strong) => "=strong", + EnforcableMitigationLevel::StackProtector(StackProtector::All) => "=all", + EnforcableMitigationLevel::StackProtector(StackProtector::Basic) => "=basic", + EnforcableMitigationLevel::StackProtector(StackProtector::Strong) => "=strong", // currently `=disabled` should not appear - EnforcedMitigationLevel::Enabled(false) => "=disabled", - EnforcedMitigationLevel::StackProtector(StackProtector::None) - | EnforcedMitigationLevel::Enabled(true) => "", + EnforcableMitigationLevel::Enabled(false) => "=disabled", + EnforcableMitigationLevel::StackProtector(StackProtector::None) + | EnforcableMitigationLevel::Enabled(true) => "", } } } -impl std::fmt::Display for EnforcedMitigationLevel { +impl std::fmt::Display for EnforcableMitigationLevel { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - EnforcedMitigationLevel::StackProtector(StackProtector::All) => { + EnforcableMitigationLevel::StackProtector(StackProtector::All) => { write!(f, "all") } - EnforcedMitigationLevel::StackProtector(StackProtector::Basic) => { + EnforcableMitigationLevel::StackProtector(StackProtector::Basic) => { write!(f, "basic") } - EnforcedMitigationLevel::StackProtector(StackProtector::Strong) => { + EnforcableMitigationLevel::StackProtector(StackProtector::Strong) => { write!(f, "strong") } - EnforcedMitigationLevel::Enabled(true) => { + EnforcableMitigationLevel::Enabled(true) => { write!(f, "enabled") } - EnforcedMitigationLevel::StackProtector(StackProtector::None) - | EnforcedMitigationLevel::Enabled(false) => { + EnforcableMitigationLevel::StackProtector(StackProtector::None) + | EnforcableMitigationLevel::Enabled(false) => { write!(f, "disabled") } } } } -impl From for EnforcedMitigationLevel { +impl From for EnforcableMitigationLevel { fn from(value: bool) -> Self { - EnforcedMitigationLevel::Enabled(value) + EnforcableMitigationLevel::Enabled(value) } } -impl From for EnforcedMitigationLevel { +impl From for EnforcableMitigationLevel { fn from(value: StackProtector) -> Self { - EnforcedMitigationLevel::StackProtector(value) + EnforcableMitigationLevel::StackProtector(value) } } -pub struct EnforcedMitigationKindParseError; +pub struct EnforcableMitigationKindParseError; #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Encodable, BlobDecodable)] pub struct MitigationEnablement { - pub kind: EnforcedMitigationKind, + pub kind: EnforcableMitigationKind, pub enabled: bool, } @@ -82,58 +82,58 @@ macro_rules! intersperse { macro_rules! enforced_mitigations { ([$self:ident] enum $kind:ident {$(($name:ident, $text:expr, $since:ident, $code:expr)),*}) => { #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Encodable, BlobDecodable)] - pub enum EnforcedMitigationKind { + pub enum EnforcableMitigationKind { $($name),* } - impl std::fmt::Display for EnforcedMitigationKind { + impl std::fmt::Display for EnforcableMitigationKind { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - $(EnforcedMitigationKind::$name => write!(f, $text)),* + $(EnforcableMitigationKind::$name => write!(f, $text)),* } } } - impl EnforcedMitigationKind { + impl EnforcableMitigationKind { pub(crate) const KINDS: &'static str = concat!("comma-separated list of mitigation kinds (available: ", intersperse!(", ", ($(concat!("`", $text, "`")),*)), ")"); } - impl FromStr for EnforcedMitigationKind { - type Err = EnforcedMitigationKindParseError; + impl FromStr for EnforcableMitigationKind { + type Err = EnforcableMitigationKindParseError; - fn from_str(v: &str) -> Result { + fn from_str(v: &str) -> Result { match v { - $($text => Ok(EnforcedMitigationKind::$name)),* + $($text => Ok(EnforcableMitigationKind::$name)),* , - _ => Err(EnforcedMitigationKindParseError), + _ => Err(EnforcableMitigationKindParseError), } } } #[allow(unused)] - impl EnforcedMitigationKind { + impl EnforcableMitigationKind { pub fn enforced_since(&self) -> Edition { match self { // Should change the enforced-since edition of StackProtector to 2015 // (all editions) when `-C stack-protector` is stabilized. - $(EnforcedMitigationKind::$name => Edition::$since),* + $(EnforcableMitigationKind::$name => Edition::$since),* } } } impl Options { - pub fn all_enforced_mitigations(&self) -> impl Iterator { - [$(EnforcedMitigationKind::$name),*].into_iter() + pub fn all_enforced_mitigations(&self) -> impl Iterator { + [$(EnforcableMitigationKind::$name),*].into_iter() } } impl Session { - pub fn gather_enabled_enforced_mitigations(&$self) -> Vec { + pub fn gather_enabled_enforcable_mitigations(&$self) -> Vec { let mut mitigations = [ $( - EnforcedMitigation { - kind: EnforcedMitigationKind::$name, + EnforcableMitigation { + kind: EnforcableMitigationKind::$name, level: From::from($code), } ),* @@ -147,7 +147,7 @@ macro_rules! enforced_mitigations { enforced_mitigations! { [self] - enum EnforcedMitigationKind { + enum EnforcableMitigationKind { (StackProtector, "stack-protector", EditionFuture, self.stack_protector()), (ControlFlowGuard, "control-flow-guard", EditionFuture, self.opts.cg.control_flow_guard == CFGuard::Checks) } @@ -155,9 +155,9 @@ enforced_mitigations! { /// Enforced mitigations, see [RFC 3855](https://github.com/rust-lang/rfcs/pull/3855) #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Encodable, BlobDecodable)] -pub struct EnforcedMitigation { - pub kind: EnforcedMitigationKind, - pub level: EnforcedMitigationLevel, +pub struct EnforcableMitigation { + pub kind: EnforcableMitigationKind, + pub level: EnforcableMitigationLevel, } impl Options { @@ -165,7 +165,7 @@ impl Options { pub fn allowed_partial_mitigations( &self, edition: Edition, - ) -> impl Iterator { + ) -> impl Iterator { let mut result: BTreeSet<_> = self .all_enforced_mitigations() .filter(|mitigation| mitigation.enforced_since() > edition) From 3600f4cd2c07f632a83e8473c4ed98e475c4cfbe Mon Sep 17 00:00:00 2001 From: Ariel Ben-Yehuda Date: Wed, 10 Dec 2025 18:37:48 +0000 Subject: [PATCH 17/25] EnforcableMitigation => DeniedPartialMitigation --- compiler/rustc_metadata/src/creader.rs | 12 +-- compiler/rustc_metadata/src/rmeta/decoder.rs | 14 ++-- compiler/rustc_metadata/src/rmeta/encoder.rs | 12 +-- compiler/rustc_metadata/src/rmeta/mod.rs | 4 +- .../rustc_metadata/src/rmeta/parameterized.rs | 2 +- compiler/rustc_session/src/options.rs | 2 +- .../src/options/enforcable_mitigations.rs | 84 +++++++++---------- 7 files changed, 65 insertions(+), 65 deletions(-) diff --git a/compiler/rustc_metadata/src/creader.rs b/compiler/rustc_metadata/src/creader.rs index 972c1e2df9323..cee7a41a1182c 100644 --- a/compiler/rustc_metadata/src/creader.rs +++ b/compiler/rustc_metadata/src/creader.rs @@ -25,7 +25,7 @@ use rustc_middle::ty::data_structures::IndexSet; use rustc_middle::ty::{TyCtxt, TyCtxtFeed}; use rustc_proc_macro::bridge::client::ProcMacro; use rustc_session::Session; -use rustc_session::config::enforcable_mitigations::EnforcableMitigationLevel; +use rustc_session::config::enforcable_mitigations::DeniedPartialMitigationLevel; use rustc_session::config::{ CrateType, ExtendedTargetModifierInfo, ExternLocation, Externs, OptionsTargetModifiers, TargetModifier, @@ -468,7 +468,7 @@ impl CStore { pub fn report_session_incompatibilities(&self, tcx: TyCtxt<'_>, krate: &Crate) { self.report_incompatible_target_modifiers(tcx, krate); - self.report_incompatible_enforcable_mitigations(tcx, krate); + self.report_incompatible_denied_partial_mitigations(tcx, krate); self.report_incompatible_async_drop_feature(tcx, krate); } @@ -493,8 +493,8 @@ impl CStore { } } - pub fn report_incompatible_enforcable_mitigations(&self, tcx: TyCtxt<'_>, krate: &Crate) { - let my_mitigations = tcx.sess.gather_enabled_enforcable_mitigations(); + pub fn report_incompatible_denied_partial_mitigations(&self, tcx: TyCtxt<'_>, krate: &Crate) { + let my_mitigations = tcx.sess.gather_enabled_denied_partial_mitigations(); let mut my_mitigations: BTreeMap<_, _> = my_mitigations.iter().map(|mitigation| (mitigation.kind, mitigation)).collect(); for skipped_mitigation in tcx.sess.opts.allowed_partial_mitigations(tcx.sess.edition()) { @@ -506,12 +506,12 @@ impl CStore { if data.is_proc_macro_crate() { continue; } - let their_mitigations = data.enabled_enforcable_mitigations(); + let their_mitigations = data.enabled_denied_partial_mitigations(); for my_mitigation in my_mitigations.values() { let their_mitigation = their_mitigations .iter() .find(|mitigation| mitigation.kind == my_mitigation.kind) - .map_or(EnforcableMitigationLevel::Enabled(false), |m| m.level); + .map_or(DeniedPartialMitigationLevel::Enabled(false), |m| m.level); if their_mitigation < my_mitigation.level { let errors = errors_per_mitigation.entry(my_mitigation.kind).or_insert(0); if *errors >= MAX_ERRORS_PER_MITIGATION { diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 7344be2cb3178..00ca24c6b7501 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -30,7 +30,7 @@ use rustc_proc_macro::bridge::client::ProcMacro; use rustc_serialize::opaque::MemDecoder; use rustc_serialize::{Decodable, Decoder}; use rustc_session::config::TargetModifier; -use rustc_session::config::enforcable_mitigations::EnforcableMitigation; +use rustc_session::config::enforcable_mitigations::DeniedPartialMitigation; use rustc_session::cstore::{CrateSource, ExternCrate}; use rustc_span::hygiene::HygieneDecodeContext; use rustc_span::{ @@ -83,7 +83,7 @@ pub(crate) type CrateNumMap = IndexVec; pub(crate) type TargetModifiers = Vec; /// The set of enforcable mitigations (RFC 3855) that are currently enabled for this crate -pub(crate) type EnforcableMitigations = Vec; +pub(crate) type DeniedPartialMitigations = Vec; pub(crate) struct CrateMetadata { /// The primary crate data - binary metadata blob. @@ -964,11 +964,11 @@ impl CrateRoot { self.target_modifiers.decode(metadata) } - pub(crate) fn decode_enforcable_mitigations<'a>( + pub(crate) fn decode_denied_partial_mitigations<'a>( &self, metadata: &'a MetadataBlob, - ) -> impl ExactSizeIterator { - self.enforcable_mitigations.decode(metadata) + ) -> impl ExactSizeIterator { + self.denied_partial_mitigations.decode(metadata) } } @@ -1952,8 +1952,8 @@ impl CrateMetadata { self.root.decode_target_modifiers(&self.blob).collect() } - pub(crate) fn enabled_enforcable_mitigations(&self) -> EnforcableMitigations { - self.root.decode_enforcable_mitigations(&self.blob).collect() + pub(crate) fn enabled_denied_partial_mitigations(&self) -> DeniedPartialMitigations { + self.root.decode_denied_partial_mitigations(&self.blob).collect() } /// Keep `new_extern_crate` if it looks better in diagnostics diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 712c5aa0e2b83..ef5416ac427d5 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -27,7 +27,7 @@ use rustc_middle::ty::codec::TyEncoder; use rustc_middle::ty::fast_reject::{self, TreatParams}; use rustc_middle::{bug, span_bug}; use rustc_serialize::{Decodable, Decoder, Encodable, Encoder, opaque}; -use rustc_session::config::enforcable_mitigations::EnforcableMitigation; +use rustc_session::config::enforcable_mitigations::DeniedPartialMitigation; use rustc_session::config::{CrateType, OptLevel, TargetModifier}; use rustc_span::hygiene::HygieneEncodeContext; use rustc_span::{ @@ -716,8 +716,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { // `SourceFiles` we actually need to encode. let source_map = stat!("source-map", || self.encode_source_map()); let target_modifiers = stat!("target-modifiers", || self.encode_target_modifiers()); - let enforcable_mitigations = - stat!("enforced-mitigations", || self.encode_enabled_enforcable_mitigations()); + let denied_partial_mitigations = + stat!("enforced-mitigations", || self.encode_enabled_denied_partial_mitigations()); let root = stat!("final", || { let attrs = tcx.hir_krate_attrs(); @@ -761,7 +761,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { foreign_modules, source_map, target_modifiers, - enforcable_mitigations, + denied_partial_mitigations, traits, impls, incoherent_impls, @@ -2108,10 +2108,10 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { self.lazy_array(tcx.sess.opts.gather_target_modifiers()) } - fn encode_enabled_enforcable_mitigations(&mut self) -> LazyArray { + fn encode_enabled_denied_partial_mitigations(&mut self) -> LazyArray { empty_proc_macro!(self); let tcx = self.tcx; - self.lazy_array(tcx.sess.gather_enabled_enforcable_mitigations()) + self.lazy_array(tcx.sess.gather_enabled_denied_partial_mitigations()) } fn encode_lib_features(&mut self) -> LazyArray<(Symbol, FeatureStability)> { diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index 6d9627b9528d9..bcc0aa1f3be8c 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -36,7 +36,7 @@ use rustc_middle::ty::fast_reject::SimplifiedType; use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_middle::util::Providers; use rustc_serialize::opaque::FileEncoder; -use rustc_session::config::enforcable_mitigations::EnforcableMitigation; +use rustc_session::config::enforcable_mitigations::DeniedPartialMitigation; use rustc_session::config::{SymbolManglingVersion, TargetModifier}; use rustc_session::cstore::{CrateDepKind, ForeignModule, LinkagePreference, NativeLib}; use rustc_span::edition::Edition; @@ -287,7 +287,7 @@ pub(crate) struct CrateRoot { source_map: LazyTable>>, target_modifiers: LazyArray, - enforcable_mitigations: LazyArray, + denied_partial_mitigations: LazyArray, compiler_builtins: bool, needs_allocator: bool, diff --git a/compiler/rustc_metadata/src/rmeta/parameterized.rs b/compiler/rustc_metadata/src/rmeta/parameterized.rs index b993932eb70dc..35ea06b384536 100644 --- a/compiler/rustc_metadata/src/rmeta/parameterized.rs +++ b/compiler/rustc_metadata/src/rmeta/parameterized.rs @@ -120,7 +120,7 @@ trivially_parameterized_over_tcx! { rustc_middle::ty::adjustment::CoerceUnsizedInfo, rustc_middle::ty::fast_reject::SimplifiedType, rustc_session::config::TargetModifier, - rustc_session::config::enforcable_mitigations::EnforcableMitigation, + rustc_session::config::enforcable_mitigations::DeniedPartialMitigation, rustc_session::cstore::ForeignModule, rustc_session::cstore::LinkagePreference, rustc_session::cstore::NativeLib, diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index 4bf92c3304f89..c84c9318fcf2a 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -893,7 +893,7 @@ mod desc { pub(crate) const parse_align: &str = "a number that is a power of 2 between 1 and 2^29"; pub(crate) const parse_assert_incr_state: &str = "one of: `loaded`, `not-loaded`"; pub(crate) const parse_allow_partial_mitigations: &str = - super::enforcable_mitigations::EnforcableMitigationKind::KINDS; + super::enforcable_mitigations::DeniedPartialMitigationKind::KINDS; } pub mod parse { diff --git a/compiler/rustc_session/src/options/enforcable_mitigations.rs b/compiler/rustc_session/src/options/enforcable_mitigations.rs index 667dd0b8aadc7..851659f416e85 100644 --- a/compiler/rustc_session/src/options/enforcable_mitigations.rs +++ b/compiler/rustc_session/src/options/enforcable_mitigations.rs @@ -10,66 +10,66 @@ use crate::config::Options; use crate::options::CFGuard; #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Encodable, BlobDecodable)] -pub enum EnforcableMitigationLevel { +pub enum DeniedPartialMitigationLevel { // Enabled(false) should be the bottom of the Ord hierarchy Enabled(bool), StackProtector(StackProtector), } -impl EnforcableMitigationLevel { +impl DeniedPartialMitigationLevel { pub fn level_str(&self) -> &'static str { match self { - EnforcableMitigationLevel::StackProtector(StackProtector::All) => "=all", - EnforcableMitigationLevel::StackProtector(StackProtector::Basic) => "=basic", - EnforcableMitigationLevel::StackProtector(StackProtector::Strong) => "=strong", + DeniedPartialMitigationLevel::StackProtector(StackProtector::All) => "=all", + DeniedPartialMitigationLevel::StackProtector(StackProtector::Basic) => "=basic", + DeniedPartialMitigationLevel::StackProtector(StackProtector::Strong) => "=strong", // currently `=disabled` should not appear - EnforcableMitigationLevel::Enabled(false) => "=disabled", - EnforcableMitigationLevel::StackProtector(StackProtector::None) - | EnforcableMitigationLevel::Enabled(true) => "", + DeniedPartialMitigationLevel::Enabled(false) => "=disabled", + DeniedPartialMitigationLevel::StackProtector(StackProtector::None) + | DeniedPartialMitigationLevel::Enabled(true) => "", } } } -impl std::fmt::Display for EnforcableMitigationLevel { +impl std::fmt::Display for DeniedPartialMitigationLevel { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - EnforcableMitigationLevel::StackProtector(StackProtector::All) => { + DeniedPartialMitigationLevel::StackProtector(StackProtector::All) => { write!(f, "all") } - EnforcableMitigationLevel::StackProtector(StackProtector::Basic) => { + DeniedPartialMitigationLevel::StackProtector(StackProtector::Basic) => { write!(f, "basic") } - EnforcableMitigationLevel::StackProtector(StackProtector::Strong) => { + DeniedPartialMitigationLevel::StackProtector(StackProtector::Strong) => { write!(f, "strong") } - EnforcableMitigationLevel::Enabled(true) => { + DeniedPartialMitigationLevel::Enabled(true) => { write!(f, "enabled") } - EnforcableMitigationLevel::StackProtector(StackProtector::None) - | EnforcableMitigationLevel::Enabled(false) => { + DeniedPartialMitigationLevel::StackProtector(StackProtector::None) + | DeniedPartialMitigationLevel::Enabled(false) => { write!(f, "disabled") } } } } -impl From for EnforcableMitigationLevel { +impl From for DeniedPartialMitigationLevel { fn from(value: bool) -> Self { - EnforcableMitigationLevel::Enabled(value) + DeniedPartialMitigationLevel::Enabled(value) } } -impl From for EnforcableMitigationLevel { +impl From for DeniedPartialMitigationLevel { fn from(value: StackProtector) -> Self { - EnforcableMitigationLevel::StackProtector(value) + DeniedPartialMitigationLevel::StackProtector(value) } } -pub struct EnforcableMitigationKindParseError; +pub struct DeniedPartialMitigationKindParseError; #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Encodable, BlobDecodable)] pub struct MitigationEnablement { - pub kind: EnforcableMitigationKind, + pub kind: DeniedPartialMitigationKind, pub enabled: bool, } @@ -82,58 +82,58 @@ macro_rules! intersperse { macro_rules! enforced_mitigations { ([$self:ident] enum $kind:ident {$(($name:ident, $text:expr, $since:ident, $code:expr)),*}) => { #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Encodable, BlobDecodable)] - pub enum EnforcableMitigationKind { + pub enum DeniedPartialMitigationKind { $($name),* } - impl std::fmt::Display for EnforcableMitigationKind { + impl std::fmt::Display for DeniedPartialMitigationKind { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - $(EnforcableMitigationKind::$name => write!(f, $text)),* + $(DeniedPartialMitigationKind::$name => write!(f, $text)),* } } } - impl EnforcableMitigationKind { + impl DeniedPartialMitigationKind { pub(crate) const KINDS: &'static str = concat!("comma-separated list of mitigation kinds (available: ", intersperse!(", ", ($(concat!("`", $text, "`")),*)), ")"); } - impl FromStr for EnforcableMitigationKind { - type Err = EnforcableMitigationKindParseError; + impl FromStr for DeniedPartialMitigationKind { + type Err = DeniedPartialMitigationKindParseError; - fn from_str(v: &str) -> Result { + fn from_str(v: &str) -> Result { match v { - $($text => Ok(EnforcableMitigationKind::$name)),* + $($text => Ok(DeniedPartialMitigationKind::$name)),* , - _ => Err(EnforcableMitigationKindParseError), + _ => Err(DeniedPartialMitigationKindParseError), } } } #[allow(unused)] - impl EnforcableMitigationKind { + impl DeniedPartialMitigationKind { pub fn enforced_since(&self) -> Edition { match self { // Should change the enforced-since edition of StackProtector to 2015 // (all editions) when `-C stack-protector` is stabilized. - $(EnforcableMitigationKind::$name => Edition::$since),* + $(DeniedPartialMitigationKind::$name => Edition::$since),* } } } impl Options { - pub fn all_enforced_mitigations(&self) -> impl Iterator { - [$(EnforcableMitigationKind::$name),*].into_iter() + pub fn all_enforced_mitigations(&self) -> impl Iterator { + [$(DeniedPartialMitigationKind::$name),*].into_iter() } } impl Session { - pub fn gather_enabled_enforcable_mitigations(&$self) -> Vec { + pub fn gather_enabled_denied_partial_mitigations(&$self) -> Vec { let mut mitigations = [ $( - EnforcableMitigation { - kind: EnforcableMitigationKind::$name, + DeniedPartialMitigation { + kind: DeniedPartialMitigationKind::$name, level: From::from($code), } ),* @@ -147,7 +147,7 @@ macro_rules! enforced_mitigations { enforced_mitigations! { [self] - enum EnforcableMitigationKind { + enum DeniedPartialMitigationKind { (StackProtector, "stack-protector", EditionFuture, self.stack_protector()), (ControlFlowGuard, "control-flow-guard", EditionFuture, self.opts.cg.control_flow_guard == CFGuard::Checks) } @@ -155,9 +155,9 @@ enforced_mitigations! { /// Enforced mitigations, see [RFC 3855](https://github.com/rust-lang/rfcs/pull/3855) #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Encodable, BlobDecodable)] -pub struct EnforcableMitigation { - pub kind: EnforcableMitigationKind, - pub level: EnforcableMitigationLevel, +pub struct DeniedPartialMitigation { + pub kind: DeniedPartialMitigationKind, + pub level: DeniedPartialMitigationLevel, } impl Options { @@ -165,7 +165,7 @@ impl Options { pub fn allowed_partial_mitigations( &self, edition: Edition, - ) -> impl Iterator { + ) -> impl Iterator { let mut result: BTreeSet<_> = self .all_enforced_mitigations() .filter(|mitigation| mitigation.enforced_since() > edition) From cc2d560ebf8273f9f193e95935e45d590ea0fcb2 Mon Sep 17 00:00:00 2001 From: Ariel Ben-Yehuda Date: Wed, 10 Dec 2025 18:46:39 +0000 Subject: [PATCH 18/25] use -Z deny-partial-mitigations instead of -Z allow-partial-mitigations=! --- compiler/rustc_session/src/options.rs | 26 +++++- ...low-partial-mitigations-current-edition.rs | 2 +- .../err-allow-partial-mitigations.both.stderr | 20 ++-- ...r-allow-partial-mitigations.disable.stderr | 10 +- ...-partial-mitigations.enable-disable.stderr | 10 +- ....enable-separately-disable-together.stderr | 92 +++++++++++++++++++ .../err-allow-partial-mitigations.rs | 17 +++- .../err-allow-partial-mitigations.sp.stderr | 10 +- ...ow-partial-mitigations.wrong-enable.stderr | 10 +- .../ok-allow-partial-mitigations-minicore.rs | 2 +- .../ok-allow-partial-mitigations.rs | 2 +- 11 files changed, 162 insertions(+), 39 deletions(-) create mode 100644 tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.enable-separately-disable-together.stderr diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index c84c9318fcf2a..ac1e400ce88e5 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -694,6 +694,9 @@ impl CodegenOptions { // Sometimes different options need to build a common structure. // That structure can be kept in one of the options' fields, the others become dummy. macro_rules! redirect_field { + ($cg:ident.deny_partial_mitigations) => { + $cg.allow_partial_mitigations + }; ($cg:ident.link_arg) => { $cg.link_args }; @@ -894,6 +897,8 @@ mod desc { pub(crate) const parse_assert_incr_state: &str = "one of: `loaded`, `not-loaded`"; pub(crate) const parse_allow_partial_mitigations: &str = super::enforcable_mitigations::DeniedPartialMitigationKind::KINDS; + pub(crate) const parse_deny_partial_mitigations: &str = + super::enforcable_mitigations::DeniedPartialMitigationKind::KINDS; } pub mod parse { @@ -2080,15 +2085,14 @@ pub mod parse { true } - pub(crate) fn parse_allow_partial_mitigations( + fn parse_partial_mitigations( slot: &mut Vec, v: Option<&str>, + enabled: bool, ) -> bool { match v { Some(s) => { for sub in s.split(',') { - let (sub, enabled) = - if sub.starts_with('!') { (&sub[1..], false) } else { (sub, true) }; match sub.parse() { Ok(kind) => slot.push(MitigationEnablement { kind, enabled }), Err(_) => return false, @@ -2099,6 +2103,20 @@ pub mod parse { None => false, } } + + pub(crate) fn parse_allow_partial_mitigations( + slot: &mut Vec, + v: Option<&str>, + ) -> bool { + parse_partial_mitigations(slot, v, true) + } + + pub(crate) fn parse_deny_partial_mitigations( + slot: &mut Vec, + v: Option<&str>, + ) -> bool { + parse_partial_mitigations(slot, v, false) + } } options! { @@ -2332,6 +2350,8 @@ options! { "deduplicate identical diagnostics (default: yes)"), default_visibility: Option = (None, parse_opt_symbol_visibility, [TRACKED], "overrides the `default_visibility` setting of the target"), + deny_partial_mitigations: Vec = (Vec::new(), parse_deny_partial_mitigations, [UNTRACKED], + "Deny mitigations not enabled for all dependency crates (comma separated list)"), dep_info_omit_d_target: bool = (false, parse_bool, [TRACKED], "in dep-info output, omit targets for tracking dependencies of the dep-info files \ themselves (default: no)"), diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-current-edition.rs b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-current-edition.rs index 1299e5e003cac..d32c579a8cae1 100644 --- a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-current-edition.rs +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-current-edition.rs @@ -3,7 +3,7 @@ //@ ignore-nvptx64 stack protector is not supported //@ ignore-wasm32-unknown-unknown stack protector is not supported //@ edition: 2024 -//@ compile-flags: -Z allow-partial-mitigations=!control-flow-guard -C control-flow-guard=on +//@ compile-flags: -Z deny-partial-mitigations=control-flow-guard -C control-flow-guard=on // check that in edition 2024, it is still possible to explicitly // disallow partial mitigations (in edition=future, they are diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.both.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.both.stderr index 35801653495ef..50febbee060f2 100644 --- a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.both.stderr +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.both.stderr @@ -1,5 +1,5 @@ error: your program uses the crate `std`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:13:1 + --> $DIR/err-allow-partial-mitigations.rs:14:1 | LL | fn main() {} | ^ @@ -8,7 +8,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `std`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations.rs:13:1 + --> $DIR/err-allow-partial-mitigations.rs:14:1 | LL | fn main() {} | ^ @@ -17,7 +17,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` error: your program uses the crate `core`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:13:1 + --> $DIR/err-allow-partial-mitigations.rs:14:1 | LL | fn main() {} | ^ @@ -26,7 +26,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `core`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations.rs:13:1 + --> $DIR/err-allow-partial-mitigations.rs:14:1 | LL | fn main() {} | ^ @@ -35,7 +35,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` error: your program uses the crate `alloc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:13:1 + --> $DIR/err-allow-partial-mitigations.rs:14:1 | LL | fn main() {} | ^ @@ -44,7 +44,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `alloc`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations.rs:13:1 + --> $DIR/err-allow-partial-mitigations.rs:14:1 | LL | fn main() {} | ^ @@ -53,7 +53,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` error: your program uses the crate `compiler_builtins`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:13:1 + --> $DIR/err-allow-partial-mitigations.rs:14:1 | LL | fn main() {} | ^ @@ -62,7 +62,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `compiler_builtins`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations.rs:13:1 + --> $DIR/err-allow-partial-mitigations.rs:14:1 | LL | fn main() {} | ^ @@ -71,7 +71,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` error: your program uses the crate `libc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:13:1 + --> $DIR/err-allow-partial-mitigations.rs:14:1 | LL | fn main() {} | ^ @@ -80,7 +80,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `libc`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations.rs:13:1 + --> $DIR/err-allow-partial-mitigations.rs:14:1 | LL | fn main() {} | ^ diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.disable.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.disable.stderr index f0e48e02347b3..b8ca779ed3b6d 100644 --- a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.disable.stderr +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.disable.stderr @@ -1,5 +1,5 @@ error: your program uses the crate `std`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:13:1 + --> $DIR/err-allow-partial-mitigations.rs:14:1 | LL | fn main() {} | ^ @@ -8,7 +8,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `core`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:13:1 + --> $DIR/err-allow-partial-mitigations.rs:14:1 | LL | fn main() {} | ^ @@ -17,7 +17,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `alloc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:13:1 + --> $DIR/err-allow-partial-mitigations.rs:14:1 | LL | fn main() {} | ^ @@ -26,7 +26,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `compiler_builtins`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:13:1 + --> $DIR/err-allow-partial-mitigations.rs:14:1 | LL | fn main() {} | ^ @@ -35,7 +35,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `libc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:13:1 + --> $DIR/err-allow-partial-mitigations.rs:14:1 | LL | fn main() {} | ^ diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.enable-disable.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.enable-disable.stderr index f0e48e02347b3..b8ca779ed3b6d 100644 --- a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.enable-disable.stderr +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.enable-disable.stderr @@ -1,5 +1,5 @@ error: your program uses the crate `std`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:13:1 + --> $DIR/err-allow-partial-mitigations.rs:14:1 | LL | fn main() {} | ^ @@ -8,7 +8,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `core`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:13:1 + --> $DIR/err-allow-partial-mitigations.rs:14:1 | LL | fn main() {} | ^ @@ -17,7 +17,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `alloc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:13:1 + --> $DIR/err-allow-partial-mitigations.rs:14:1 | LL | fn main() {} | ^ @@ -26,7 +26,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `compiler_builtins`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:13:1 + --> $DIR/err-allow-partial-mitigations.rs:14:1 | LL | fn main() {} | ^ @@ -35,7 +35,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `libc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:13:1 + --> $DIR/err-allow-partial-mitigations.rs:14:1 | LL | fn main() {} | ^ diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.enable-separately-disable-together.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.enable-separately-disable-together.stderr new file mode 100644 index 0000000000000..50febbee060f2 --- /dev/null +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.enable-separately-disable-together.stderr @@ -0,0 +1,92 @@ +error: your program uses the crate `std`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations.rs:14:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `std` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `std`, that is not compiled with `control-flow-guard` enabled + --> $DIR/err-allow-partial-mitigations.rs:14:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `std` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` + +error: your program uses the crate `core`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations.rs:14:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `core` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `core`, that is not compiled with `control-flow-guard` enabled + --> $DIR/err-allow-partial-mitigations.rs:14:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `core` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` + +error: your program uses the crate `alloc`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations.rs:14:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `alloc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `alloc`, that is not compiled with `control-flow-guard` enabled + --> $DIR/err-allow-partial-mitigations.rs:14:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `alloc` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` + +error: your program uses the crate `compiler_builtins`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations.rs:14:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `compiler_builtins` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `compiler_builtins`, that is not compiled with `control-flow-guard` enabled + --> $DIR/err-allow-partial-mitigations.rs:14:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `compiler_builtins` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` + +error: your program uses the crate `libc`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations.rs:14:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `libc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `libc`, that is not compiled with `control-flow-guard` enabled + --> $DIR/err-allow-partial-mitigations.rs:14:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `libc` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` + +error: aborting due to 10 previous errors + diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.rs b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.rs index 0aa1c5933ea53..8ba8873053e05 100644 --- a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.rs +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.rs @@ -1,14 +1,15 @@ // ignore-tidy-linelength -//@ revisions: sp both disable enable-disable wrong-enable +//@ revisions: sp both disable enable-disable wrong-enable enable-separately-disable-together //@ check-fail //@ ignore-nvptx64 stack protector is not supported //@ ignore-wasm32-unknown-unknown stack protector is not supported //@ edition:future //@ [both] compile-flags: -Z unstable-options -C control-flow-guard=on -Z stack-protector=all //@ [sp] compile-flags: -Z unstable-options -Z stack-protector=all -//@ [disable] compile-flags: -Z unstable-options -Z allow-partial-mitigations=!stack-protector -Z stack-protector=all -//@ [enable-disable] compile-flags: -Z unstable-options -Z allow-partial-mitigations=stack-protector -Z allow-partial-mitigations=!stack-protector -Z stack-protector=all +//@ [disable] compile-flags: -Z unstable-options -Z deny-partial-mitigations=stack-protector -Z stack-protector=all +//@ [enable-disable] compile-flags: -Z unstable-options -Z allow-partial-mitigations=stack-protector -Z deny-partial-mitigations=stack-protector -Z stack-protector=all //@ [wrong-enable] compile-flags: -Z unstable-options -Z allow-partial-mitigations=control-flow-guard -Z stack-protector=all +//@ [enable-separately-disable-together] compile-flags: -Z unstable-options -Z allow-partial-mitigations=stack-protector -Z allow-partial-mitigations=control-flow-guard -Z deny-partial-mitigations=control-flow-guard,stack-protector -C control-flow-guard=on -Z stack-protector=all fn main() {} //[both]~^ ERROR that is not compiled with @@ -41,3 +42,13 @@ fn main() {} //[wrong-enable]~| ERROR that is not compiled with //[wrong-enable]~| ERROR that is not compiled with //[wrong-enable]~| ERROR that is not compiled with +//[enable-separately-disable-together]~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ERROR that is not compiled with +//[enable-separately-disable-together]~| ERROR that is not compiled with +//[enable-separately-disable-together]~| ERROR that is not compiled with +//[enable-separately-disable-together]~| ERROR that is not compiled with +//[enable-separately-disable-together]~| ERROR that is not compiled with +//[enable-separately-disable-together]~| ERROR that is not compiled with +//[enable-separately-disable-together]~| ERROR that is not compiled with +//[enable-separately-disable-together]~| ERROR that is not compiled with +//[enable-separately-disable-together]~| ERROR that is not compiled with +//[enable-separately-disable-together]~| ERROR that is not compiled with diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.sp.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.sp.stderr index f0e48e02347b3..b8ca779ed3b6d 100644 --- a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.sp.stderr +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.sp.stderr @@ -1,5 +1,5 @@ error: your program uses the crate `std`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:13:1 + --> $DIR/err-allow-partial-mitigations.rs:14:1 | LL | fn main() {} | ^ @@ -8,7 +8,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `core`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:13:1 + --> $DIR/err-allow-partial-mitigations.rs:14:1 | LL | fn main() {} | ^ @@ -17,7 +17,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `alloc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:13:1 + --> $DIR/err-allow-partial-mitigations.rs:14:1 | LL | fn main() {} | ^ @@ -26,7 +26,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `compiler_builtins`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:13:1 + --> $DIR/err-allow-partial-mitigations.rs:14:1 | LL | fn main() {} | ^ @@ -35,7 +35,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `libc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:13:1 + --> $DIR/err-allow-partial-mitigations.rs:14:1 | LL | fn main() {} | ^ diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.wrong-enable.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.wrong-enable.stderr index f0e48e02347b3..b8ca779ed3b6d 100644 --- a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.wrong-enable.stderr +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.wrong-enable.stderr @@ -1,5 +1,5 @@ error: your program uses the crate `std`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:13:1 + --> $DIR/err-allow-partial-mitigations.rs:14:1 | LL | fn main() {} | ^ @@ -8,7 +8,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `core`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:13:1 + --> $DIR/err-allow-partial-mitigations.rs:14:1 | LL | fn main() {} | ^ @@ -17,7 +17,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `alloc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:13:1 + --> $DIR/err-allow-partial-mitigations.rs:14:1 | LL | fn main() {} | ^ @@ -26,7 +26,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `compiler_builtins`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:13:1 + --> $DIR/err-allow-partial-mitigations.rs:14:1 | LL | fn main() {} | ^ @@ -35,7 +35,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `libc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:13:1 + --> $DIR/err-allow-partial-mitigations.rs:14:1 | LL | fn main() {} | ^ diff --git a/tests/ui/allow-partial-mitigations/ok-allow-partial-mitigations-minicore.rs b/tests/ui/allow-partial-mitigations/ok-allow-partial-mitigations-minicore.rs index 16eecd455dbe0..bc8aaa3cb2e61 100644 --- a/tests/ui/allow-partial-mitigations/ok-allow-partial-mitigations-minicore.rs +++ b/tests/ui/allow-partial-mitigations/ok-allow-partial-mitigations-minicore.rs @@ -4,7 +4,7 @@ //@ edition:future //@ revisions: default deny //@[default] compile-flags: -Z unstable-options -Z stack-protector=all -//@[deny] compile-flags: -Z allow-partial-mitigations=!stack-protector -Z unstable-options -Z stack-protector=all +//@[deny] compile-flags: -Z deny-partial-mitigations=stack-protector -Z unstable-options -Z stack-protector=all // ^ enables stack-protector for both minicore and this crate diff --git a/tests/ui/allow-partial-mitigations/ok-allow-partial-mitigations.rs b/tests/ui/allow-partial-mitigations/ok-allow-partial-mitigations.rs index c0ec555305841..c0bc09276568f 100644 --- a/tests/ui/allow-partial-mitigations/ok-allow-partial-mitigations.rs +++ b/tests/ui/allow-partial-mitigations/ok-allow-partial-mitigations.rs @@ -6,6 +6,6 @@ //@ ignore-wasm32-unknown-unknown stack protector is not supported //@ [both] compile-flags: -Z unstable-options -Z allow-partial-mitigations=stack-protector,control-flow-guard -C control-flow-guard=on -Z stack-protector=all //@ [sp] compile-flags: -Z unstable-options -Z allow-partial-mitigations=stack-protector -Z stack-protector=all -//@ [disable-enable] compile-flags: -Z unstable-options -Z allow-partial-mitigations=!stack-protector -Z allow-partial-mitigations=stack-protector -Z stack-protector=all +//@ [disable-enable] compile-flags: -Z unstable-options -Z deny-partial-mitigations=stack-protector -Z allow-partial-mitigations=stack-protector -Z stack-protector=all fn main() {} From c55bfc66a6bf47c06309bc971656c7d57c377a77 Mon Sep 17 00:00:00 2001 From: Ariel Ben-Yehuda Date: Wed, 10 Dec 2025 20:24:03 +0000 Subject: [PATCH 19/25] enforcable -> enforceable --- compiler/rustc_metadata/src/creader.rs | 2 +- compiler/rustc_metadata/src/rmeta/decoder.rs | 4 ++-- compiler/rustc_metadata/src/rmeta/encoder.rs | 2 +- compiler/rustc_metadata/src/rmeta/mod.rs | 2 +- compiler/rustc_metadata/src/rmeta/parameterized.rs | 2 +- compiler/rustc_session/src/options.rs | 10 +++++----- ...cable_mitigations.rs => enforceable_mitigations.rs} | 0 7 files changed, 11 insertions(+), 11 deletions(-) rename compiler/rustc_session/src/options/{enforcable_mitigations.rs => enforceable_mitigations.rs} (100%) diff --git a/compiler/rustc_metadata/src/creader.rs b/compiler/rustc_metadata/src/creader.rs index cee7a41a1182c..e155e7b0580df 100644 --- a/compiler/rustc_metadata/src/creader.rs +++ b/compiler/rustc_metadata/src/creader.rs @@ -25,7 +25,7 @@ use rustc_middle::ty::data_structures::IndexSet; use rustc_middle::ty::{TyCtxt, TyCtxtFeed}; use rustc_proc_macro::bridge::client::ProcMacro; use rustc_session::Session; -use rustc_session::config::enforcable_mitigations::DeniedPartialMitigationLevel; +use rustc_session::config::enforceable_mitigations::DeniedPartialMitigationLevel; use rustc_session::config::{ CrateType, ExtendedTargetModifierInfo, ExternLocation, Externs, OptionsTargetModifiers, TargetModifier, diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 00ca24c6b7501..06d764f772b23 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -30,7 +30,7 @@ use rustc_proc_macro::bridge::client::ProcMacro; use rustc_serialize::opaque::MemDecoder; use rustc_serialize::{Decodable, Decoder}; use rustc_session::config::TargetModifier; -use rustc_session::config::enforcable_mitigations::DeniedPartialMitigation; +use rustc_session::config::enforceable_mitigations::DeniedPartialMitigation; use rustc_session::cstore::{CrateSource, ExternCrate}; use rustc_span::hygiene::HygieneDecodeContext; use rustc_span::{ @@ -82,7 +82,7 @@ pub(crate) type CrateNumMap = IndexVec; /// Target modifiers - abi or exploit mitigations flags that cause unsoundness when mixed pub(crate) type TargetModifiers = Vec; -/// The set of enforcable mitigations (RFC 3855) that are currently enabled for this crate +/// The set of enforceable mitigations (RFC 3855) that are currently enabled for this crate pub(crate) type DeniedPartialMitigations = Vec; pub(crate) struct CrateMetadata { diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index ef5416ac427d5..933ba5b67cc11 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -27,7 +27,7 @@ use rustc_middle::ty::codec::TyEncoder; use rustc_middle::ty::fast_reject::{self, TreatParams}; use rustc_middle::{bug, span_bug}; use rustc_serialize::{Decodable, Decoder, Encodable, Encoder, opaque}; -use rustc_session::config::enforcable_mitigations::DeniedPartialMitigation; +use rustc_session::config::enforceable_mitigations::DeniedPartialMitigation; use rustc_session::config::{CrateType, OptLevel, TargetModifier}; use rustc_span::hygiene::HygieneEncodeContext; use rustc_span::{ diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index bcc0aa1f3be8c..c9bb988ba82d0 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -36,7 +36,7 @@ use rustc_middle::ty::fast_reject::SimplifiedType; use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_middle::util::Providers; use rustc_serialize::opaque::FileEncoder; -use rustc_session::config::enforcable_mitigations::DeniedPartialMitigation; +use rustc_session::config::enforceable_mitigations::DeniedPartialMitigation; use rustc_session::config::{SymbolManglingVersion, TargetModifier}; use rustc_session::cstore::{CrateDepKind, ForeignModule, LinkagePreference, NativeLib}; use rustc_span::edition::Edition; diff --git a/compiler/rustc_metadata/src/rmeta/parameterized.rs b/compiler/rustc_metadata/src/rmeta/parameterized.rs index 35ea06b384536..39686aeb22ffd 100644 --- a/compiler/rustc_metadata/src/rmeta/parameterized.rs +++ b/compiler/rustc_metadata/src/rmeta/parameterized.rs @@ -120,7 +120,7 @@ trivially_parameterized_over_tcx! { rustc_middle::ty::adjustment::CoerceUnsizedInfo, rustc_middle::ty::fast_reject::SimplifiedType, rustc_session::config::TargetModifier, - rustc_session::config::enforcable_mitigations::DeniedPartialMitigation, + rustc_session::config::enforceable_mitigations::DeniedPartialMitigation, rustc_session::cstore::ForeignModule, rustc_session::cstore::LinkagePreference, rustc_session::cstore::NativeLib, diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index ac1e400ce88e5..967ff79396eba 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -20,7 +20,7 @@ use rustc_target::spec::{ TargetTuple, TlsModel, }; -use crate::config::enforcable_mitigations::MitigationEnablement; +use crate::config::enforceable_mitigations::MitigationEnablement; use crate::config::*; use crate::search_paths::SearchPath; use crate::utils::NativeLib; @@ -85,7 +85,7 @@ pub struct TargetModifier { pub value_name: String, } -pub mod enforcable_mitigations; +pub mod enforceable_mitigations; mod target_modifier_consistency_check { use super::*; @@ -896,16 +896,16 @@ mod desc { pub(crate) const parse_align: &str = "a number that is a power of 2 between 1 and 2^29"; pub(crate) const parse_assert_incr_state: &str = "one of: `loaded`, `not-loaded`"; pub(crate) const parse_allow_partial_mitigations: &str = - super::enforcable_mitigations::DeniedPartialMitigationKind::KINDS; + super::enforceable_mitigations::DeniedPartialMitigationKind::KINDS; pub(crate) const parse_deny_partial_mitigations: &str = - super::enforcable_mitigations::DeniedPartialMitigationKind::KINDS; + super::enforceable_mitigations::DeniedPartialMitigationKind::KINDS; } pub mod parse { use std::str::FromStr; pub(crate) use super::*; - use crate::config::enforcable_mitigations::MitigationEnablement; + use crate::config::enforceable_mitigations::MitigationEnablement; pub(crate) const MAX_THREADS_CAP: usize = 256; /// Ignore the value. Used for removed options where we don't actually want to store diff --git a/compiler/rustc_session/src/options/enforcable_mitigations.rs b/compiler/rustc_session/src/options/enforceable_mitigations.rs similarity index 100% rename from compiler/rustc_session/src/options/enforcable_mitigations.rs rename to compiler/rustc_session/src/options/enforceable_mitigations.rs From b4bfd7fa43217be6fe8ec215928dc7a91782a4f3 Mon Sep 17 00:00:00 2001 From: Ariel Ben-Yehuda Date: Tue, 16 Dec 2025 23:55:35 +0000 Subject: [PATCH 20/25] address review comments --- compiler/rustc_metadata/src/creader.rs | 7 +++--- compiler/rustc_metadata/src/rmeta/decoder.rs | 2 +- compiler/rustc_metadata/src/rmeta/encoder.rs | 2 +- compiler/rustc_metadata/src/rmeta/mod.rs | 2 +- .../rustc_metadata/src/rmeta/parameterized.rs | 2 +- compiler/rustc_session/src/options.rs | 22 +++++++++---------- ..._mitigations.rs => mitigation_coverage.rs} | 12 +++++----- 7 files changed, 24 insertions(+), 25 deletions(-) rename compiler/rustc_session/src/options/{enforceable_mitigations.rs => mitigation_coverage.rs} (94%) diff --git a/compiler/rustc_metadata/src/creader.rs b/compiler/rustc_metadata/src/creader.rs index e155e7b0580df..7be7e3517fe8f 100644 --- a/compiler/rustc_metadata/src/creader.rs +++ b/compiler/rustc_metadata/src/creader.rs @@ -24,8 +24,7 @@ use rustc_middle::bug; use rustc_middle::ty::data_structures::IndexSet; use rustc_middle::ty::{TyCtxt, TyCtxtFeed}; use rustc_proc_macro::bridge::client::ProcMacro; -use rustc_session::Session; -use rustc_session::config::enforceable_mitigations::DeniedPartialMitigationLevel; +use rustc_session::config::mitigation_coverage::DeniedPartialMitigationLevel; use rustc_session::config::{ CrateType, ExtendedTargetModifierInfo, ExternLocation, Externs, OptionsTargetModifiers, TargetModifier, @@ -468,7 +467,7 @@ impl CStore { pub fn report_session_incompatibilities(&self, tcx: TyCtxt<'_>, krate: &Crate) { self.report_incompatible_target_modifiers(tcx, krate); - self.report_incompatible_denied_partial_mitigations(tcx, krate); + self.report_incompatible_partial_mitigations(tcx, krate); self.report_incompatible_async_drop_feature(tcx, krate); } @@ -493,7 +492,7 @@ impl CStore { } } - pub fn report_incompatible_denied_partial_mitigations(&self, tcx: TyCtxt<'_>, krate: &Crate) { + pub fn report_incompatible_partial_mitigations(&self, tcx: TyCtxt<'_>, krate: &Crate) { let my_mitigations = tcx.sess.gather_enabled_denied_partial_mitigations(); let mut my_mitigations: BTreeMap<_, _> = my_mitigations.iter().map(|mitigation| (mitigation.kind, mitigation)).collect(); diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 06d764f772b23..2993eb340cfe4 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -30,7 +30,7 @@ use rustc_proc_macro::bridge::client::ProcMacro; use rustc_serialize::opaque::MemDecoder; use rustc_serialize::{Decodable, Decoder}; use rustc_session::config::TargetModifier; -use rustc_session::config::enforceable_mitigations::DeniedPartialMitigation; +use rustc_session::config::mitigation_coverage::DeniedPartialMitigation; use rustc_session::cstore::{CrateSource, ExternCrate}; use rustc_span::hygiene::HygieneDecodeContext; use rustc_span::{ diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 933ba5b67cc11..2a0eeef442a33 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -27,7 +27,7 @@ use rustc_middle::ty::codec::TyEncoder; use rustc_middle::ty::fast_reject::{self, TreatParams}; use rustc_middle::{bug, span_bug}; use rustc_serialize::{Decodable, Decoder, Encodable, Encoder, opaque}; -use rustc_session::config::enforceable_mitigations::DeniedPartialMitigation; +use rustc_session::config::mitigation_coverage::DeniedPartialMitigation; use rustc_session::config::{CrateType, OptLevel, TargetModifier}; use rustc_span::hygiene::HygieneEncodeContext; use rustc_span::{ diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index c9bb988ba82d0..781d3c6d18372 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -36,7 +36,7 @@ use rustc_middle::ty::fast_reject::SimplifiedType; use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_middle::util::Providers; use rustc_serialize::opaque::FileEncoder; -use rustc_session::config::enforceable_mitigations::DeniedPartialMitigation; +use rustc_session::config::mitigation_coverage::DeniedPartialMitigation; use rustc_session::config::{SymbolManglingVersion, TargetModifier}; use rustc_session::cstore::{CrateDepKind, ForeignModule, LinkagePreference, NativeLib}; use rustc_span::edition::Edition; diff --git a/compiler/rustc_metadata/src/rmeta/parameterized.rs b/compiler/rustc_metadata/src/rmeta/parameterized.rs index 39686aeb22ffd..1531584e99788 100644 --- a/compiler/rustc_metadata/src/rmeta/parameterized.rs +++ b/compiler/rustc_metadata/src/rmeta/parameterized.rs @@ -120,7 +120,7 @@ trivially_parameterized_over_tcx! { rustc_middle::ty::adjustment::CoerceUnsizedInfo, rustc_middle::ty::fast_reject::SimplifiedType, rustc_session::config::TargetModifier, - rustc_session::config::enforceable_mitigations::DeniedPartialMitigation, + rustc_session::config::mitigation_coverage::DeniedPartialMitigation, rustc_session::cstore::ForeignModule, rustc_session::cstore::LinkagePreference, rustc_session::cstore::NativeLib, diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index 967ff79396eba..cdc9be1567ecd 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -20,7 +20,7 @@ use rustc_target::spec::{ TargetTuple, TlsModel, }; -use crate::config::enforceable_mitigations::MitigationEnablement; +use crate::config::mitigation_coverage::MitigationCoverage; use crate::config::*; use crate::search_paths::SearchPath; use crate::utils::NativeLib; @@ -85,7 +85,7 @@ pub struct TargetModifier { pub value_name: String, } -pub mod enforceable_mitigations; +pub mod mitigation_coverage; mod target_modifier_consistency_check { use super::*; @@ -896,16 +896,16 @@ mod desc { pub(crate) const parse_align: &str = "a number that is a power of 2 between 1 and 2^29"; pub(crate) const parse_assert_incr_state: &str = "one of: `loaded`, `not-loaded`"; pub(crate) const parse_allow_partial_mitigations: &str = - super::enforceable_mitigations::DeniedPartialMitigationKind::KINDS; + super::mitigation_coverage::DeniedPartialMitigationKind::KINDS; pub(crate) const parse_deny_partial_mitigations: &str = - super::enforceable_mitigations::DeniedPartialMitigationKind::KINDS; + super::mitigation_coverage::DeniedPartialMitigationKind::KINDS; } pub mod parse { use std::str::FromStr; pub(crate) use super::*; - use crate::config::enforceable_mitigations::MitigationEnablement; + use crate::config::mitigation_coverage::MitigationCoverage; pub(crate) const MAX_THREADS_CAP: usize = 256; /// Ignore the value. Used for removed options where we don't actually want to store @@ -2086,7 +2086,7 @@ pub mod parse { } fn parse_partial_mitigations( - slot: &mut Vec, + slot: &mut Vec, v: Option<&str>, enabled: bool, ) -> bool { @@ -2094,7 +2094,7 @@ pub mod parse { Some(s) => { for sub in s.split(',') { match sub.parse() { - Ok(kind) => slot.push(MitigationEnablement { kind, enabled }), + Ok(kind) => slot.push(MitigationCoverage { kind, enabled }), Err(_) => return false, } } @@ -2105,14 +2105,14 @@ pub mod parse { } pub(crate) fn parse_allow_partial_mitigations( - slot: &mut Vec, + slot: &mut Vec, v: Option<&str>, ) -> bool { parse_partial_mitigations(slot, v, true) } pub(crate) fn parse_deny_partial_mitigations( - slot: &mut Vec, + slot: &mut Vec, v: Option<&str>, ) -> bool { parse_partial_mitigations(slot, v, false) @@ -2281,7 +2281,7 @@ options! { // tidy-alphabetical-start allow_features: Option> = (None, parse_opt_comma_list, [TRACKED], "only allow the listed language features to be enabled in code (comma separated)"), - allow_partial_mitigations: Vec = (Vec::new(), parse_allow_partial_mitigations, [UNTRACKED], + allow_partial_mitigations: Vec = (Vec::new(), parse_allow_partial_mitigations, [UNTRACKED], "Allow mitigations not enabled for all dependency crates (comma separated list)"), always_encode_mir: bool = (false, parse_bool, [TRACKED], "encode MIR of all functions into the crate metadata (default: no)"), @@ -2350,7 +2350,7 @@ options! { "deduplicate identical diagnostics (default: yes)"), default_visibility: Option = (None, parse_opt_symbol_visibility, [TRACKED], "overrides the `default_visibility` setting of the target"), - deny_partial_mitigations: Vec = (Vec::new(), parse_deny_partial_mitigations, [UNTRACKED], + deny_partial_mitigations: Vec = (Vec::new(), parse_deny_partial_mitigations, [UNTRACKED], "Deny mitigations not enabled for all dependency crates (comma separated list)"), dep_info_omit_d_target: bool = (false, parse_bool, [TRACKED], "in dep-info output, omit targets for tracking dependencies of the dep-info files \ diff --git a/compiler/rustc_session/src/options/enforceable_mitigations.rs b/compiler/rustc_session/src/options/mitigation_coverage.rs similarity index 94% rename from compiler/rustc_session/src/options/enforceable_mitigations.rs rename to compiler/rustc_session/src/options/mitigation_coverage.rs index 851659f416e85..d851fac1ea1e8 100644 --- a/compiler/rustc_session/src/options/enforceable_mitigations.rs +++ b/compiler/rustc_session/src/options/mitigation_coverage.rs @@ -68,7 +68,7 @@ impl From for DeniedPartialMitigationLevel { pub struct DeniedPartialMitigationKindParseError; #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Encodable, BlobDecodable)] -pub struct MitigationEnablement { +pub struct MitigationCoverage { pub kind: DeniedPartialMitigationKind, pub enabled: bool, } @@ -79,7 +79,7 @@ macro_rules! intersperse { }; } -macro_rules! enforced_mitigations { +macro_rules! denied_partial_mitigations { ([$self:ident] enum $kind:ident {$(($name:ident, $text:expr, $since:ident, $code:expr)),*}) => { #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Encodable, BlobDecodable)] pub enum DeniedPartialMitigationKind { @@ -123,7 +123,7 @@ macro_rules! enforced_mitigations { } impl Options { - pub fn all_enforced_mitigations(&self) -> impl Iterator { + pub fn all_denied_partial_mitigations(&self) -> impl Iterator { [$(DeniedPartialMitigationKind::$name),*].into_iter() } } @@ -145,7 +145,7 @@ macro_rules! enforced_mitigations { } } -enforced_mitigations! { +denied_partial_mitigations! { [self] enum DeniedPartialMitigationKind { (StackProtector, "stack-protector", EditionFuture, self.stack_protector()), @@ -153,7 +153,7 @@ enforced_mitigations! { } } -/// Enforced mitigations, see [RFC 3855](https://github.com/rust-lang/rfcs/pull/3855) +/// Denied-partial mitigations, see [RFC 3855](https://github.com/rust-lang/rfcs/pull/3855) #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Encodable, BlobDecodable)] pub struct DeniedPartialMitigation { pub kind: DeniedPartialMitigationKind, @@ -167,7 +167,7 @@ impl Options { edition: Edition, ) -> impl Iterator { let mut result: BTreeSet<_> = self - .all_enforced_mitigations() + .all_denied_partial_mitigations() .filter(|mitigation| mitigation.enforced_since() > edition) .collect(); for mitigation in &self.unstable_opts.allow_partial_mitigations { From a0ff19d8ec8be0b8c15416c7038c4d589c5adc34 Mon Sep 17 00:00:00 2001 From: Ariel Ben-Yehuda Date: Fri, 20 Feb 2026 01:38:38 +0200 Subject: [PATCH 21/25] reset mitigation status on a mitigation option as per the RFC --- compiler/rustc_session/src/config.rs | 12 +- compiler/rustc_session/src/options.rs | 147 ++++++++++-------- .../src/options/mitigation_coverage.rs | 94 +++++++++-- src/librustdoc/config.rs | 8 +- ...mitigations-1-error.cfg-allow-first.stderr | 47 ++++++ ...tions-1-error.disable-enable-reset.stderr} | 10 +- ...artial-mitigations-1-error.disable.stderr} | 10 +- ...mitigations-1-error.enable-disable.stderr} | 10 +- .../err-allow-partial-mitigations-1-error.rs | 20 +++ ...mitigations-1-error.sp-allow-first.stderr} | 10 +- ...llow-partial-mitigations-1-error.sp.stderr | 47 ++++++ ...al-mitigations-1-error.wrong-enable.stderr | 47 ++++++ ...-partial-mitigations-2-errors.both.stderr} | 20 +-- ...enable-separately-disable-together.stderr} | 20 +-- .../err-allow-partial-mitigations-2-errors.rs | 20 +++ .../err-allow-partial-mitigations-bad-cli.rs | 12 ++ ...r-allow-partial-mitigations-bad-cli.stderr | 2 + ...low-partial-mitigations-current-edition.rs | 2 +- .../err-allow-partial-mitigations.rs | 54 ------- ...low-partial-mitigations-current-edition.rs | 13 ++ .../ok-allow-partial-mitigations.rs | 6 +- 21 files changed, 423 insertions(+), 188 deletions(-) create mode 100644 tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.cfg-allow-first.stderr rename tests/ui/allow-partial-mitigations/{err-allow-partial-mitigations.sp.stderr => err-allow-partial-mitigations-1-error.disable-enable-reset.stderr} (89%) rename tests/ui/allow-partial-mitigations/{err-allow-partial-mitigations.disable.stderr => err-allow-partial-mitigations-1-error.disable.stderr} (89%) rename tests/ui/allow-partial-mitigations/{err-allow-partial-mitigations.enable-disable.stderr => err-allow-partial-mitigations-1-error.enable-disable.stderr} (89%) create mode 100644 tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.rs rename tests/ui/allow-partial-mitigations/{err-allow-partial-mitigations.wrong-enable.stderr => err-allow-partial-mitigations-1-error.sp-allow-first.stderr} (89%) create mode 100644 tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.sp.stderr create mode 100644 tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.wrong-enable.stderr rename tests/ui/allow-partial-mitigations/{err-allow-partial-mitigations.both.stderr => err-allow-partial-mitigations-2-errors.both.stderr} (89%) rename tests/ui/allow-partial-mitigations/{err-allow-partial-mitigations.enable-separately-disable-together.stderr => err-allow-partial-mitigations-2-errors.enable-separately-disable-together.stderr} (89%) create mode 100644 tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-2-errors.rs create mode 100644 tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-bad-cli.rs create mode 100644 tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-bad-cli.stderr delete mode 100644 tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.rs create mode 100644 tests/ui/allow-partial-mitigations/ok-allow-partial-mitigations-current-edition.rs diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index e4ef1d40d72d5..7f0391384d3a5 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -1449,6 +1449,7 @@ impl Default for Options { logical_env: FxIndexMap::default(), verbose: false, target_modifiers: BTreeMap::default(), + mitigation_coverage_map: Default::default(), } } } @@ -2456,9 +2457,9 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M let crate_types = parse_crate_types_from_list(unparsed_crate_types) .unwrap_or_else(|e| early_dcx.early_fatal(e)); - let mut target_modifiers = BTreeMap::::new(); + let mut collected_options = Default::default(); - let mut unstable_opts = UnstableOptions::build(early_dcx, matches, &mut target_modifiers); + let mut unstable_opts = UnstableOptions::build(early_dcx, matches, &mut collected_options); let (lint_opts, describe_lints, lint_cap) = get_cmd_lint_options(early_dcx, matches); if !unstable_opts.unstable_options && json_timings { @@ -2474,7 +2475,7 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M let output_types = parse_output_types(early_dcx, &unstable_opts, matches); - let mut cg = CodegenOptions::build(early_dcx, matches, &mut target_modifiers); + let mut cg = CodegenOptions::build(early_dcx, matches, &mut collected_options); let (disable_local_thinlto, codegen_units) = should_override_cgus_and_disable_thinlto( early_dcx, &output_types, @@ -2625,7 +2626,7 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M // -Zretpoline-external-thunk also requires -Zretpoline if unstable_opts.retpoline_external_thunk { unstable_opts.retpoline = true; - target_modifiers.insert( + collected_options.target_modifiers.insert( OptionsTargetModifiers::UnstableOptions(UnstableOptionsTargetModifiers::retpoline), "true".to_string(), ); @@ -2785,7 +2786,8 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M color, logical_env, verbose, - target_modifiers, + target_modifiers: collected_options.target_modifiers, + mitigation_coverage_map: collected_options.mitigations, } } diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index cdc9be1567ecd..9fc6036b98b34 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -20,7 +20,6 @@ use rustc_target::spec::{ TargetTuple, TlsModel, }; -use crate::config::mitigation_coverage::MitigationCoverage; use crate::config::*; use crate::search_paths::SearchPath; use crate::utils::NativeLib; @@ -201,15 +200,15 @@ macro_rules! gather_tmods { tmod_push!($struct_name, $tmod_enum_name, $opt_name, $opt_expr, $init, $mods, $tmod_vals) }; ($struct_name:ident, $tmod_enum_name:ident, $opt_name:ident, $opt_expr:expr, $init:expr, $mods:expr, $tmod_vals:expr, - [SUBSTRUCT], []) => { + [SUBSTRUCT], [$(MITIGATION)?]) => { $opt_expr.gather_target_modifiers($mods, $tmod_vals); }; ($struct_name:ident, $tmod_enum_name:ident, $opt_name:ident, $opt_expr:expr, $init:expr, $mods:expr, $tmod_vals:expr, - [UNTRACKED], []) => {{}}; + [UNTRACKED], [$(MITIGATION)?]) => {{}}; ($struct_name:ident, $tmod_enum_name:ident, $opt_name:ident, $opt_expr:expr, $init:expr, $mods:expr, $tmod_vals:expr, - [TRACKED], []) => {{}}; + [TRACKED], [$(MITIGATION)?]) => {{}}; ($struct_name:ident, $tmod_enum_name:ident, $opt_name:ident, $opt_expr:expr, $init:expr, $mods:expr, $tmod_vals:expr, - [TRACKED_NO_CRATE_HASH], []) => {{}}; + [TRACKED_NO_CRATE_HASH], [$(MITIGATION)?]) => {{}}; } macro_rules! gather_tmods_top_level { @@ -219,7 +218,7 @@ macro_rules! gather_tmods_top_level { ($opt_name:ident, $opt_expr:expr, $mods:expr, $tmod_vals:expr, [$non_substruct:ident TARGET_MODIFIER]) => { compile_error!("Top level option can't be target modifier"); }; - ($opt_name:ident, $opt_expr:expr, $mods:expr, $tmod_vals:expr, [$non_substruct:ident]) => {}; + ($opt_name:ident, $opt_expr:expr, $mods:expr, $tmod_vals:expr, [$non_substruct:ident $(MITIGATION)?]) => {}; } /// Macro for generating OptionsTargetsModifiers top-level enum with impl. @@ -324,6 +323,7 @@ macro_rules! top_level_options { pub $opt: $t ),*, pub target_modifiers: BTreeMap, + pub mitigation_coverage_map: mitigation_coverage::MitigationCoverageMap, } impl Options { @@ -506,11 +506,20 @@ top_level_options!( } ); +macro_rules! mitigation_enum_opt { + ($opt:ident, MITIGATION) => { + Some(mitigation_coverage::DeniedPartialMitigationKind::$opt) + }; + ($opt:ident, $(TARGET_MODIFIER)?) => { + None + }; +} + macro_rules! tmod_enum_opt { - ($struct_name:ident, $tmod_enum_name:ident, $opt:ident, $v:ident) => { + ($struct_name:ident, $tmod_enum_name:ident, $opt:ident, TARGET_MODIFIER) => { Some(OptionsTargetModifiers::$struct_name($tmod_enum_name::$opt)) }; - ($struct_name:ident, $tmod_enum_name:ident, $opt:ident, ) => { + ($struct_name:ident, $tmod_enum_name:ident, $opt:ident, $(MITIGATION)?) => { None }; } @@ -582,7 +591,7 @@ macro_rules! tmod_enum { ( $tmod_enum_name:ident, $prefix:expr, @parse {$($eout:tt)*}, ($puser_value:ident){$($pout:tt)*}; - $opt:ident, $parse:ident, $t:ty, [] | + $opt:ident, $parse:ident, $t:ty, [$(MITIGATION)?] | $($tail:tt)* ) => { tmod_enum! { @@ -599,6 +608,47 @@ macro_rules! tmod_enum { }; } +#[derive(Default)] +pub struct CollectedOptions { + pub target_modifiers: BTreeMap, + pub mitigations: mitigation_coverage::MitigationCoverageMap, +} + +macro_rules! setter_for { + // the allow/deny-mitigations options use collected/index instead of the cg, since they + // work across option groups + (allow_partial_mitigations, $struct_name:ident, $parse:ident) => { + pub(super) fn allow_partial_mitigations( + _cg: &mut super::$struct_name, + collected: &mut super::CollectedOptions, + v: Option<&str>, + index: usize, + ) -> bool { + collected.mitigations.handle_allowdeny_mitigation_option(v, index, true) + } + }; + (deny_partial_mitigations, $struct_name:ident, $parse:ident) => { + pub(super) fn deny_partial_mitigations( + _cg: &mut super::$struct_name, + collected: &mut super::CollectedOptions, + v: Option<&str>, + index: usize, + ) -> bool { + collected.mitigations.handle_allowdeny_mitigation_option(v, index, false) + } + }; + ($opt:ident, $struct_name:ident, $parse:ident) => { + pub(super) fn $opt( + cg: &mut super::$struct_name, + _collected: &mut super::CollectedOptions, + v: Option<&str>, + _index: usize, + ) -> bool { + super::parse::$parse(&mut redirect_field!(cg.$opt), v) + } + }; +} + /// Defines all `CodegenOptions`/`DebuggingOptions` fields and parsers all at once. The goal of this /// macro is to define an interface that can be programmatically used by the option parser /// to initialize the struct without hardcoding field names all over the place. @@ -612,7 +662,7 @@ macro_rules! options { $($( #[$attr:meta] )* $opt:ident : $t:ty = ( $init:expr, $parse:ident, - [$dep_tracking_marker:ident $( $tmod:ident )?], + [$dep_tracking_marker:ident $( $modifier_kind:ident )?], $desc:expr $(, removed: $removed:ident )?) ),* ,) => @@ -621,7 +671,7 @@ macro_rules! options { #[rustc_lint_opt_ty] pub struct $struct_name { $( $( #[$attr] )* pub $opt: $t),* } - tmod_enum!( $tmod_enum_name, $prefix, {$($opt, $parse, $t, [$($tmod),*])|*} ); + tmod_enum!( $tmod_enum_name, $prefix, {$($opt, $parse, $t, [$($modifier_kind),*])|*} ); impl Default for $struct_name { fn default() -> $struct_name { @@ -633,7 +683,7 @@ macro_rules! options { pub fn build( early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches, - target_modifiers: &mut BTreeMap, + target_modifiers: &mut CollectedOptions, ) -> $struct_name { build_options(early_dcx, matches, target_modifiers, $stat, $prefix, $outputname) } @@ -663,7 +713,7 @@ macro_rules! options { ) { $({ gather_tmods!($struct_name, $tmod_enum_name, $opt, &self.$opt, $init, _mods, _tmod_vals, - [$dep_tracking_marker], [$($tmod),*]); + [$dep_tracking_marker], [$($modifier_kind),*]); })* } } @@ -671,13 +721,13 @@ macro_rules! options { pub const $stat: OptionDescrs<$struct_name> = &[ $( OptionDesc{ name: stringify!($opt), setter: $optmod::$opt, type_desc: desc::$parse, desc: $desc, removed: None $( .or(Some(RemovedOption::$removed)) )?, - tmod: tmod_enum_opt!($struct_name, $tmod_enum_name, $opt, $($tmod),*) } ),* ]; + tmod: tmod_enum_opt!($struct_name, $tmod_enum_name, $opt, $($modifier_kind),*), + mitigation: mitigation_enum_opt!($opt, $($modifier_kind),*), + } ),* ]; mod $optmod { $( - pub(super) fn $opt(cg: &mut super::$struct_name, v: Option<&str>) -> bool { - super::parse::$parse(&mut redirect_field!(cg.$opt), v) - } + setter_for!($opt, $struct_name, $parse); )* } @@ -694,9 +744,6 @@ impl CodegenOptions { // Sometimes different options need to build a common structure. // That structure can be kept in one of the options' fields, the others become dummy. macro_rules! redirect_field { - ($cg:ident.deny_partial_mitigations) => { - $cg.allow_partial_mitigations - }; ($cg:ident.link_arg) => { $cg.link_args }; @@ -708,7 +755,7 @@ macro_rules! redirect_field { }; } -type OptionSetter = fn(&mut O, v: Option<&str>) -> bool; +type OptionSetter = fn(&mut O, &mut CollectedOptions, v: Option<&str>, pos: usize) -> bool; type OptionDescrs = &'static [OptionDesc]; /// Indicates whether a removed option should warn or error. @@ -726,6 +773,7 @@ pub struct OptionDesc { desc: &'static str, removed: Option, tmod: Option, + mitigation: Option, } impl OptionDesc { @@ -741,13 +789,13 @@ impl OptionDesc { fn build_options( early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches, - target_modifiers: &mut BTreeMap, + collected_options: &mut CollectedOptions, descrs: OptionDescrs, prefix: &str, outputname: &str, ) -> O { let mut op = O::default(); - for option in matches.opt_strs(prefix) { + for (index, option) in matches.opt_strs_pos(prefix) { let (key, value) = match option.split_once('=') { None => (option, None), Some((k, v)) => (k.to_string(), Some(v)), @@ -755,7 +803,7 @@ fn build_options( let option_to_lookup = key.replace('-', "_"); match descrs.iter().find(|opt_desc| opt_desc.name == option_to_lookup) { - Some(OptionDesc { name: _, setter, type_desc, desc, removed, tmod }) => { + Some(OptionDesc { name: _, setter, type_desc, desc, removed, tmod, mitigation }) => { if let Some(removed) = removed { // deprecation works for prefixed options only assert!(!prefix.is_empty()); @@ -768,7 +816,7 @@ fn build_options( } } } - if !setter(&mut op, value) { + if !setter(&mut op, collected_options, value, index) { match value { None => early_dcx.early_fatal( format!( @@ -784,7 +832,10 @@ fn build_options( } if let Some(tmod) = *tmod { let v = value.map_or(String::new(), ToOwned::to_owned); - target_modifiers.insert(tmod, v); + collected_options.target_modifiers.insert(tmod, v); + } + if let Some(mitigation) = mitigation { + collected_options.mitigations.reset_mitigation(*mitigation, index); } } None => early_dcx.early_fatal(format!("unknown {outputname} option: `{key}`")), @@ -905,7 +956,6 @@ pub mod parse { use std::str::FromStr; pub(crate) use super::*; - use crate::config::mitigation_coverage::MitigationCoverage; pub(crate) const MAX_THREADS_CAP: usize = 256; /// Ignore the value. Used for removed options where we don't actually want to store @@ -2084,39 +2134,6 @@ pub mod parse { }; true } - - fn parse_partial_mitigations( - slot: &mut Vec, - v: Option<&str>, - enabled: bool, - ) -> bool { - match v { - Some(s) => { - for sub in s.split(',') { - match sub.parse() { - Ok(kind) => slot.push(MitigationCoverage { kind, enabled }), - Err(_) => return false, - } - } - true - } - None => false, - } - } - - pub(crate) fn parse_allow_partial_mitigations( - slot: &mut Vec, - v: Option<&str>, - ) -> bool { - parse_partial_mitigations(slot, v, true) - } - - pub(crate) fn parse_deny_partial_mitigations( - slot: &mut Vec, - v: Option<&str>, - ) -> bool { - parse_partial_mitigations(slot, v, false) - } } options! { @@ -2139,7 +2156,7 @@ options! { collapse_macro_debuginfo: CollapseMacroDebuginfo = (CollapseMacroDebuginfo::Unspecified, parse_collapse_macro_debuginfo, [TRACKED], "set option to collapse debuginfo for macros"), - control_flow_guard: CFGuard = (CFGuard::Disabled, parse_cfguard, [TRACKED], + control_flow_guard: CFGuard = (CFGuard::Disabled, parse_cfguard, [TRACKED MITIGATION], "use Windows Control Flow Guard (default: no)"), debug_assertions: Option = (None, parse_opt_bool, [TRACKED], "explicitly enable the `cfg(debug_assertions)` directive"), @@ -2281,7 +2298,9 @@ options! { // tidy-alphabetical-start allow_features: Option> = (None, parse_opt_comma_list, [TRACKED], "only allow the listed language features to be enabled in code (comma separated)"), - allow_partial_mitigations: Vec = (Vec::new(), parse_allow_partial_mitigations, [UNTRACKED], + // the real parser is at the `setter_for` macro, to allow `-Z` and `-C` options to + // work together. + allow_partial_mitigations: () = ((), parse_allow_partial_mitigations, [UNTRACKED], "Allow mitigations not enabled for all dependency crates (comma separated list)"), always_encode_mir: bool = (false, parse_bool, [TRACKED], "encode MIR of all functions into the crate metadata (default: no)"), @@ -2350,7 +2369,7 @@ options! { "deduplicate identical diagnostics (default: yes)"), default_visibility: Option = (None, parse_opt_symbol_visibility, [TRACKED], "overrides the `default_visibility` setting of the target"), - deny_partial_mitigations: Vec = (Vec::new(), parse_deny_partial_mitigations, [UNTRACKED], + deny_partial_mitigations: () = ((), parse_deny_partial_mitigations, [UNTRACKED], "Deny mitigations not enabled for all dependency crates (comma separated list)"), dep_info_omit_d_target: bool = (false, parse_bool, [TRACKED], "in dep-info output, omit targets for tracking dependencies of the dep-info files \ @@ -2741,7 +2760,7 @@ written to standard error output)"), src_hash_algorithm: Option = (None, parse_src_file_hash, [TRACKED], "hash algorithm of source files in debug info (`md5`, `sha1`, or `sha256`)"), #[rustc_lint_opt_deny_field_access("use `Session::stack_protector` instead of this field")] - stack_protector: StackProtector = (StackProtector::None, parse_stack_protector, [TRACKED], + stack_protector: StackProtector = (StackProtector::None, parse_stack_protector, [TRACKED MITIGATION], "control stack smash protection strategy (`rustc --print stack-protector-strategies` for details)"), staticlib_allow_rdylib_deps: bool = (false, parse_bool, [TRACKED], "allow staticlibs to have rust dylib dependencies"), diff --git a/compiler/rustc_session/src/options/mitigation_coverage.rs b/compiler/rustc_session/src/options/mitigation_coverage.rs index d851fac1ea1e8..cf839706fe787 100644 --- a/compiler/rustc_session/src/options/mitigation_coverage.rs +++ b/compiler/rustc_session/src/options/mitigation_coverage.rs @@ -1,4 +1,4 @@ -use std::collections::BTreeSet; +use std::collections::{BTreeMap, BTreeSet}; use std::str::FromStr; use rustc_macros::{BlobDecodable, Encodable}; @@ -65,14 +65,66 @@ impl From for DeniedPartialMitigationLevel { } } -pub struct DeniedPartialMitigationKindParseError; +#[derive(Copy, Clone)] +struct MitigationStatus { + // This is the index of the option in the command line. This is needed because + // re-enabling a mitigation resets the partial mitigation status if it's later in the command + // line, and this works across `-C` and `-Z` args. + // + // e.g. `-Z stack-protector=strong` resets `-C allow-partial-mitigations=stack-protector`. + index: usize, + allowed: Option, +} -#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Encodable, BlobDecodable)] -pub struct MitigationCoverage { - pub kind: DeniedPartialMitigationKind, - pub enabled: bool, +#[derive(Clone, Default)] +pub struct MitigationCoverageMap { + map: BTreeMap, } +impl MitigationCoverageMap { + fn apply_mitigation( + &mut self, + kind: DeniedPartialMitigationKind, + index: usize, + allowed: Option, + ) { + self.map + .entry(kind) + .and_modify(|e| { + if index >= e.index { + *e = MitigationStatus { index, allowed } + } + }) + .or_insert(MitigationStatus { index, allowed }); + } + + pub(crate) fn handle_allowdeny_mitigation_option( + &mut self, + v: Option<&str>, + index: usize, + allowed: bool, + ) -> bool { + match v { + Some(s) => { + for sub in s.split(',') { + match sub.parse() { + Ok(kind) => self.apply_mitigation(kind, index, Some(allowed)), + Err(_) => return false, + } + } + true + } + None => false, + } + } + + pub(crate) fn reset_mitigation(&mut self, kind: DeniedPartialMitigationKind, index: usize) { + self.apply_mitigation(kind, index, None); + } +} + +pub struct DeniedPartialMitigationKindParseError; + macro_rules! intersperse { ($sep:expr, ($first:expr $(, $rest:expr)* $(,)?)) => { concat!($first $(, $sep, $rest)*) @@ -81,6 +133,7 @@ macro_rules! intersperse { macro_rules! denied_partial_mitigations { ([$self:ident] enum $kind:ident {$(($name:ident, $text:expr, $since:ident, $code:expr)),*}) => { + #[allow(non_camel_case_types)] #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Encodable, BlobDecodable)] pub enum DeniedPartialMitigationKind { $($name),* @@ -113,12 +166,13 @@ macro_rules! denied_partial_mitigations { #[allow(unused)] impl DeniedPartialMitigationKind { - pub fn enforced_since(&self) -> Edition { - match self { + pub fn allowed_by_default_at(&self, edition: Edition) -> bool { + let enforced_since = match self { // Should change the enforced-since edition of StackProtector to 2015 // (all editions) when `-C stack-protector` is stabilized. $(DeniedPartialMitigationKind::$name => Edition::$since),* - } + }; + edition < enforced_since } } @@ -148,8 +202,10 @@ macro_rules! denied_partial_mitigations { denied_partial_mitigations! { [self] enum DeniedPartialMitigationKind { - (StackProtector, "stack-protector", EditionFuture, self.stack_protector()), - (ControlFlowGuard, "control-flow-guard", EditionFuture, self.opts.cg.control_flow_guard == CFGuard::Checks) + // The mitigation name should match the option name in rustc_session::options, + // to allow for resetting the mitigation + (stack_protector, "stack-protector", EditionFuture, self.stack_protector()), + (control_flow_guard, "control-flow-guard", EditionFuture, self.opts.cg.control_flow_guard == CFGuard::Checks) } } @@ -168,13 +224,17 @@ impl Options { ) -> impl Iterator { let mut result: BTreeSet<_> = self .all_denied_partial_mitigations() - .filter(|mitigation| mitigation.enforced_since() > edition) + .filter(|mitigation| mitigation.allowed_by_default_at(edition)) .collect(); - for mitigation in &self.unstable_opts.allow_partial_mitigations { - if mitigation.enabled { - result.insert(mitigation.kind); - } else { - result.remove(&mitigation.kind); + for (kind, MitigationStatus { index: _, allowed }) in &self.mitigation_coverage_map.map { + match allowed { + Some(true) => { + result.insert(*kind); + } + Some(false) => { + result.remove(kind); + } + None => {} } } result.into_iter() diff --git a/src/librustdoc/config.rs b/src/librustdoc/config.rs index 3caff6edd504a..4a2b3847d839c 100644 --- a/src/librustdoc/config.rs +++ b/src/librustdoc/config.rs @@ -413,9 +413,9 @@ impl Options { config::parse_error_format(early_dcx, matches, color, json_color, json_rendered); let diagnostic_width = matches.opt_get("diagnostic-width").unwrap_or_default(); - let mut target_modifiers = BTreeMap::::new(); - let codegen_options = CodegenOptions::build(early_dcx, matches, &mut target_modifiers); - let unstable_opts = UnstableOptions::build(early_dcx, matches, &mut target_modifiers); + let mut collected_options = Default::default(); + let codegen_options = CodegenOptions::build(early_dcx, matches, &mut collected_options); + let unstable_opts = UnstableOptions::build(early_dcx, matches, &mut collected_options); let remap_path_prefix = match parse_remap_path_prefix(matches) { Ok(prefix_mappings) => prefix_mappings, @@ -894,7 +894,7 @@ impl Options { scrape_examples_options, unstable_features, doctest_build_args, - target_modifiers, + target_modifiers: collected_options.target_modifiers, }; let render_options = RenderOptions { output, diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.cfg-allow-first.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.cfg-allow-first.stderr new file mode 100644 index 0000000000000..24bcf83a9be6f --- /dev/null +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.cfg-allow-first.stderr @@ -0,0 +1,47 @@ +error: your program uses the crate `std`, that is not compiled with `control-flow-guard` enabled + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `std` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` + +error: your program uses the crate `core`, that is not compiled with `control-flow-guard` enabled + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `core` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` + +error: your program uses the crate `alloc`, that is not compiled with `control-flow-guard` enabled + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `alloc` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` + +error: your program uses the crate `compiler_builtins`, that is not compiled with `control-flow-guard` enabled + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `compiler_builtins` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` + +error: your program uses the crate `libc`, that is not compiled with `control-flow-guard` enabled + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `libc` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` + +error: aborting due to 5 previous errors + diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.sp.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.disable-enable-reset.stderr similarity index 89% rename from tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.sp.stderr rename to tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.disable-enable-reset.stderr index b8ca779ed3b6d..f039dc7acbd09 100644 --- a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.sp.stderr +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.disable-enable-reset.stderr @@ -1,5 +1,5 @@ error: your program uses the crate `std`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 | LL | fn main() {} | ^ @@ -8,7 +8,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `core`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 | LL | fn main() {} | ^ @@ -17,7 +17,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `alloc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 | LL | fn main() {} | ^ @@ -26,7 +26,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `compiler_builtins`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 | LL | fn main() {} | ^ @@ -35,7 +35,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `libc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 | LL | fn main() {} | ^ diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.disable.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.disable.stderr similarity index 89% rename from tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.disable.stderr rename to tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.disable.stderr index b8ca779ed3b6d..f039dc7acbd09 100644 --- a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.disable.stderr +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.disable.stderr @@ -1,5 +1,5 @@ error: your program uses the crate `std`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 | LL | fn main() {} | ^ @@ -8,7 +8,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `core`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 | LL | fn main() {} | ^ @@ -17,7 +17,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `alloc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 | LL | fn main() {} | ^ @@ -26,7 +26,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `compiler_builtins`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 | LL | fn main() {} | ^ @@ -35,7 +35,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `libc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 | LL | fn main() {} | ^ diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.enable-disable.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.enable-disable.stderr similarity index 89% rename from tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.enable-disable.stderr rename to tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.enable-disable.stderr index b8ca779ed3b6d..f039dc7acbd09 100644 --- a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.enable-disable.stderr +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.enable-disable.stderr @@ -1,5 +1,5 @@ error: your program uses the crate `std`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 | LL | fn main() {} | ^ @@ -8,7 +8,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `core`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 | LL | fn main() {} | ^ @@ -17,7 +17,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `alloc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 | LL | fn main() {} | ^ @@ -26,7 +26,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `compiler_builtins`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 | LL | fn main() {} | ^ @@ -35,7 +35,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `libc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 | LL | fn main() {} | ^ diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.rs b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.rs new file mode 100644 index 0000000000000..4b419e8212c10 --- /dev/null +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.rs @@ -0,0 +1,20 @@ +// ignore-tidy-linelength +//@ revisions: sp disable enable-disable wrong-enable disable-enable-reset cfg-allow-first sp-allow-first +//@ check-fail +//@ ignore-nvptx64 stack protector is not supported +//@ ignore-wasm32-unknown-unknown stack protector is not supported +//@ edition:future +//@ [sp] compile-flags: -Z unstable-options -Z stack-protector=all +//@ [disable] compile-flags: -Z unstable-options -Z deny-partial-mitigations=stack-protector -Z stack-protector=all +//@ [enable-disable] compile-flags: -Z unstable-options -Z allow-partial-mitigations=stack-protector -Z deny-partial-mitigations=stack-protector -Z stack-protector=all +//@ [wrong-enable] compile-flags: -Z unstable-options -Z allow-partial-mitigations=control-flow-guard -Z stack-protector=all +//@ [cfg-allow-first] compile-flags: -Z unstable-options -Z allow-partial-mitigations=stack-protector -C control-flow-guard=on +//@ [sp-allow-first] compile-flags: -Z unstable-options -Z allow-partial-mitigations=stack-protector -Z stack-protector=all +//@ [disable-enable-reset] compile-flags: -Z unstable-options -Z deny-partial-mitigations=stack-protector -Z allow-partial-mitigations=stack-protector -Z stack-protector=all + +fn main() {} +//~^ ERROR that is not compiled with +//~| ERROR that is not compiled with +//~| ERROR that is not compiled with +//~| ERROR that is not compiled with +//~| ERROR that is not compiled with diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.wrong-enable.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.sp-allow-first.stderr similarity index 89% rename from tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.wrong-enable.stderr rename to tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.sp-allow-first.stderr index b8ca779ed3b6d..f039dc7acbd09 100644 --- a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.wrong-enable.stderr +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.sp-allow-first.stderr @@ -1,5 +1,5 @@ error: your program uses the crate `std`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 | LL | fn main() {} | ^ @@ -8,7 +8,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `core`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 | LL | fn main() {} | ^ @@ -17,7 +17,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `alloc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 | LL | fn main() {} | ^ @@ -26,7 +26,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `compiler_builtins`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 | LL | fn main() {} | ^ @@ -35,7 +35,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `libc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 | LL | fn main() {} | ^ diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.sp.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.sp.stderr new file mode 100644 index 0000000000000..f039dc7acbd09 --- /dev/null +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.sp.stderr @@ -0,0 +1,47 @@ +error: your program uses the crate `std`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `std` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `core`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `core` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `alloc`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `alloc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `compiler_builtins`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `compiler_builtins` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `libc`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `libc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: aborting due to 5 previous errors + diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.wrong-enable.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.wrong-enable.stderr new file mode 100644 index 0000000000000..f039dc7acbd09 --- /dev/null +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.wrong-enable.stderr @@ -0,0 +1,47 @@ +error: your program uses the crate `std`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `std` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `core`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `core` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `alloc`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `alloc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `compiler_builtins`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `compiler_builtins` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `libc`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `libc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: aborting due to 5 previous errors + diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.both.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-2-errors.both.stderr similarity index 89% rename from tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.both.stderr rename to tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-2-errors.both.stderr index 50febbee060f2..cbbe300365303 100644 --- a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.both.stderr +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-2-errors.both.stderr @@ -1,5 +1,5 @@ error: your program uses the crate `std`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 | LL | fn main() {} | ^ @@ -8,7 +8,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `std`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 | LL | fn main() {} | ^ @@ -17,7 +17,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` error: your program uses the crate `core`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 | LL | fn main() {} | ^ @@ -26,7 +26,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `core`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 | LL | fn main() {} | ^ @@ -35,7 +35,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` error: your program uses the crate `alloc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 | LL | fn main() {} | ^ @@ -44,7 +44,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `alloc`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 | LL | fn main() {} | ^ @@ -53,7 +53,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` error: your program uses the crate `compiler_builtins`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 | LL | fn main() {} | ^ @@ -62,7 +62,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `compiler_builtins`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 | LL | fn main() {} | ^ @@ -71,7 +71,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` error: your program uses the crate `libc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 | LL | fn main() {} | ^ @@ -80,7 +80,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `libc`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 | LL | fn main() {} | ^ diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.enable-separately-disable-together.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-2-errors.enable-separately-disable-together.stderr similarity index 89% rename from tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.enable-separately-disable-together.stderr rename to tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-2-errors.enable-separately-disable-together.stderr index 50febbee060f2..cbbe300365303 100644 --- a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.enable-separately-disable-together.stderr +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-2-errors.enable-separately-disable-together.stderr @@ -1,5 +1,5 @@ error: your program uses the crate `std`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 | LL | fn main() {} | ^ @@ -8,7 +8,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `std`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 | LL | fn main() {} | ^ @@ -17,7 +17,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` error: your program uses the crate `core`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 | LL | fn main() {} | ^ @@ -26,7 +26,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `core`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 | LL | fn main() {} | ^ @@ -35,7 +35,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` error: your program uses the crate `alloc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 | LL | fn main() {} | ^ @@ -44,7 +44,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `alloc`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 | LL | fn main() {} | ^ @@ -53,7 +53,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` error: your program uses the crate `compiler_builtins`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 | LL | fn main() {} | ^ @@ -62,7 +62,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `compiler_builtins`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 | LL | fn main() {} | ^ @@ -71,7 +71,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` error: your program uses the crate `libc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 | LL | fn main() {} | ^ @@ -80,7 +80,7 @@ LL | fn main() {} = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `libc`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations.rs:14:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 | LL | fn main() {} | ^ diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-2-errors.rs b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-2-errors.rs new file mode 100644 index 0000000000000..2ce4172023aad --- /dev/null +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-2-errors.rs @@ -0,0 +1,20 @@ +// ignore-tidy-linelength +//@ revisions: both enable-separately-disable-together +//@ check-fail +//@ ignore-nvptx64 stack protector is not supported +//@ ignore-wasm32-unknown-unknown stack protector is not supported +//@ edition:future +//@ [both] compile-flags: -Z unstable-options -C control-flow-guard=on -Z stack-protector=all +//@ [enable-separately-disable-together] compile-flags: -Z unstable-options -Z allow-partial-mitigations=stack-protector -Z allow-partial-mitigations=control-flow-guard -Z deny-partial-mitigations=control-flow-guard,stack-protector -C control-flow-guard=on -Z stack-protector=all + +fn main() {} +//~^ ERROR that is not compiled with +//~| ERROR that is not compiled with +//~| ERROR that is not compiled with +//~| ERROR that is not compiled with +//~| ERROR that is not compiled with +//~| ERROR that is not compiled with +//~| ERROR that is not compiled with +//~| ERROR that is not compiled with +//~| ERROR that is not compiled with +//~| ERROR that is not compiled with diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-bad-cli.rs b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-bad-cli.rs new file mode 100644 index 0000000000000..0f45830b9b64f --- /dev/null +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-bad-cli.rs @@ -0,0 +1,12 @@ +// ignore-tidy-linelength +//@ check-fail +//@ ignore-nvptx64 stack protector is not supported +//@ ignore-wasm32-unknown-unknown stack protector is not supported +//@ edition:future +//@ compile-flags: -Z unstable-options -Z deny-partial-mitigations=garbage + +// have a test that the list of mitigations is generated correctly + +//~? ERROR incorrect value `garbage` for unstable option `deny-partial-mitigations` - comma-separated list of mitigation kinds (available: + +fn main() {} diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-bad-cli.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-bad-cli.stderr new file mode 100644 index 0000000000000..9af53a1689aec --- /dev/null +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-bad-cli.stderr @@ -0,0 +1,2 @@ +error: incorrect value `garbage` for unstable option `deny-partial-mitigations` - comma-separated list of mitigation kinds (available: `stack-protector`, `control-flow-guard`) was expected + diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-current-edition.rs b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-current-edition.rs index d32c579a8cae1..5ed5edf63ece2 100644 --- a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-current-edition.rs +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-current-edition.rs @@ -3,7 +3,7 @@ //@ ignore-nvptx64 stack protector is not supported //@ ignore-wasm32-unknown-unknown stack protector is not supported //@ edition: 2024 -//@ compile-flags: -Z deny-partial-mitigations=control-flow-guard -C control-flow-guard=on +//@ compile-flags: -C control-flow-guard=on -Z deny-partial-mitigations=control-flow-guard // check that in edition 2024, it is still possible to explicitly // disallow partial mitigations (in edition=future, they are diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.rs b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.rs deleted file mode 100644 index 8ba8873053e05..0000000000000 --- a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations.rs +++ /dev/null @@ -1,54 +0,0 @@ -// ignore-tidy-linelength -//@ revisions: sp both disable enable-disable wrong-enable enable-separately-disable-together -//@ check-fail -//@ ignore-nvptx64 stack protector is not supported -//@ ignore-wasm32-unknown-unknown stack protector is not supported -//@ edition:future -//@ [both] compile-flags: -Z unstable-options -C control-flow-guard=on -Z stack-protector=all -//@ [sp] compile-flags: -Z unstable-options -Z stack-protector=all -//@ [disable] compile-flags: -Z unstable-options -Z deny-partial-mitigations=stack-protector -Z stack-protector=all -//@ [enable-disable] compile-flags: -Z unstable-options -Z allow-partial-mitigations=stack-protector -Z deny-partial-mitigations=stack-protector -Z stack-protector=all -//@ [wrong-enable] compile-flags: -Z unstable-options -Z allow-partial-mitigations=control-flow-guard -Z stack-protector=all -//@ [enable-separately-disable-together] compile-flags: -Z unstable-options -Z allow-partial-mitigations=stack-protector -Z allow-partial-mitigations=control-flow-guard -Z deny-partial-mitigations=control-flow-guard,stack-protector -C control-flow-guard=on -Z stack-protector=all - -fn main() {} -//[both]~^ ERROR that is not compiled with -//[both]~| ERROR that is not compiled with -//[both]~| ERROR that is not compiled with -//[both]~| ERROR that is not compiled with -//[both]~| ERROR that is not compiled with -//[both]~| ERROR that is not compiled with -//[both]~| ERROR that is not compiled with -//[both]~| ERROR that is not compiled with -//[both]~| ERROR that is not compiled with -//[both]~| ERROR that is not compiled with -//[sp]~^^^^^^^^^^^ ERROR that is not compiled with -//[sp]~| ERROR that is not compiled with -//[sp]~| ERROR that is not compiled with -//[sp]~| ERROR that is not compiled with -//[sp]~| ERROR that is not compiled with -//[disable]~^^^^^^^^^^^^^^^^ ERROR that is not compiled with -//[disable]~| ERROR that is not compiled with -//[disable]~| ERROR that is not compiled with -//[disable]~| ERROR that is not compiled with -//[disable]~| ERROR that is not compiled with -//[enable-disable]~^^^^^^^^^^^^^^^^^^^^^ ERROR that is not compiled with -//[enable-disable]~| ERROR that is not compiled with -//[enable-disable]~| ERROR that is not compiled with -//[enable-disable]~| ERROR that is not compiled with -//[enable-disable]~| ERROR that is not compiled with -//[wrong-enable]~^^^^^^^^^^^^^^^^^^^^^^^^^^ ERROR that is not compiled with -//[wrong-enable]~| ERROR that is not compiled with -//[wrong-enable]~| ERROR that is not compiled with -//[wrong-enable]~| ERROR that is not compiled with -//[wrong-enable]~| ERROR that is not compiled with -//[enable-separately-disable-together]~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ERROR that is not compiled with -//[enable-separately-disable-together]~| ERROR that is not compiled with -//[enable-separately-disable-together]~| ERROR that is not compiled with -//[enable-separately-disable-together]~| ERROR that is not compiled with -//[enable-separately-disable-together]~| ERROR that is not compiled with -//[enable-separately-disable-together]~| ERROR that is not compiled with -//[enable-separately-disable-together]~| ERROR that is not compiled with -//[enable-separately-disable-together]~| ERROR that is not compiled with -//[enable-separately-disable-together]~| ERROR that is not compiled with -//[enable-separately-disable-together]~| ERROR that is not compiled with diff --git a/tests/ui/allow-partial-mitigations/ok-allow-partial-mitigations-current-edition.rs b/tests/ui/allow-partial-mitigations/ok-allow-partial-mitigations-current-edition.rs new file mode 100644 index 0000000000000..6a9c297698b63 --- /dev/null +++ b/tests/ui/allow-partial-mitigations/ok-allow-partial-mitigations-current-edition.rs @@ -0,0 +1,13 @@ +// ignore-tidy-linelength +//@ revisions: no-deny deny-first +//@ check-pass +//@ ignore-nvptx64 stack protector is not supported +//@ ignore-wasm32-unknown-unknown stack protector is not supported +//@ edition: 2024 +//@ [deny-first] compile-flags: -Z deny-partial-mitigations=control-flow-guard -C control-flow-guard=on +//@ [no-deny] compile-flags: -C control-flow-guard=on + +// check that the `-C control-flow-guard=on` overrides the `-Z deny-partial-mitigations=control-flow-guard`, +// which in edition 2024 leads to partial mitigations being allowed + +fn main() {} diff --git a/tests/ui/allow-partial-mitigations/ok-allow-partial-mitigations.rs b/tests/ui/allow-partial-mitigations/ok-allow-partial-mitigations.rs index c0bc09276568f..804873f397516 100644 --- a/tests/ui/allow-partial-mitigations/ok-allow-partial-mitigations.rs +++ b/tests/ui/allow-partial-mitigations/ok-allow-partial-mitigations.rs @@ -4,8 +4,8 @@ //@ edition:future //@ ignore-nvptx64 stack protector is not supported //@ ignore-wasm32-unknown-unknown stack protector is not supported -//@ [both] compile-flags: -Z unstable-options -Z allow-partial-mitigations=stack-protector,control-flow-guard -C control-flow-guard=on -Z stack-protector=all -//@ [sp] compile-flags: -Z unstable-options -Z allow-partial-mitigations=stack-protector -Z stack-protector=all -//@ [disable-enable] compile-flags: -Z unstable-options -Z deny-partial-mitigations=stack-protector -Z allow-partial-mitigations=stack-protector -Z stack-protector=all +//@ [both] compile-flags: -Z unstable-options -Z stack-protector=all -C control-flow-guard=on -Z allow-partial-mitigations=stack-protector,control-flow-guard +//@ [sp] compile-flags: -Z unstable-options -Z stack-protector=all -Z allow-partial-mitigations=stack-protector +//@ [disable-enable] compile-flags: -Z unstable-options -Z deny-partial-mitigations=stack-protector -Z stack-protector=all -Z allow-partial-mitigations=stack-protector fn main() {} From 1b96797c0855dbe0bad2c7f98a21202aec718aae Mon Sep 17 00:00:00 2001 From: Ariel Ben-Yehuda Date: Tue, 7 Apr 2026 00:32:29 +0300 Subject: [PATCH 22/25] address review comments --- compiler/rustc_metadata/src/errors.rs | 2 +- compiler/rustc_metadata/src/rmeta/decoder.rs | 7 +- compiler/rustc_metadata/src/rmeta/encoder.rs | 4 +- .../src/options/mitigation_coverage.rs | 10 +- tests/ui/README.md | 4 +- ...d-future-allow-reset-by-mitigation.stderr} | 20 ++-- .../err-allow-partial-mitigations-1-error.rs | 35 +++++-- ...llow-partial-mitigations-1-error.sp.stderr | 47 ---------- ...or.stack-protector-allow-then-deny.stderr} | 20 ++-- ...ector-but-allow-control-flow-guard.stderr} | 20 ++-- ...r-future-allow-reset-by-mitigation.stderr} | 20 ++-- ...ure-deny-allow-reset-by-mitigation.stderr} | 20 ++-- ...tor-future-deny-reset-by-mitigation.stderr | 47 ++++++++++ ...tack-protector-future-explicit-deny.stderr | 47 ++++++++++ ...ions-1-error.stack-protector-future.stderr | 47 ++++++++++ ...al-mitigations-1-error.wrong-enable.stderr | 47 ---------- ...w-partial-mitigations-2-errors.both.stderr | 40 ++++---- ....enable-separately-disable-together.stderr | 40 ++++---- ....enable-together-disable-separately.stderr | 92 +++++++++++++++++++ .../err-allow-partial-mitigations-2-errors.rs | 12 ++- .../err-allow-partial-mitigations-bad-cli.rs | 2 +- ...on.control-flow-2024-explicit-deny.stderr} | 20 ++-- ...low-partial-mitigations-current-edition.rs | 3 +- ...low-partial-mitigations-current-edition.rs | 20 +++- .../ok-allow-partial-mitigations.rs | 15 ++- 25 files changed, 416 insertions(+), 225 deletions(-) rename tests/ui/allow-partial-mitigations/{err-allow-partial-mitigations-1-error.cfg-allow-first.stderr => err-allow-partial-mitigations-1-error.control-flow-guard-future-allow-reset-by-mitigation.stderr} (76%) delete mode 100644 tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.sp.stderr rename tests/ui/allow-partial-mitigations/{err-allow-partial-mitigations-1-error.sp-allow-first.stderr => err-allow-partial-mitigations-1-error.stack-protector-allow-then-deny.stderr} (76%) rename tests/ui/allow-partial-mitigations/{err-allow-partial-mitigations-1-error.disable-enable-reset.stderr => err-allow-partial-mitigations-1-error.stack-protector-but-allow-control-flow-guard.stderr} (76%) rename tests/ui/allow-partial-mitigations/{err-allow-partial-mitigations-1-error.disable.stderr => err-allow-partial-mitigations-1-error.stack-protector-future-allow-reset-by-mitigation.stderr} (76%) rename tests/ui/allow-partial-mitigations/{err-allow-partial-mitigations-1-error.enable-disable.stderr => err-allow-partial-mitigations-1-error.stack-protector-future-deny-allow-reset-by-mitigation.stderr} (76%) create mode 100644 tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.stack-protector-future-deny-reset-by-mitigation.stderr create mode 100644 tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.stack-protector-future-explicit-deny.stderr create mode 100644 tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.stack-protector-future.stderr delete mode 100644 tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.wrong-enable.stderr create mode 100644 tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-2-errors.enable-together-disable-separately.stderr rename tests/ui/allow-partial-mitigations/{err-allow-partial-mitigations-current-edition.stderr => err-allow-partial-mitigations-current-edition.control-flow-2024-explicit-deny.stderr} (75%) diff --git a/compiler/rustc_metadata/src/errors.rs b/compiler/rustc_metadata/src/errors.rs index 79568dcec81ec..a6f35a179d703 100644 --- a/compiler/rustc_metadata/src/errors.rs +++ b/compiler/rustc_metadata/src/errors.rs @@ -702,7 +702,7 @@ pub(crate) struct UnusedCrateDependency { "your program uses the crate `{$extern_crate}`, that is not compiled with `{$mitigation_name}{$mitigation_level}` enabled" )] #[note( - "recompile `{$extern_crate}` with `{$mitigation_name}{$mitigation_level}` enabled, or use `-Z allow-partial-mitigations={$mitigation_name}` to allow creating an artifact that has the mitigation only partially enabled " + "recompile `{$extern_crate}` with `{$mitigation_name}{$mitigation_level}` enabled, or use `-Z allow-partial-mitigations={$mitigation_name}` to allow creating an artifact that has the mitigation partially enabled " )] #[help( "it is possible to disable `-Z allow-partial-mitigations={$mitigation_name}` via `-Z deny-partial-mitigations={$mitigation_name}`" diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 2993eb340cfe4..280b3b12e5c85 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -79,10 +79,13 @@ impl MetadataBlob { /// own crate numbers. pub(crate) type CrateNumMap = IndexVec; -/// Target modifiers - abi or exploit mitigations flags that cause unsoundness when mixed +/// Target modifiers - abi or exploit mitigations options that may cause unsoundness when mixed or +/// partially enabled. pub(crate) type TargetModifiers = Vec; -/// The set of enforceable mitigations (RFC 3855) that are currently enabled for this crate +/// The set of mitigations that cannot be partially enabled (see +/// [RFC 3855](https://github.com/rust-lang/rfcs/pull/3855)), but are currently enabled for this +/// crate. pub(crate) type DeniedPartialMitigations = Vec; pub(crate) struct CrateMetadata { diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 2a0eeef442a33..17dc3564c6ac9 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -716,8 +716,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { // `SourceFiles` we actually need to encode. let source_map = stat!("source-map", || self.encode_source_map()); let target_modifiers = stat!("target-modifiers", || self.encode_target_modifiers()); - let denied_partial_mitigations = - stat!("enforced-mitigations", || self.encode_enabled_denied_partial_mitigations()); + let denied_partial_mitigations = stat!("denied-partial-mitigations", || self + .encode_enabled_denied_partial_mitigations()); let root = stat!("final", || { let attrs = tcx.hir_krate_attrs(); diff --git a/compiler/rustc_session/src/options/mitigation_coverage.rs b/compiler/rustc_session/src/options/mitigation_coverage.rs index cf839706fe787..f396392cd2638 100644 --- a/compiler/rustc_session/src/options/mitigation_coverage.rs +++ b/compiler/rustc_session/src/options/mitigation_coverage.rs @@ -167,12 +167,12 @@ macro_rules! denied_partial_mitigations { #[allow(unused)] impl DeniedPartialMitigationKind { pub fn allowed_by_default_at(&self, edition: Edition) -> bool { - let enforced_since = match self { - // Should change the enforced-since edition of StackProtector to 2015 + let denied_since = match self { + // Should change the denied-since edition of StackProtector to 2015 // (all editions) when `-C stack-protector` is stabilized. $(DeniedPartialMitigationKind::$name => Edition::$since),* }; - edition < enforced_since + edition < denied_since } } @@ -209,7 +209,9 @@ denied_partial_mitigations! { } } -/// Denied-partial mitigations, see [RFC 3855](https://github.com/rust-lang/rfcs/pull/3855) +/// A mitigation that cannot be partially enabled (see +/// [RFC 3855](https://github.com/rust-lang/rfcs/pull/3855)), but are currently enabled for this +/// crate. #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Encodable, BlobDecodable)] pub struct DeniedPartialMitigation { pub kind: DeniedPartialMitigationKind, diff --git a/tests/ui/README.md b/tests/ui/README.md index ab9b58aa4981f..4527105dd32ad 100644 --- a/tests/ui/README.md +++ b/tests/ui/README.md @@ -24,9 +24,9 @@ See [Allocator traits and `std::heap` #32838](https://github.com/rust-lang/rust/ ## `tests/ui/allow-partial-mitigations` -These tests exercise the check against partial mitigation enforcement. +These tests exercise the support for mitigation coverage and the `allow-partial-mitigations` and `deny-partial-mitigations` options. -See [the mitigation enforcement RFC](https://github.com/rust-lang/rfcs/pull/3855). +See [RFC 3855](https://github.com/rust-lang/rfcs/pull/3855). ## `tests/ui/annotate-moves` diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.cfg-allow-first.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.control-flow-guard-future-allow-reset-by-mitigation.stderr similarity index 76% rename from tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.cfg-allow-first.stderr rename to tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.control-flow-guard-future-allow-reset-by-mitigation.stderr index 24bcf83a9be6f..fa4b9a8c99f8c 100644 --- a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.cfg-allow-first.stderr +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.control-flow-guard-future-allow-reset-by-mitigation.stderr @@ -1,46 +1,46 @@ error: your program uses the crate `std`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 | LL | fn main() {} | ^ | - = note: recompile `std` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `std` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` error: your program uses the crate `core`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 | LL | fn main() {} | ^ | - = note: recompile `core` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `core` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` error: your program uses the crate `alloc`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 | LL | fn main() {} | ^ | - = note: recompile `alloc` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `alloc` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` error: your program uses the crate `compiler_builtins`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 | LL | fn main() {} | ^ | - = note: recompile `compiler_builtins` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `compiler_builtins` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` error: your program uses the crate `libc`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 | LL | fn main() {} | ^ | - = note: recompile `libc` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `libc` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` error: aborting due to 5 previous errors diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.rs b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.rs index 4b419e8212c10..f87d40196d6e5 100644 --- a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.rs +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.rs @@ -1,16 +1,35 @@ // ignore-tidy-linelength -//@ revisions: sp disable enable-disable wrong-enable disable-enable-reset cfg-allow-first sp-allow-first +//@ revisions: stack-protector-future stack-protector-future-explicit-deny stack-protector-future-deny-reset-by-mitigation stack-protector-allow-then-deny stack-protector-but-allow-control-flow-guard control-flow-guard-future-allow-reset-by-mitigation stack-protector-future-allow-reset-by-mitigation stack-protector-future-deny-allow-reset-by-mitigation //@ check-fail //@ ignore-nvptx64 stack protector is not supported //@ ignore-wasm32-unknown-unknown stack protector is not supported //@ edition:future -//@ [sp] compile-flags: -Z unstable-options -Z stack-protector=all -//@ [disable] compile-flags: -Z unstable-options -Z deny-partial-mitigations=stack-protector -Z stack-protector=all -//@ [enable-disable] compile-flags: -Z unstable-options -Z allow-partial-mitigations=stack-protector -Z deny-partial-mitigations=stack-protector -Z stack-protector=all -//@ [wrong-enable] compile-flags: -Z unstable-options -Z allow-partial-mitigations=control-flow-guard -Z stack-protector=all -//@ [cfg-allow-first] compile-flags: -Z unstable-options -Z allow-partial-mitigations=stack-protector -C control-flow-guard=on -//@ [sp-allow-first] compile-flags: -Z unstable-options -Z allow-partial-mitigations=stack-protector -Z stack-protector=all -//@ [disable-enable-reset] compile-flags: -Z unstable-options -Z deny-partial-mitigations=stack-protector -Z allow-partial-mitigations=stack-protector -Z stack-protector=all + +// test that stack-protector is denied-partial in edition=future +//@ [stack-protector-future] compile-flags: -Z unstable-options -Z stack-protector=all + +// same, but with explicit deny +//@ [stack-protector-future-explicit-deny] compile-flags: -Z unstable-options -Z stack-protector=all -Z deny-partial-mitigations=stack-protector + +// same, but with explicit deny before the enable. The `-Z stack-protector=all` resets the mitigation status +// to default which is deny at edition=future. +// at edition=2024, this would be allowed, see ok-allow-partial-mitigations-current-edition scenario stack-protector-future-deny-reset-by-mitigation +//@ [stack-protector-future-deny-reset-by-mitigation] compile-flags: -Z unstable-options -Z deny-partial-mitigations=stack-protector -Z stack-protector=all + +// same, but with explicit allow followed by explicit deny +//@ [stack-protector-allow-then-deny] compile-flags: -Z unstable-options -Z stack-protector=all -Z allow-partial-mitigations=stack-protector -Z deny-partial-mitigations=stack-protector + +// check that allowing an unrelated mitigation (control-flow-guard) does not allow a different mitigation (stack-protector) +//@ [stack-protector-but-allow-control-flow-guard] compile-flags: -Z unstable-options -Z stack-protector=all -Z allow-partial-mitigations=control-flow-guard + +// check that `-C control-flow-guard` overrides the `-Z allow-partial-mitigations=control-flow-guard` (to the default, which is deny at edition=future) +//@ [control-flow-guard-future-allow-reset-by-mitigation] compile-flags: -Z unstable-options -Z allow-partial-mitigations=control-flow-guard -C control-flow-guard=on + +// check that `-Z stack-protector` overrides the `-Z allow-partial-mitigations=stack-protector` (to the default, which is deny at edition=future) +//@ [stack-protector-future-allow-reset-by-mitigation] compile-flags: -Z unstable-options -Z allow-partial-mitigations=stack-protector -Z stack-protector=all + +// check that this is the case even if there was a "deny" before the "allow" +//@ [stack-protector-future-deny-allow-reset-by-mitigation] compile-flags: -Z unstable-options -Z deny-partial-mitigations=stack-protector -Z allow-partial-mitigations=stack-protector -Z stack-protector=all fn main() {} //~^ ERROR that is not compiled with diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.sp.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.sp.stderr deleted file mode 100644 index f039dc7acbd09..0000000000000 --- a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.sp.stderr +++ /dev/null @@ -1,47 +0,0 @@ -error: your program uses the crate `std`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 - | -LL | fn main() {} - | ^ - | - = note: recompile `std` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled - = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` - -error: your program uses the crate `core`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 - | -LL | fn main() {} - | ^ - | - = note: recompile `core` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled - = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` - -error: your program uses the crate `alloc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 - | -LL | fn main() {} - | ^ - | - = note: recompile `alloc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled - = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` - -error: your program uses the crate `compiler_builtins`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 - | -LL | fn main() {} - | ^ - | - = note: recompile `compiler_builtins` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled - = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` - -error: your program uses the crate `libc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 - | -LL | fn main() {} - | ^ - | - = note: recompile `libc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled - = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` - -error: aborting due to 5 previous errors - diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.sp-allow-first.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.stack-protector-allow-then-deny.stderr similarity index 76% rename from tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.sp-allow-first.stderr rename to tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.stack-protector-allow-then-deny.stderr index f039dc7acbd09..3565173435b53 100644 --- a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.sp-allow-first.stderr +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.stack-protector-allow-then-deny.stderr @@ -1,46 +1,46 @@ error: your program uses the crate `std`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 | LL | fn main() {} | ^ | - = note: recompile `std` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `std` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `core`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 | LL | fn main() {} | ^ | - = note: recompile `core` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `core` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `alloc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 | LL | fn main() {} | ^ | - = note: recompile `alloc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `alloc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `compiler_builtins`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 | LL | fn main() {} | ^ | - = note: recompile `compiler_builtins` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `compiler_builtins` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `libc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 | LL | fn main() {} | ^ | - = note: recompile `libc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `libc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: aborting due to 5 previous errors diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.disable-enable-reset.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.stack-protector-but-allow-control-flow-guard.stderr similarity index 76% rename from tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.disable-enable-reset.stderr rename to tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.stack-protector-but-allow-control-flow-guard.stderr index f039dc7acbd09..3565173435b53 100644 --- a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.disable-enable-reset.stderr +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.stack-protector-but-allow-control-flow-guard.stderr @@ -1,46 +1,46 @@ error: your program uses the crate `std`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 | LL | fn main() {} | ^ | - = note: recompile `std` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `std` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `core`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 | LL | fn main() {} | ^ | - = note: recompile `core` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `core` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `alloc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 | LL | fn main() {} | ^ | - = note: recompile `alloc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `alloc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `compiler_builtins`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 | LL | fn main() {} | ^ | - = note: recompile `compiler_builtins` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `compiler_builtins` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `libc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 | LL | fn main() {} | ^ | - = note: recompile `libc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `libc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: aborting due to 5 previous errors diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.disable.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.stack-protector-future-allow-reset-by-mitigation.stderr similarity index 76% rename from tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.disable.stderr rename to tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.stack-protector-future-allow-reset-by-mitigation.stderr index f039dc7acbd09..3565173435b53 100644 --- a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.disable.stderr +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.stack-protector-future-allow-reset-by-mitigation.stderr @@ -1,46 +1,46 @@ error: your program uses the crate `std`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 | LL | fn main() {} | ^ | - = note: recompile `std` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `std` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `core`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 | LL | fn main() {} | ^ | - = note: recompile `core` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `core` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `alloc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 | LL | fn main() {} | ^ | - = note: recompile `alloc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `alloc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `compiler_builtins`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 | LL | fn main() {} | ^ | - = note: recompile `compiler_builtins` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `compiler_builtins` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `libc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 | LL | fn main() {} | ^ | - = note: recompile `libc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `libc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: aborting due to 5 previous errors diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.enable-disable.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.stack-protector-future-deny-allow-reset-by-mitigation.stderr similarity index 76% rename from tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.enable-disable.stderr rename to tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.stack-protector-future-deny-allow-reset-by-mitigation.stderr index f039dc7acbd09..3565173435b53 100644 --- a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.enable-disable.stderr +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.stack-protector-future-deny-allow-reset-by-mitigation.stderr @@ -1,46 +1,46 @@ error: your program uses the crate `std`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 | LL | fn main() {} | ^ | - = note: recompile `std` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `std` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `core`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 | LL | fn main() {} | ^ | - = note: recompile `core` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `core` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `alloc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 | LL | fn main() {} | ^ | - = note: recompile `alloc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `alloc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `compiler_builtins`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 | LL | fn main() {} | ^ | - = note: recompile `compiler_builtins` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `compiler_builtins` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `libc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 | LL | fn main() {} | ^ | - = note: recompile `libc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `libc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: aborting due to 5 previous errors diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.stack-protector-future-deny-reset-by-mitigation.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.stack-protector-future-deny-reset-by-mitigation.stderr new file mode 100644 index 0000000000000..3565173435b53 --- /dev/null +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.stack-protector-future-deny-reset-by-mitigation.stderr @@ -0,0 +1,47 @@ +error: your program uses the crate `std`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `std` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `core`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `core` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `alloc`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `alloc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `compiler_builtins`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `compiler_builtins` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `libc`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `libc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: aborting due to 5 previous errors + diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.stack-protector-future-explicit-deny.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.stack-protector-future-explicit-deny.stderr new file mode 100644 index 0000000000000..3565173435b53 --- /dev/null +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.stack-protector-future-explicit-deny.stderr @@ -0,0 +1,47 @@ +error: your program uses the crate `std`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `std` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `core`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `core` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `alloc`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `alloc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `compiler_builtins`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `compiler_builtins` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `libc`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `libc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: aborting due to 5 previous errors + diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.stack-protector-future.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.stack-protector-future.stderr new file mode 100644 index 0000000000000..3565173435b53 --- /dev/null +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.stack-protector-future.stderr @@ -0,0 +1,47 @@ +error: your program uses the crate `std`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `std` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `core`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `core` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `alloc`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `alloc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `compiler_builtins`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `compiler_builtins` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `libc`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations-1-error.rs:34:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `libc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: aborting due to 5 previous errors + diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.wrong-enable.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.wrong-enable.stderr deleted file mode 100644 index f039dc7acbd09..0000000000000 --- a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-1-error.wrong-enable.stderr +++ /dev/null @@ -1,47 +0,0 @@ -error: your program uses the crate `std`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 - | -LL | fn main() {} - | ^ - | - = note: recompile `std` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled - = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` - -error: your program uses the crate `core`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 - | -LL | fn main() {} - | ^ - | - = note: recompile `core` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled - = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` - -error: your program uses the crate `alloc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 - | -LL | fn main() {} - | ^ - | - = note: recompile `alloc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled - = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` - -error: your program uses the crate `compiler_builtins`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 - | -LL | fn main() {} - | ^ - | - = note: recompile `compiler_builtins` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled - = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` - -error: your program uses the crate `libc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-1-error.rs:15:1 - | -LL | fn main() {} - | ^ - | - = note: recompile `libc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled - = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` - -error: aborting due to 5 previous errors - diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-2-errors.both.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-2-errors.both.stderr index cbbe300365303..98029972ea19c 100644 --- a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-2-errors.both.stderr +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-2-errors.both.stderr @@ -1,91 +1,91 @@ error: your program uses the crate `std`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:18:1 | LL | fn main() {} | ^ | - = note: recompile `std` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `std` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `std`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:18:1 | LL | fn main() {} | ^ | - = note: recompile `std` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `std` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` error: your program uses the crate `core`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:18:1 | LL | fn main() {} | ^ | - = note: recompile `core` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `core` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `core`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:18:1 | LL | fn main() {} | ^ | - = note: recompile `core` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `core` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` error: your program uses the crate `alloc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:18:1 | LL | fn main() {} | ^ | - = note: recompile `alloc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `alloc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `alloc`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:18:1 | LL | fn main() {} | ^ | - = note: recompile `alloc` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `alloc` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` error: your program uses the crate `compiler_builtins`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:18:1 | LL | fn main() {} | ^ | - = note: recompile `compiler_builtins` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `compiler_builtins` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `compiler_builtins`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:18:1 | LL | fn main() {} | ^ | - = note: recompile `compiler_builtins` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `compiler_builtins` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` error: your program uses the crate `libc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:18:1 | LL | fn main() {} | ^ | - = note: recompile `libc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `libc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `libc`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:18:1 | LL | fn main() {} | ^ | - = note: recompile `libc` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `libc` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` error: aborting due to 10 previous errors diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-2-errors.enable-separately-disable-together.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-2-errors.enable-separately-disable-together.stderr index cbbe300365303..98029972ea19c 100644 --- a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-2-errors.enable-separately-disable-together.stderr +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-2-errors.enable-separately-disable-together.stderr @@ -1,91 +1,91 @@ error: your program uses the crate `std`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:18:1 | LL | fn main() {} | ^ | - = note: recompile `std` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `std` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `std`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:18:1 | LL | fn main() {} | ^ | - = note: recompile `std` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `std` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` error: your program uses the crate `core`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:18:1 | LL | fn main() {} | ^ | - = note: recompile `core` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `core` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `core`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:18:1 | LL | fn main() {} | ^ | - = note: recompile `core` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `core` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` error: your program uses the crate `alloc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:18:1 | LL | fn main() {} | ^ | - = note: recompile `alloc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `alloc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `alloc`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:18:1 | LL | fn main() {} | ^ | - = note: recompile `alloc` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `alloc` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` error: your program uses the crate `compiler_builtins`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:18:1 | LL | fn main() {} | ^ | - = note: recompile `compiler_builtins` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `compiler_builtins` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `compiler_builtins`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:18:1 | LL | fn main() {} | ^ | - = note: recompile `compiler_builtins` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `compiler_builtins` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` error: your program uses the crate `libc`, that is not compiled with `stack-protector=all` enabled - --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:18:1 | LL | fn main() {} | ^ | - = note: recompile `libc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `libc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` error: your program uses the crate `libc`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations-2-errors.rs:10:1 + --> $DIR/err-allow-partial-mitigations-2-errors.rs:18:1 | LL | fn main() {} | ^ | - = note: recompile `libc` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `libc` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` error: aborting due to 10 previous errors diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-2-errors.enable-together-disable-separately.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-2-errors.enable-together-disable-separately.stderr new file mode 100644 index 0000000000000..98029972ea19c --- /dev/null +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-2-errors.enable-together-disable-separately.stderr @@ -0,0 +1,92 @@ +error: your program uses the crate `std`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations-2-errors.rs:18:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `std` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `std`, that is not compiled with `control-flow-guard` enabled + --> $DIR/err-allow-partial-mitigations-2-errors.rs:18:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `std` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` + +error: your program uses the crate `core`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations-2-errors.rs:18:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `core` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `core`, that is not compiled with `control-flow-guard` enabled + --> $DIR/err-allow-partial-mitigations-2-errors.rs:18:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `core` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` + +error: your program uses the crate `alloc`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations-2-errors.rs:18:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `alloc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `alloc`, that is not compiled with `control-flow-guard` enabled + --> $DIR/err-allow-partial-mitigations-2-errors.rs:18:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `alloc` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` + +error: your program uses the crate `compiler_builtins`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations-2-errors.rs:18:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `compiler_builtins` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `compiler_builtins`, that is not compiled with `control-flow-guard` enabled + --> $DIR/err-allow-partial-mitigations-2-errors.rs:18:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `compiler_builtins` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` + +error: your program uses the crate `libc`, that is not compiled with `stack-protector=all` enabled + --> $DIR/err-allow-partial-mitigations-2-errors.rs:18:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `libc` with `stack-protector=all` enabled, or use `-Z allow-partial-mitigations=stack-protector` to allow creating an artifact that has the mitigation partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=stack-protector` via `-Z deny-partial-mitigations=stack-protector` + +error: your program uses the crate `libc`, that is not compiled with `control-flow-guard` enabled + --> $DIR/err-allow-partial-mitigations-2-errors.rs:18:1 + | +LL | fn main() {} + | ^ + | + = note: recompile `libc` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation partially enabled + = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` + +error: aborting due to 10 previous errors + diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-2-errors.rs b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-2-errors.rs index 2ce4172023aad..88809892c8af4 100644 --- a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-2-errors.rs +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-2-errors.rs @@ -1,11 +1,19 @@ // ignore-tidy-linelength -//@ revisions: both enable-separately-disable-together +//@ revisions: both enable-separately-disable-together enable-together-disable-separately //@ check-fail //@ ignore-nvptx64 stack protector is not supported //@ ignore-wasm32-unknown-unknown stack protector is not supported //@ edition:future + +// just use 2 partial mitigations, without any allow/deny flag. Should be denied at edition=future. //@ [both] compile-flags: -Z unstable-options -C control-flow-guard=on -Z stack-protector=all -//@ [enable-separately-disable-together] compile-flags: -Z unstable-options -Z allow-partial-mitigations=stack-protector -Z allow-partial-mitigations=control-flow-guard -Z deny-partial-mitigations=control-flow-guard,stack-protector -C control-flow-guard=on -Z stack-protector=all + +// check that mitigations are denied if they are enabled separately and then disabled in a single command, +// to test the "foo,bar" syntax +//@ [enable-separately-disable-together] compile-flags: -Z unstable-options -C control-flow-guard=on -Z stack-protector=all -Z allow-partial-mitigations=stack-protector -Z allow-partial-mitigations=control-flow-guard -Z deny-partial-mitigations=control-flow-guard,stack-protector + +// same, but for allow +//@ [enable-together-disable-separately] compile-flags: -Z unstable-options -C control-flow-guard=on -Z stack-protector=all -Z allow-partial-mitigations=stack-protector,control-flow-guard -Z deny-partial-mitigations=control-flow-guard -Z deny-partial-mitigations=stack-protector fn main() {} //~^ ERROR that is not compiled with diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-bad-cli.rs b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-bad-cli.rs index 0f45830b9b64f..c5fe5b0e1158c 100644 --- a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-bad-cli.rs +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-bad-cli.rs @@ -5,7 +5,7 @@ //@ edition:future //@ compile-flags: -Z unstable-options -Z deny-partial-mitigations=garbage -// have a test that the list of mitigations is generated correctly +// test that the list of mitigations in the error message is generated correctly //~? ERROR incorrect value `garbage` for unstable option `deny-partial-mitigations` - comma-separated list of mitigation kinds (available: diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-current-edition.stderr b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-current-edition.control-flow-2024-explicit-deny.stderr similarity index 75% rename from tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-current-edition.stderr rename to tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-current-edition.control-flow-2024-explicit-deny.stderr index 02e9154986791..7940f3f907d3b 100644 --- a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-current-edition.stderr +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-current-edition.control-flow-2024-explicit-deny.stderr @@ -1,46 +1,46 @@ error: your program uses the crate `std`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations-current-edition.rs:12:1 + --> $DIR/err-allow-partial-mitigations-current-edition.rs:13:1 | LL | fn main() {} | ^ | - = note: recompile `std` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `std` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` error: your program uses the crate `core`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations-current-edition.rs:12:1 + --> $DIR/err-allow-partial-mitigations-current-edition.rs:13:1 | LL | fn main() {} | ^ | - = note: recompile `core` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `core` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` error: your program uses the crate `alloc`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations-current-edition.rs:12:1 + --> $DIR/err-allow-partial-mitigations-current-edition.rs:13:1 | LL | fn main() {} | ^ | - = note: recompile `alloc` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `alloc` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` error: your program uses the crate `compiler_builtins`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations-current-edition.rs:12:1 + --> $DIR/err-allow-partial-mitigations-current-edition.rs:13:1 | LL | fn main() {} | ^ | - = note: recompile `compiler_builtins` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `compiler_builtins` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` error: your program uses the crate `libc`, that is not compiled with `control-flow-guard` enabled - --> $DIR/err-allow-partial-mitigations-current-edition.rs:12:1 + --> $DIR/err-allow-partial-mitigations-current-edition.rs:13:1 | LL | fn main() {} | ^ | - = note: recompile `libc` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation only partially enabled + = note: recompile `libc` with `control-flow-guard` enabled, or use `-Z allow-partial-mitigations=control-flow-guard` to allow creating an artifact that has the mitigation partially enabled = help: it is possible to disable `-Z allow-partial-mitigations=control-flow-guard` via `-Z deny-partial-mitigations=control-flow-guard` error: aborting due to 5 previous errors diff --git a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-current-edition.rs b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-current-edition.rs index 5ed5edf63ece2..4f5acb0280d0b 100644 --- a/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-current-edition.rs +++ b/tests/ui/allow-partial-mitigations/err-allow-partial-mitigations-current-edition.rs @@ -1,9 +1,10 @@ // ignore-tidy-linelength +//@ revisions: control-flow-2024-explicit-deny //@ check-fail //@ ignore-nvptx64 stack protector is not supported //@ ignore-wasm32-unknown-unknown stack protector is not supported //@ edition: 2024 -//@ compile-flags: -C control-flow-guard=on -Z deny-partial-mitigations=control-flow-guard +//@ [control-flow-2024-explicit-deny] compile-flags: -C control-flow-guard=on -Z deny-partial-mitigations=control-flow-guard // check that in edition 2024, it is still possible to explicitly // disallow partial mitigations (in edition=future, they are diff --git a/tests/ui/allow-partial-mitigations/ok-allow-partial-mitigations-current-edition.rs b/tests/ui/allow-partial-mitigations/ok-allow-partial-mitigations-current-edition.rs index 6a9c297698b63..dddad265aff4a 100644 --- a/tests/ui/allow-partial-mitigations/ok-allow-partial-mitigations-current-edition.rs +++ b/tests/ui/allow-partial-mitigations/ok-allow-partial-mitigations-current-edition.rs @@ -1,13 +1,25 @@ // ignore-tidy-linelength -//@ revisions: no-deny deny-first +//@ revisions: control-flow-guard-2024-default control-flow-guard-2024-deny-reset-by-mitigation stack-protector-2024-deny-reset-by-mitigation stack-protector-2024-allow-deny-reset-by-mitigation //@ check-pass //@ ignore-nvptx64 stack protector is not supported //@ ignore-wasm32-unknown-unknown stack protector is not supported //@ edition: 2024 -//@ [deny-first] compile-flags: -Z deny-partial-mitigations=control-flow-guard -C control-flow-guard=on -//@ [no-deny] compile-flags: -C control-flow-guard=on // check that the `-C control-flow-guard=on` overrides the `-Z deny-partial-mitigations=control-flow-guard`, -// which in edition 2024 leads to partial mitigations being allowed +// which in edition 2024 leads to partial mitigations being allowed. Test with both an explicit +// deny and without one. + +// just test control-flow-guard at edition 2024. allowed-partial due to backwards compatibility. +//@ [control-flow-guard-2024-default] compile-flags: -C control-flow-guard=on + +// test that -C control-flow-guard=on resets -Z deny-partial-mitigations=control-flow-guard +//@ [control-flow-guard-2024-deny-reset-by-mitigation] compile-flags: -Z deny-partial-mitigations=control-flow-guard -C control-flow-guard=on + +// same but for stack-protector, to match the stack-protector-future-deny-reset-by-mitigation test in +// err-allow-partial-mitigations-1-error (which has the same args but on edition=future). +//@ [stack-protector-2024-deny-reset-by-mitigation] compile-flags: -Z deny-partial-mitigations=stack-protector -Z stack-protector=all + +// check that this is the case even if there was an "allow" then a "deny" +//@ [stack-protector-2024-allow-deny-reset-by-mitigation] compile-flags: -Z unstable-options -Z allow-partial-mitigations=stack-protector -Z deny-partial-mitigations=stack-protector -Z stack-protector=all fn main() {} diff --git a/tests/ui/allow-partial-mitigations/ok-allow-partial-mitigations.rs b/tests/ui/allow-partial-mitigations/ok-allow-partial-mitigations.rs index 804873f397516..f9f013e44cc62 100644 --- a/tests/ui/allow-partial-mitigations/ok-allow-partial-mitigations.rs +++ b/tests/ui/allow-partial-mitigations/ok-allow-partial-mitigations.rs @@ -1,11 +1,18 @@ // ignore-tidy-linelength -//@ revisions: sp both disable-enable +//@ revisions: stack-protector-explicit-allow stack-protector-and-control-flow-guard-explicit-allow stack-protector-deny-then-allow //@ check-pass //@ edition:future //@ ignore-nvptx64 stack protector is not supported //@ ignore-wasm32-unknown-unknown stack protector is not supported -//@ [both] compile-flags: -Z unstable-options -Z stack-protector=all -C control-flow-guard=on -Z allow-partial-mitigations=stack-protector,control-flow-guard -//@ [sp] compile-flags: -Z unstable-options -Z stack-protector=all -Z allow-partial-mitigations=stack-protector -//@ [disable-enable] compile-flags: -Z unstable-options -Z deny-partial-mitigations=stack-protector -Z stack-protector=all -Z allow-partial-mitigations=stack-protector + +// requesting both stack-protector and control-flow-guard and then allow-partial-mitigations it +//@ [stack-protector-and-control-flow-guard-explicit-allow] compile-flags: -Z unstable-options -Z stack-protector=all -C control-flow-guard=on -Z allow-partial-mitigations=stack-protector,control-flow-guard + +// requesting stack-protector and then allow-partial-mitigations it +//@ [stack-protector-explicit-allow] compile-flags: -Z unstable-options -Z stack-protector=all -Z allow-partial-mitigations=stack-protector + +// testing that the later allow-partial-mitigations overrides the earlier deny-partial-mitigations +// see also the stack-protector-allow-then-deny test (in the error tests) for the other order +//@ [stack-protector-deny-then-allow] compile-flags: -Z unstable-options -Z stack-protector=all -Z deny-partial-mitigations=stack-protector -Z allow-partial-mitigations=stack-protector fn main() {} From 46befd885d24b2f019c880093de5e098d2e1bd44 Mon Sep 17 00:00:00 2001 From: CoCo-Japan-pan <115922543+CoCo-Japan-pan@users.noreply.github.com> Date: Fri, 10 Apr 2026 22:40:29 +0900 Subject: [PATCH 23/25] Display only crate name for external trait `impl` restrictions --- compiler/rustc_middle/src/ty/trait_def.rs | 2 +- tests/ui/impl-restriction/impl-restriction-check.e2015.stderr | 2 +- tests/ui/impl-restriction/impl-restriction-check.e2018.stderr | 2 +- tests/ui/impl-restriction/impl-restriction-check.rs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_middle/src/ty/trait_def.rs b/compiler/rustc_middle/src/ty/trait_def.rs index a58a9c9d75792..af5d81a108ba7 100644 --- a/compiler/rustc_middle/src/ty/trait_def.rs +++ b/compiler/rustc_middle/src/ty/trait_def.rs @@ -140,7 +140,7 @@ impl ImplRestrictionKind { if restricted_to.krate == rustc_hir::def_id::LOCAL_CRATE { with_crate_prefix!(with_no_trimmed_paths!(tcx.def_path_str(restricted_to))) } else { - with_no_trimmed_paths!(tcx.def_path_str(restricted_to)) + tcx.def_path_str(restricted_to.krate.as_mod_def_id()) } } } diff --git a/tests/ui/impl-restriction/impl-restriction-check.e2015.stderr b/tests/ui/impl-restriction/impl-restriction-check.e2015.stderr index 0534705835f4c..402464f8a545d 100644 --- a/tests/ui/impl-restriction/impl-restriction-check.e2015.stderr +++ b/tests/ui/impl-restriction/impl-restriction-check.e2015.stderr @@ -10,7 +10,7 @@ note: trait restricted here LL | pub impl(crate) trait TopLevel {} | ^^^^^^^^^^^ -error: trait cannot be implemented outside `external::inner` +error: trait cannot be implemented outside `external` --> $DIR/impl-restriction-check.rs:13:1 | LL | impl external::inner::Inner for LocalType {} diff --git a/tests/ui/impl-restriction/impl-restriction-check.e2018.stderr b/tests/ui/impl-restriction/impl-restriction-check.e2018.stderr index d350c7f514142..8bd256cc9cae6 100644 --- a/tests/ui/impl-restriction/impl-restriction-check.e2018.stderr +++ b/tests/ui/impl-restriction/impl-restriction-check.e2018.stderr @@ -10,7 +10,7 @@ note: trait restricted here LL | pub impl(crate) trait TopLevel {} | ^^^^^^^^^^^ -error: trait cannot be implemented outside `external::inner` +error: trait cannot be implemented outside `external` --> $DIR/impl-restriction-check.rs:13:1 | LL | impl external::inner::Inner for LocalType {} diff --git a/tests/ui/impl-restriction/impl-restriction-check.rs b/tests/ui/impl-restriction/impl-restriction-check.rs index b4dd64de0c054..cf5e699dfce91 100644 --- a/tests/ui/impl-restriction/impl-restriction-check.rs +++ b/tests/ui/impl-restriction/impl-restriction-check.rs @@ -10,7 +10,7 @@ extern crate external_impl_restriction as external; struct LocalType; // needed to avoid orphan rule errors impl external::TopLevel for LocalType {} //~ ERROR trait cannot be implemented outside `external` -impl external::inner::Inner for LocalType {} //~ ERROR trait cannot be implemented outside `external::inner` +impl external::inner::Inner for LocalType {} //~ ERROR trait cannot be implemented outside `external` pub mod foo { pub mod bar { From 1137762b09fa78860fd39df8b1308f125e3642fb Mon Sep 17 00:00:00 2001 From: Jake Drew Date: Fri, 10 Apr 2026 22:47:09 +0100 Subject: [PATCH 24/25] Suggest similar target names on unrecognized `--target` --- compiler/rustc_session/src/config.rs | 13 +++++++++++++ tests/ui/errors/unknown-target-suggestion.rs | 11 +++++++++++ tests/ui/errors/unknown-target-suggestion.stderr | 5 +++++ tests/ui/errors/wrong-target-spec.stderr | 1 + 4 files changed, 30 insertions(+) create mode 100644 tests/ui/errors/unknown-target-suggestion.rs create mode 100644 tests/ui/errors/unknown-target-suggestion.stderr diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index e4ef1d40d72d5..34168760151c1 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -1615,6 +1615,19 @@ pub fn build_target_config( let mut err = early_dcx.early_struct_fatal(format!("error loading target specification: {e}")); err.help("run `rustc --print target-list` for a list of built-in targets"); + let typed = target.tuple(); + let limit = typed.len() / 3 + 1; + if let Some(suggestion) = rustc_target::spec::TARGETS + .iter() + .filter_map(|&t| { + rustc_span::edit_distance::edit_distance_with_substrings(typed, t, limit) + .map(|d| (d, t)) + }) + .min_by_key(|(d, _)| *d) + .map(|(_, t)| t) + { + err.help(format!("did you mean `{suggestion}`?")); + } err.emit() } } diff --git a/tests/ui/errors/unknown-target-suggestion.rs b/tests/ui/errors/unknown-target-suggestion.rs new file mode 100644 index 0000000000000..0c371aed921a7 --- /dev/null +++ b/tests/ui/errors/unknown-target-suggestion.rs @@ -0,0 +1,11 @@ +// Checks that an unknown --target also suggests a similar known target. +// See https://github.com/rust-lang/rust/issues/155085 + +// ignore-tidy-target-specific-tests +//@ compile-flags: --target x86_64-linux-gnu + +fn main() {} + +//~? ERROR error loading target specification: could not find specification for target "x86_64-linux-gnu" +//~? HELP run `rustc --print target-list` for a list of built-in targets +//~? HELP did you mean `x86_64-unknown-linux-gnu` diff --git a/tests/ui/errors/unknown-target-suggestion.stderr b/tests/ui/errors/unknown-target-suggestion.stderr new file mode 100644 index 0000000000000..09d4827241520 --- /dev/null +++ b/tests/ui/errors/unknown-target-suggestion.stderr @@ -0,0 +1,5 @@ +error: error loading target specification: could not find specification for target "x86_64-linux-gnu" + | + = help: run `rustc --print target-list` for a list of built-in targets + = help: did you mean `x86_64-unknown-linux-gnu`? + diff --git a/tests/ui/errors/wrong-target-spec.stderr b/tests/ui/errors/wrong-target-spec.stderr index 98b03ae00cb3a..cab485312bfdd 100644 --- a/tests/ui/errors/wrong-target-spec.stderr +++ b/tests/ui/errors/wrong-target-spec.stderr @@ -1,4 +1,5 @@ error: error loading target specification: could not find specification for target "x86_64_unknown-linux-musl" | = help: run `rustc --print target-list` for a list of built-in targets + = help: did you mean `x86_64-unknown-linux-musl`? From 270ebfcdf5e08dca9ab7fe5ecb764bfadc4916cc Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Fri, 10 Apr 2026 22:42:12 -0400 Subject: [PATCH 25/25] Tweak comment about intrinsics in cross-crate-inlinable --- compiler/rustc_mir_transform/src/cross_crate_inline.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_mir_transform/src/cross_crate_inline.rs b/compiler/rustc_mir_transform/src/cross_crate_inline.rs index 53aa5f450dbb3..644f2d22ef7ff 100644 --- a/compiler/rustc_mir_transform/src/cross_crate_inline.rs +++ b/compiler/rustc_mir_transform/src/cross_crate_inline.rs @@ -146,8 +146,9 @@ impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> { TerminatorKind::Call { func, unwind, .. } => { // We track calls because they make our function not a leaf (and in theory, the // number of calls indicates how likely this function is to perturb other CGUs). - // But intrinsics don't have a body that gets assigned to a CGU, so they are - // ignored. + // But there are a handful of intrinsics such as raw_eq that should not block + // cross-crate-inlining. Adding a broad exception for all intrinsics benchmarks well + // and seems more sustainable than an ever-growing list of intrinsics to ignore. if let Some((fn_def_id, _)) = func.const_fn_def() && find_attr!(tcx, fn_def_id, RustcIntrinsic) {