Skip to content

Commit 7899368

Browse files
committed
Auto merge of #133205 - matthiaskrgr:rollup-xhhhp5u, r=matthiaskrgr
Rollup of 4 pull requests Successful merges: - #131081 (Use `ConstArgKind::Path` for all single-segment paths, not just params under `min_generic_const_args`) - #132577 (Report the `unexpected_cfgs` lint in external macros) - #133023 (Merge `-Zhir-stats` into `-Zinput-stats`) - #133200 (ignore an occasionally-failing test in Miri) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 89b6885 + 9aac15d commit 7899368

File tree

39 files changed

+435
-369
lines changed

39 files changed

+435
-369
lines changed

Cargo.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ dependencies = [
111111

112112
[[package]]
113113
name = "anstream"
114-
version = "0.6.17"
114+
version = "0.6.18"
115115
source = "registry+https://github.com/rust-lang/crates.io-index"
116-
checksum = "23a1e53f0f5d86382dafe1cf314783b2044280f406e7e1506368220ad11b1338"
116+
checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b"
117117
dependencies = [
118118
"anstyle",
119119
"anstyle-parse",

compiler/rustc_ast_lowering/src/lib.rs

+60-48
Original file line numberDiff line numberDiff line change
@@ -2052,6 +2052,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20522052
}
20532053
}
20542054

2055+
/// Used when lowering a type argument that turned out to actually be a const argument.
2056+
///
2057+
/// Only use for that purpose since otherwise it will create a duplicate def.
20552058
#[instrument(level = "debug", skip(self))]
20562059
fn lower_const_path_to_const_arg(
20572060
&mut self,
@@ -2060,51 +2063,58 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20602063
ty_id: NodeId,
20612064
span: Span,
20622065
) -> &'hir hir::ConstArg<'hir> {
2063-
let ct_kind = match res {
2064-
Res::Def(DefKind::ConstParam, _) => {
2065-
let qpath = self.lower_qpath(
2066-
ty_id,
2067-
&None,
2068-
path,
2069-
ParamMode::Optional,
2070-
AllowReturnTypeNotation::No,
2071-
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
2072-
None,
2073-
);
2074-
hir::ConstArgKind::Path(qpath)
2075-
}
2076-
_ => {
2077-
// Construct an AnonConst where the expr is the "ty"'s path.
2066+
let tcx = self.tcx;
20782067

2079-
let parent_def_id = self.current_def_id_parent;
2080-
let node_id = self.next_node_id();
2081-
let span = self.lower_span(span);
2082-
2083-
// Add a definition for the in-band const def.
2084-
let def_id =
2085-
self.create_def(parent_def_id, node_id, kw::Empty, DefKind::AnonConst, span);
2086-
let hir_id = self.lower_node_id(node_id);
2068+
// FIXME(min_generic_const_args): we only allow one-segment const paths for now
2069+
let ct_kind = if path.is_potential_trivial_const_arg()
2070+
&& (tcx.features().min_generic_const_args()
2071+
|| matches!(res, Res::Def(DefKind::ConstParam, _)))
2072+
{
2073+
let qpath = self.lower_qpath(
2074+
ty_id,
2075+
&None,
2076+
path,
2077+
ParamMode::Optional,
2078+
AllowReturnTypeNotation::No,
2079+
// FIXME(min_generic_const_args): update for `fn foo() -> Bar<FOO<impl Trait>>` support
2080+
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
2081+
None,
2082+
);
2083+
hir::ConstArgKind::Path(qpath)
2084+
} else {
2085+
// Construct an AnonConst where the expr is the "ty"'s path.
2086+
2087+
let parent_def_id = self.current_def_id_parent;
2088+
let node_id = self.next_node_id();
2089+
let span = self.lower_span(span);
2090+
2091+
// Add a definition for the in-band const def.
2092+
// We're lowering a const argument that was originally thought to be a type argument,
2093+
// so the def collector didn't create the def ahead of time. That's why we have to do
2094+
// it here.
2095+
let def_id =
2096+
self.create_def(parent_def_id, node_id, kw::Empty, DefKind::AnonConst, span);
2097+
let hir_id = self.lower_node_id(node_id);
2098+
2099+
let path_expr = Expr {
2100+
id: ty_id,
2101+
kind: ExprKind::Path(None, path.clone()),
2102+
span,
2103+
attrs: AttrVec::new(),
2104+
tokens: None,
2105+
};
20872106

2088-
let path_expr = Expr {
2089-
id: ty_id,
2090-
kind: ExprKind::Path(None, path.clone()),
2107+
let ct = self.with_new_scopes(span, |this| {
2108+
self.arena.alloc(hir::AnonConst {
2109+
def_id,
2110+
hir_id,
2111+
body: this.with_def_id_parent(def_id, |this| {
2112+
this.lower_const_body(path_expr.span, Some(&path_expr))
2113+
}),
20912114
span,
2092-
attrs: AttrVec::new(),
2093-
tokens: None,
2094-
};
2095-
2096-
let ct = self.with_new_scopes(span, |this| {
2097-
self.arena.alloc(hir::AnonConst {
2098-
def_id,
2099-
hir_id,
2100-
body: this.with_def_id_parent(def_id, |this| {
2101-
this.lower_const_body(path_expr.span, Some(&path_expr))
2102-
}),
2103-
span,
2104-
})
2105-
});
2106-
hir::ConstArgKind::Anon(ct)
2107-
}
2115+
})
2116+
});
2117+
hir::ConstArgKind::Anon(ct)
21082118
};
21092119

21102120
self.arena.alloc(hir::ConstArg {
@@ -2122,6 +2132,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21222132

21232133
#[instrument(level = "debug", skip(self))]
21242134
fn lower_anon_const_to_const_arg_direct(&mut self, anon: &AnonConst) -> hir::ConstArg<'hir> {
2135+
let tcx = self.tcx;
21252136
// Unwrap a block, so that e.g. `{ P }` is recognised as a parameter. Const arguments
21262137
// currently have to be wrapped in curly brackets, so it's necessary to special-case.
21272138
let expr = if let ExprKind::Block(block, _) = &anon.value.kind
@@ -2135,18 +2146,19 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21352146
};
21362147
let maybe_res =
21372148
self.resolver.get_partial_res(expr.id).and_then(|partial_res| partial_res.full_res());
2138-
debug!("res={:?}", maybe_res);
2139-
// FIXME(min_generic_const_args): for now we only lower params to ConstArgKind::Path
2140-
if let Some(res) = maybe_res
2141-
&& let Res::Def(DefKind::ConstParam, _) = res
2142-
&& let ExprKind::Path(qself, path) = &expr.kind
2149+
// FIXME(min_generic_const_args): we only allow one-segment const paths for now
2150+
if let ExprKind::Path(None, path) = &expr.kind
2151+
&& path.is_potential_trivial_const_arg()
2152+
&& (tcx.features().min_generic_const_args()
2153+
|| matches!(maybe_res, Some(Res::Def(DefKind::ConstParam, _))))
21432154
{
21442155
let qpath = self.lower_qpath(
21452156
expr.id,
2146-
qself,
2157+
&None,
21472158
path,
21482159
ParamMode::Optional,
21492160
AllowReturnTypeNotation::No,
2161+
// FIXME(min_generic_const_args): update for `fn foo() -> Bar<FOO<impl Trait>>` support
21502162
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
21512163
None,
21522164
);

compiler/rustc_ast_passes/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
pub mod ast_validation;
1919
mod errors;
2020
pub mod feature_gate;
21-
pub mod node_count;
2221
pub mod show_span;
2322

2423
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }

compiler/rustc_ast_passes/src/node_count.rs

-129
This file was deleted.

compiler/rustc_feature/src/unstable.rs

+2
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,8 @@ declare_features! (
529529
(unstable, macro_metavar_expr_concat, "1.81.0", Some(124225)),
530530
/// Allows `#[marker]` on certain traits allowing overlapping implementations.
531531
(unstable, marker_trait_attr, "1.30.0", Some(29864)),
532+
/// Enables the generic const args MVP (only bare paths, not arbitrary computation).
533+
(incomplete, min_generic_const_args, "CURRENT_RUSTC_VERSION", Some(132980)),
532534
/// A minimal, sound subset of specialization intended to be used by the
533535
/// standard library until the soundness issues with specialization
534536
/// are fixed.

compiler/rustc_hir_analysis/src/collect.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use tracing::{debug, instrument};
4646

4747
use crate::check::intrinsic::intrinsic_operation_unsafety;
4848
use crate::errors;
49-
use crate::hir_ty_lowering::{HirTyLowerer, RegionInferReason};
49+
use crate::hir_ty_lowering::{FeedConstTy, HirTyLowerer, RegionInferReason};
5050

5151
pub(crate) mod dump;
5252
mod generics_of;
@@ -88,6 +88,7 @@ pub fn provide(providers: &mut Providers) {
8888
coroutine_for_closure,
8989
opaque_ty_origin,
9090
rendered_precise_capturing_args,
91+
const_param_default,
9192
..*providers
9293
};
9394
}
@@ -1790,3 +1791,23 @@ fn rendered_precise_capturing_args<'tcx>(
17901791
_ => None,
17911792
})
17921793
}
1794+
1795+
fn const_param_default<'tcx>(
1796+
tcx: TyCtxt<'tcx>,
1797+
def_id: LocalDefId,
1798+
) -> ty::EarlyBinder<'tcx, Const<'tcx>> {
1799+
let default_ct = match tcx.hir_node_by_def_id(def_id) {
1800+
hir::Node::GenericParam(hir::GenericParam {
1801+
kind: hir::GenericParamKind::Const { default: Some(ct), .. },
1802+
..
1803+
}) => ct,
1804+
_ => span_bug!(
1805+
tcx.def_span(def_id),
1806+
"`const_param_default` expected a generic parameter with a constant"
1807+
),
1808+
};
1809+
let icx = ItemCtxt::new(tcx, def_id);
1810+
// FIXME(const_generics): investigate which places do and don't need const ty feeding
1811+
let ct = icx.lowerer().lower_const_arg(default_ct, FeedConstTy::No);
1812+
ty::EarlyBinder::bind(ct)
1813+
}

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,12 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
223223
trace!(?predicates);
224224
}
225225
hir::GenericParamKind::Const { .. } => {
226+
let param_def_id = param.def_id.to_def_id();
226227
let ct_ty = tcx
227-
.type_of(param.def_id.to_def_id())
228+
.type_of(param_def_id)
228229
.no_bound_vars()
229230
.expect("const parameters cannot be generic");
230-
let ct = icx.lowerer().lower_const_param(param.hir_id);
231+
let ct = icx.lowerer().lower_const_param(param_def_id, param.hir_id);
231232
predicates
232233
.insert((ty::ClauseKind::ConstArgHasType(ct, ct_ty).upcast(tcx), param.span));
233234
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ use tracing::{debug, instrument};
1919
use super::errors::GenericsArgsErrExtend;
2020
use crate::bounds::Bounds;
2121
use crate::errors;
22-
use crate::hir_ty_lowering::{AssocItemQSelf, HirTyLowerer, PredicateFilter, RegionInferReason};
22+
use crate::hir_ty_lowering::{
23+
AssocItemQSelf, FeedConstTy, HirTyLowerer, PredicateFilter, RegionInferReason,
24+
};
2325

2426
impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
2527
/// Add a `Sized` bound to the `bounds` if appropriate.
@@ -346,9 +348,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
346348
hir::AssocItemConstraintKind::Equality { term } => {
347349
let term = match term {
348350
hir::Term::Ty(ty) => self.lower_ty(ty).into(),
349-
hir::Term::Const(ct) => {
350-
ty::Const::from_const_arg(tcx, ct, ty::FeedConstTy::No).into()
351-
}
351+
hir::Term::Const(ct) => self.lower_const_arg(ct, FeedConstTy::No).into(),
352352
};
353353

354354
// Find any late-bound regions declared in `ty` that are not

0 commit comments

Comments
 (0)