Skip to content

Commit 4cd4c18

Browse files
committed
Auto merge of #152373 - Zalathar:rollup-wDsfoHs, r=Zalathar
Rollup of 4 pull requests Successful merges: - #150823 (Implement MVP for opaque generic const arguments) - #152071 (Implement stdio FD constants) - #152171 (Port `rustc_strict_coherence` to the new attribute parser) - #152291 (Port `rustc_insignificant_dtor`) Failed merges: - #152180 (Port `rustc_reservation_impl` to the new attribute parser)
2 parents 39219ce + 7cb4501 commit 4cd4c18

59 files changed

Lines changed: 457 additions & 73 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

compiler/rustc_ast_passes/src/errors.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,16 @@ pub(crate) struct IncompatibleFeatures {
950950
pub f2: Symbol,
951951
}
952952

953+
#[derive(Diagnostic)]
954+
#[diag("`{$parent}` requires {$missing} to be enabled")]
955+
#[help("enable all of these features")]
956+
pub(crate) struct MissingDependentFeatures {
957+
#[primary_span]
958+
pub parent_span: Span,
959+
pub parent: Symbol,
960+
pub missing: String,
961+
}
962+
953963
#[derive(Diagnostic)]
954964
#[diag("negative bounds are not supported")]
955965
pub(crate) struct NegativeBoundUnsupported {

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
441441
pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
442442
maybe_stage_features(sess, features, krate);
443443
check_incompatible_features(sess, features);
444+
check_dependent_features(sess, features);
444445
check_new_solver_banned_features(sess, features);
445446

446447
let mut visitor = PostExpansionVisitor { sess, features };
@@ -649,6 +650,27 @@ fn check_incompatible_features(sess: &Session, features: &Features) {
649650
}
650651
}
651652

653+
fn check_dependent_features(sess: &Session, features: &Features) {
654+
for &(parent, children) in
655+
rustc_feature::DEPENDENT_FEATURES.iter().filter(|(parent, _)| features.enabled(*parent))
656+
{
657+
if children.iter().any(|f| !features.enabled(*f)) {
658+
let parent_span = features
659+
.enabled_features_iter_stable_order()
660+
.find_map(|(name, span)| (name == parent).then_some(span))
661+
.unwrap();
662+
// FIXME: should probably format this in fluent instead of here
663+
let missing = children
664+
.iter()
665+
.filter(|f| !features.enabled(**f))
666+
.map(|s| format!("`{}`", s.as_str()))
667+
.intersperse(String::from(", "))
668+
.collect();
669+
sess.dcx().emit_err(errors::MissingDependentFeatures { parent_span, parent, missing });
670+
}
671+
}
672+
}
673+
652674
fn check_new_solver_banned_features(sess: &Session, features: &Features) {
653675
if !sess.opts.unstable_opts.next_solver.globally {
654676
return;

compiler/rustc_ast_passes/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// tidy-alphabetical-start
66
#![feature(box_patterns)]
77
#![feature(if_let_guard)]
8+
#![feature(iter_intersperse)]
89
#![feature(iter_is_partitioned)]
910
// tidy-alphabetical-end
1011

compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,19 @@ impl<S: Stage> CombineAttributeParser<S> for RustcThenThisWouldNeedParser {
739739
}
740740
}
741741

742+
pub(crate) struct RustcInsignificantDtorParser;
743+
744+
impl<S: Stage> NoArgsAttributeParser<S> for RustcInsignificantDtorParser {
745+
const PATH: &[Symbol] = &[sym::rustc_insignificant_dtor];
746+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
747+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
748+
Allow(Target::Enum),
749+
Allow(Target::Struct),
750+
Allow(Target::ForeignTy),
751+
]);
752+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcInsignificantDtor;
753+
}
754+
742755
pub(crate) struct RustcEffectiveVisibilityParser;
743756

744757
impl<S: Stage> NoArgsAttributeParser<S> for RustcEffectiveVisibilityParser {
@@ -845,3 +858,18 @@ impl<S: Stage> NoArgsAttributeParser<S> for RustcIntrinsicConstStableIndirectPar
845858
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
846859
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcIntrinsicConstStableIndirect;
847860
}
861+
862+
pub(crate) struct RustcStrictCoherenceParser;
863+
864+
impl<S: Stage> NoArgsAttributeParser<S> for RustcStrictCoherenceParser {
865+
const PATH: &[Symbol] = &[sym::rustc_strict_coherence];
866+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
867+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
868+
Allow(Target::Trait),
869+
Allow(Target::Struct),
870+
Allow(Target::Enum),
871+
Allow(Target::Union),
872+
Allow(Target::ForeignTy),
873+
]);
874+
const CREATE: fn(Span) -> AttributeKind = AttributeKind::RustcStrictCoherence;
875+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ attribute_parsers!(
272272
Single<WithoutArgs<RustcEvaluateWhereClausesParser>>,
273273
Single<WithoutArgs<RustcHasIncoherentInherentImplsParser>>,
274274
Single<WithoutArgs<RustcHiddenTypeOfOpaquesParser>>,
275+
Single<WithoutArgs<RustcInsignificantDtorParser>>,
275276
Single<WithoutArgs<RustcIntrinsicConstStableIndirectParser>>,
276277
Single<WithoutArgs<RustcIntrinsicParser>>,
277278
Single<WithoutArgs<RustcLintOptTyParser>>,
@@ -289,6 +290,7 @@ attribute_parsers!(
289290
Single<WithoutArgs<RustcReallocatorParser>>,
290291
Single<WithoutArgs<RustcRegionsParser>>,
291292
Single<WithoutArgs<RustcShouldNotBeCalledOnConstItems>>,
293+
Single<WithoutArgs<RustcStrictCoherenceParser>>,
292294
Single<WithoutArgs<RustcVarianceOfOpaquesParser>>,
293295
Single<WithoutArgs<RustcVarianceParser>>,
294296
Single<WithoutArgs<SpecializationTraitParser>>,

compiler/rustc_feature/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,6 @@ pub use builtin_attrs::{
136136
};
137137
pub use removed::REMOVED_LANG_FEATURES;
138138
pub use unstable::{
139-
EnabledLangFeature, EnabledLibFeature, Features, INCOMPATIBLE_FEATURES, UNSTABLE_LANG_FEATURES,
139+
DEPENDENT_FEATURES, EnabledLangFeature, EnabledLibFeature, Features, INCOMPATIBLE_FEATURES,
140+
UNSTABLE_LANG_FEATURES,
140141
};

compiler/rustc_feature/src/unstable.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,8 @@ declare_features! (
605605
(unstable, offset_of_enum, "1.75.0", Some(120141)),
606606
/// Allows using fields with slice type in offset_of!
607607
(unstable, offset_of_slice, "1.81.0", Some(126151)),
608+
/// Allows using generics in more complex const expressions, based on definitional equality.
609+
(unstable, opaque_generic_const_args, "CURRENT_RUSTC_VERSION", Some(151972)),
608610
/// Allows using `#[optimize(X)]`.
609611
(unstable, optimize_attribute, "1.34.0", Some(54882)),
610612
/// Allows specifying nop padding on functions for dynamic patching.
@@ -782,3 +784,9 @@ pub const INCOMPATIBLE_FEATURES: &[(Symbol, Symbol)] = &[
782784
// boolean logic required to tell which typing rules to use.
783785
(sym::ref_pat_eat_one_layer_2024, sym::ref_pat_eat_one_layer_2024_structural),
784786
];
787+
788+
/// Some features require one or more other features to be enabled.
789+
pub const DEPENDENT_FEATURES: &[(Symbol, &[Symbol])] = &[
790+
(sym::opaque_generic_const_args, &[sym::min_generic_const_args]),
791+
(sym::unsized_const_params, &[sym::adt_const_params]),
792+
];

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,9 @@ pub enum AttributeKind {
11261126
/// Represents `#[rustc_if_this_changed]`
11271127
RustcIfThisChanged(Span, Option<Symbol>),
11281128

1129+
/// Represents `#[rustc_insignificant_dtor]`
1130+
RustcInsignificantDtor,
1131+
11291132
/// Represents `#[rustc_intrinsic]`
11301133
RustcIntrinsic,
11311134

@@ -1239,6 +1242,9 @@ pub enum AttributeKind {
12391242
/// Represents `#[rustc_std_internal_symbol]`.
12401243
RustcStdInternalSymbol(Span),
12411244

1245+
/// Represents `#[rustc_strict_coherence]`.
1246+
RustcStrictCoherence(Span),
1247+
12421248
/// Represents `#[rustc_symbol_name]`
12431249
RustcSymbolName(Span),
12441250

@@ -1275,6 +1281,7 @@ pub enum AttributeKind {
12751281
/// Span of the attribute.
12761282
span: Span,
12771283
},
1284+
12781285
/// Represents `#[target_feature(enable = "...")]` and
12791286
/// `#[unsafe(force_target_feature(enable = "...")]`.
12801287
TargetFeature { features: ThinVec<(Symbol, Span)>, attr_span: Span, was_forced: bool },

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ impl AttributeKind {
120120
RustcHasIncoherentInherentImpls => Yes,
121121
RustcHiddenTypeOfOpaques => No,
122122
RustcIfThisChanged(..) => No,
123+
RustcInsignificantDtor => Yes,
123124
RustcIntrinsic => Yes,
124125
RustcIntrinsicConstStableIndirect => No,
125126
RustcLayout(..) => No,
@@ -156,6 +157,7 @@ impl AttributeKind {
156157
RustcSkipDuringMethodDispatch { .. } => No,
157158
RustcSpecializationTrait(..) => No,
158159
RustcStdInternalSymbol(..) => No,
160+
RustcStrictCoherence(..) => Yes,
159161
RustcSymbolName(..) => Yes,
160162
RustcThenThisWouldNeed(..) => No,
161163
RustcUnsafeSpecializationMarker(..) => No,

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
1717
use std::cell::Cell;
1818
use std::iter;
19-
use std::ops::Bound;
19+
use std::ops::{Bound, ControlFlow};
2020

2121
use rustc_abi::{ExternAbi, Size};
2222
use rustc_ast::Recovered;
@@ -26,12 +26,13 @@ use rustc_errors::{
2626
Applicability, Diag, DiagCtxtHandle, E0228, ErrorGuaranteed, StashKey, struct_span_code_err,
2727
};
2828
use rustc_hir::attrs::AttributeKind;
29-
use rustc_hir::def::DefKind;
29+
use rustc_hir::def::{DefKind, Res};
3030
use rustc_hir::def_id::{DefId, LocalDefId};
31-
use rustc_hir::intravisit::{InferKind, Visitor, VisitorExt};
31+
use rustc_hir::intravisit::{self, InferKind, Visitor, VisitorExt};
3232
use rustc_hir::{self as hir, GenericParamKind, HirId, Node, PreciseCapturingArgKind, find_attr};
3333
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
3434
use rustc_infer::traits::{DynCompatibilityViolation, ObligationCause};
35+
use rustc_middle::hir::nested_filter;
3536
use rustc_middle::query::Providers;
3637
use rustc_middle::ty::util::{Discr, IntTypeExt};
3738
use rustc_middle::ty::{
@@ -1511,6 +1512,20 @@ fn anon_const_kind<'tcx>(tcx: TyCtxt<'tcx>, def: LocalDefId) -> ty::AnonConstKin
15111512
let parent_hir_node = tcx.hir_node(tcx.parent_hir_id(const_arg_id));
15121513
if tcx.features().generic_const_exprs() {
15131514
ty::AnonConstKind::GCE
1515+
} else if tcx.features().opaque_generic_const_args() {
1516+
// Only anon consts that are the RHS of a const item can be OGCA.
1517+
// Note: We can't just check tcx.parent because it needs to be EXACTLY
1518+
// the RHS, not just part of the RHS.
1519+
if !is_anon_const_rhs_of_const_item(tcx, def) {
1520+
return ty::AnonConstKind::MCG;
1521+
}
1522+
1523+
let body = tcx.hir_body_owned_by(def);
1524+
let mut visitor = OGCAParamVisitor(tcx);
1525+
match visitor.visit_body(body) {
1526+
ControlFlow::Break(UsesParam) => ty::AnonConstKind::OGCA,
1527+
ControlFlow::Continue(()) => ty::AnonConstKind::MCG,
1528+
}
15141529
} else if tcx.features().min_generic_const_args() {
15151530
ty::AnonConstKind::MCG
15161531
} else if let hir::Node::Expr(hir::Expr {
@@ -1528,6 +1543,49 @@ fn anon_const_kind<'tcx>(tcx: TyCtxt<'tcx>, def: LocalDefId) -> ty::AnonConstKin
15281543
}
15291544
}
15301545

1546+
fn is_anon_const_rhs_of_const_item<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> bool {
1547+
let hir_id = tcx.local_def_id_to_hir_id(def_id);
1548+
let Some((_, grandparent_node)) = tcx.hir_parent_iter(hir_id).nth(1) else { return false };
1549+
let (Node::Item(hir::Item { kind: hir::ItemKind::Const(_, _, _, ct_rhs), .. })
1550+
| Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(_, ct_rhs), .. })
1551+
| Node::TraitItem(hir::TraitItem {
1552+
kind: hir::TraitItemKind::Const(_, Some(ct_rhs)), ..
1553+
})) = grandparent_node
1554+
else {
1555+
return false;
1556+
};
1557+
let hir::ConstItemRhs::TypeConst(hir::ConstArg {
1558+
kind: hir::ConstArgKind::Anon(rhs_anon), ..
1559+
}) = ct_rhs
1560+
else {
1561+
return false;
1562+
};
1563+
def_id == rhs_anon.def_id
1564+
}
1565+
1566+
struct OGCAParamVisitor<'tcx>(TyCtxt<'tcx>);
1567+
1568+
struct UsesParam;
1569+
1570+
impl<'tcx> Visitor<'tcx> for OGCAParamVisitor<'tcx> {
1571+
type NestedFilter = nested_filter::OnlyBodies;
1572+
type Result = ControlFlow<UsesParam>;
1573+
1574+
fn maybe_tcx(&mut self) -> TyCtxt<'tcx> {
1575+
self.0
1576+
}
1577+
1578+
fn visit_path(&mut self, path: &hir::Path<'tcx>, _id: HirId) -> ControlFlow<UsesParam> {
1579+
if let Res::Def(DefKind::TyParam | DefKind::ConstParam | DefKind::LifetimeParam, _) =
1580+
path.res
1581+
{
1582+
return ControlFlow::Break(UsesParam);
1583+
}
1584+
1585+
intravisit::walk_path(self, path)
1586+
}
1587+
}
1588+
15311589
#[instrument(level = "debug", skip(tcx), ret)]
15321590
fn const_of_item<'tcx>(
15331591
tcx: TyCtxt<'tcx>,

0 commit comments

Comments
 (0)