Skip to content

Commit 87b1f29

Browse files
committed
Begin to use ConstArgKind::Path for all paths, not just params
Note that these test output changes are not desired.
1 parent c87004a commit 87b1f29

10 files changed

+105
-142
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+10-47
Original file line numberDiff line numberDiff line change
@@ -2357,53 +2357,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23572357
ty_id: NodeId,
23582358
span: Span,
23592359
) -> &'hir hir::ConstArg<'hir> {
2360-
let ct_kind = match res {
2361-
Res::Def(DefKind::ConstParam, _) => {
2362-
let qpath = self.lower_qpath(
2363-
ty_id,
2364-
&None,
2365-
path,
2366-
ParamMode::Optional,
2367-
AllowReturnTypeNotation::No,
2368-
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
2369-
None,
2370-
);
2371-
hir::ConstArgKind::Path(qpath)
2372-
}
2373-
_ => {
2374-
// Construct an AnonConst where the expr is the "ty"'s path.
2375-
2376-
let parent_def_id = self.current_def_id_parent;
2377-
let node_id = self.next_node_id();
2378-
let span = self.lower_span(span);
2379-
2380-
// Add a definition for the in-band const def.
2381-
let def_id =
2382-
self.create_def(parent_def_id, node_id, kw::Empty, DefKind::AnonConst, span);
2383-
let hir_id = self.lower_node_id(node_id);
2384-
2385-
let path_expr = Expr {
2386-
id: ty_id,
2387-
kind: ExprKind::Path(None, path.clone()),
2388-
span,
2389-
attrs: AttrVec::new(),
2390-
tokens: None,
2391-
};
2392-
2393-
let ct = self.with_new_scopes(span, |this| {
2394-
self.arena.alloc(hir::AnonConst {
2395-
def_id,
2396-
hir_id,
2397-
body: this.with_def_id_parent(def_id, |this| {
2398-
this.lower_const_body(path_expr.span, Some(&path_expr))
2399-
}),
2400-
span,
2401-
})
2402-
});
2403-
hir::ConstArgKind::Anon(ct)
2404-
}
2405-
};
2406-
2360+
let qpath = self.lower_qpath(
2361+
ty_id,
2362+
&None,
2363+
path,
2364+
ParamMode::Optional,
2365+
AllowReturnTypeNotation::No,
2366+
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
2367+
None,
2368+
);
2369+
let ct_kind = hir::ConstArgKind::Path(qpath);
24072370
self.arena.alloc(hir::ConstArg {
24082371
hir_id: self.next_id(),
24092372
kind: ct_kind,

compiler/rustc_middle/src/ty/consts.rs

+35-24
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,7 @@ impl<'tcx> Const<'tcx> {
215215
}
216216

217217
match const_arg.kind {
218-
hir::ConstArgKind::Path(qpath) => {
219-
// FIXME(min_generic_const_args): for now only params are lowered to ConstArgKind::Path
220-
Self::from_param(tcx, qpath, const_arg.hir_id)
221-
}
218+
hir::ConstArgKind::Path(qpath) => Self::from_path(tcx, qpath, const_arg.hir_id),
222219
hir::ConstArgKind::Anon(anon) => Self::from_anon_const(tcx, anon.def_id),
223220
}
224221
}
@@ -240,7 +237,7 @@ impl<'tcx> Const<'tcx> {
240237

241238
let ty = tcx.type_of(def).no_bound_vars().expect("const parameter types cannot be generic");
242239

243-
match Self::try_from_lit_or_param(tcx, ty, expr) {
240+
match Self::try_from_lit(tcx, ty, expr) {
244241
Some(v) => v,
245242
None => ty::Const::new_unevaluated(tcx, ty::UnevaluatedConst {
246243
def: def.to_def_id(),
@@ -249,40 +246,54 @@ impl<'tcx> Const<'tcx> {
249246
}
250247
}
251248

252-
/// Lower a const param to a [`Const`].
253-
///
254-
/// IMPORTANT: `qpath` must be a const param, otherwise this will panic
255-
fn from_param(tcx: TyCtxt<'tcx>, qpath: hir::QPath<'tcx>, hir_id: HirId) -> Self {
256-
let hir::QPath::Resolved(_, &hir::Path { res: Res::Def(DefKind::ConstParam, def_id), .. }) =
257-
qpath
258-
else {
259-
span_bug!(qpath.span(), "non-param {qpath:?} passed to Const::from_param")
260-
};
249+
fn from_path(tcx: TyCtxt<'tcx>, qpath: hir::QPath<'tcx>, hir_id: HirId) -> Self {
250+
match qpath {
251+
hir::QPath::Resolved(_, &path) => match path.res {
252+
Res::Def(DefKind::ConstParam, def_id) => Self::from_param(tcx, def_id, hir_id),
253+
Res::Def(DefKind::Const, def_id) => {
254+
// FIXME: support parametrized consts (instead of empty list).
255+
let uv = ty::UnevaluatedConst::new(def_id, ty::List::empty());
256+
Self::new_unevaluated(tcx, uv)
257+
}
258+
r => Self::new_error(
259+
tcx,
260+
tcx.dcx().span_delayed_bug(
261+
tcx.hir().span(hir_id),
262+
format!("Const::from_path: invalid qpath res: {r:?}"),
263+
),
264+
),
265+
},
266+
hir::QPath::TypeRelative(..) => todo!("associated const paths"),
267+
// FIXME: use span_delayed_bug?
268+
hir::QPath::LangItem(..) => span_bug!(
269+
tcx.hir().span(hir_id),
270+
"Const::from_path: invalid qpath provided: {qpath:?}"
271+
),
272+
}
273+
}
261274

262-
match tcx.named_bound_var(hir_id) {
275+
/// Lower a const param to a [`Const`]. This is only meant as a helper for [`Self::from_path`].
276+
fn from_param(tcx: TyCtxt<'tcx>, param_def_id: DefId, path_hir_id: HirId) -> Self {
277+
match tcx.named_bound_var(path_hir_id) {
263278
Some(rbv::ResolvedArg::EarlyBound(_)) => {
264279
// Find the name and index of the const parameter by indexing the generics of
265280
// the parent item and construct a `ParamConst`.
266-
let item_def_id = tcx.parent(def_id);
281+
let item_def_id = tcx.parent(param_def_id);
267282
let generics = tcx.generics_of(item_def_id);
268-
let index = generics.param_def_id_to_index[&def_id];
269-
let name = tcx.item_name(def_id);
283+
let index = generics.param_def_id_to_index[&param_def_id];
284+
let name = tcx.item_name(param_def_id);
270285
ty::Const::new_param(tcx, ty::ParamConst::new(index, name))
271286
}
272287
Some(rbv::ResolvedArg::LateBound(debruijn, index, _)) => {
273288
ty::Const::new_bound(tcx, debruijn, ty::BoundVar::from_u32(index))
274289
}
275290
Some(rbv::ResolvedArg::Error(guar)) => ty::Const::new_error(tcx, guar),
276-
arg => bug!("unexpected bound var resolution for {:?}: {arg:?}", hir_id),
291+
arg => bug!("unexpected bound var resolution for {:?}: {arg:?}", path_hir_id),
277292
}
278293
}
279294

280295
#[instrument(skip(tcx), level = "debug")]
281-
fn try_from_lit_or_param(
282-
tcx: TyCtxt<'tcx>,
283-
ty: Ty<'tcx>,
284-
expr: &'tcx hir::Expr<'tcx>,
285-
) -> Option<Self> {
296+
fn try_from_lit(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, expr: &'tcx hir::Expr<'tcx>) -> Option<Self> {
286297
// Unwrap a block, so that e.g. `{ P }` is recognised as a parameter. Const arguments
287298
// currently have to be wrapped in curly brackets, so it's necessary to special-case.
288299
let expr = match &expr.kind {

tests/ui/const-generics/fn-const-param-infer.adt_const_params.stderr

+3-14
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,7 @@ LL | let _ = Checked::<{ generic_arg::<u32> }>;
1313
= note: expected fn pointer `fn(usize) -> _`
1414
found fn item `fn(u32) -> _ {generic_arg::<u32>}`
1515

16-
error[E0282]: type annotations needed
17-
--> $DIR/fn-const-param-infer.rs:35:23
18-
|
19-
LL | let _ = Checked::<generic>;
20-
| ^^^^^^^ cannot infer type of the type parameter `T` declared on the function `generic`
21-
|
22-
help: consider specifying the generic argument
23-
|
24-
LL | let _ = Checked::<generic::<T>>;
25-
| +++++
26-
27-
error: aborting due to 3 previous errors
16+
error: aborting due to 2 previous errors
2817

29-
Some errors have detailed explanations: E0282, E0308, E0741.
30-
For more information about an error, try `rustc --explain E0282`.
18+
Some errors have detailed explanations: E0308, E0741.
19+
For more information about an error, try `rustc --explain E0308`.

tests/ui/const-generics/fn-const-param-infer.full.stderr

+3-14
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,7 @@ LL | let _ = Checked::<{ generic_arg::<u32> }>;
1313
= note: expected fn pointer `fn(usize) -> _`
1414
found fn item `fn(u32) -> _ {generic_arg::<u32>}`
1515

16-
error[E0282]: type annotations needed
17-
--> $DIR/fn-const-param-infer.rs:35:23
18-
|
19-
LL | let _ = Checked::<generic>;
20-
| ^^^^^^^ cannot infer type of the type parameter `T` declared on the function `generic`
21-
|
22-
help: consider specifying the generic argument
23-
|
24-
LL | let _ = Checked::<generic::<T>>;
25-
| +++++
26-
27-
error: aborting due to 3 previous errors
16+
error: aborting due to 2 previous errors
2817

29-
Some errors have detailed explanations: E0282, E0308, E0741.
30-
For more information about an error, try `rustc --explain E0282`.
18+
Some errors have detailed explanations: E0308, E0741.
19+
For more information about an error, try `rustc --explain E0308`.

tests/ui/const-generics/fn-const-param-infer.min.stderr

+2-14
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,6 @@ LL | let _ = Checked::<{ generic_arg::<u32> }>;
1515
= note: expected fn pointer `fn(usize) -> _`
1616
found fn item `fn(u32) -> _ {generic_arg::<u32>}`
1717

18-
error[E0282]: type annotations needed
19-
--> $DIR/fn-const-param-infer.rs:35:23
20-
|
21-
LL | let _ = Checked::<generic>;
22-
| ^^^^^^^ cannot infer type of the type parameter `T` declared on the function `generic`
23-
|
24-
help: consider specifying the generic argument
25-
|
26-
LL | let _ = Checked::<generic::<T>>;
27-
| +++++
28-
29-
error: aborting due to 3 previous errors
18+
error: aborting due to 2 previous errors
3019

31-
Some errors have detailed explanations: E0282, E0308.
32-
For more information about an error, try `rustc --explain E0282`.
20+
For more information about this error, try `rustc --explain E0308`.

tests/ui/const-generics/generic_const_exprs/unevaluated-const-ice-119731.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,17 @@ help: add `#![feature(adt_const_params)]` to the crate attributes to enable more
7272
LL + #![feature(adt_const_params)]
7373
|
7474

75-
error: maximum number of nodes exceeded in constant v20::v17::<v10, v2>::{constant#0}
76-
--> $DIR/unevaluated-const-ice-119731.rs:27:37
75+
error: maximum number of nodes exceeded in constant v20::v2
76+
--> $DIR/unevaluated-const-ice-119731.rs:11:5
7777
|
78-
LL | impl<const v10: usize> v17<v10, v2> {
79-
| ^^
78+
LL | const v2: v11 = [[256; v4]; v4];
79+
| ^^^^^^^^^^^^^
8080

81-
error: maximum number of nodes exceeded in constant v20::v17::<v10, v2>::{constant#0}
82-
--> $DIR/unevaluated-const-ice-119731.rs:27:37
81+
error: maximum number of nodes exceeded in constant v20::v2
82+
--> $DIR/unevaluated-const-ice-119731.rs:11:5
8383
|
84-
LL | impl<const v10: usize> v17<v10, v2> {
85-
| ^^
84+
LL | const v2: v11 = [[256; v4]; v4];
85+
| ^^^^^^^^^^^^^
8686
|
8787
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
8888

+44-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,50 @@
1-
error[E0308]: mismatched types
2-
--> $DIR/opaque_types2.rs:13:11
1+
error[E0391]: cycle detected when computing type of `Foo::{opaque#0}`
2+
--> $DIR/opaque_types2.rs:3:12
33
|
44
LL | type Foo = impl Sized;
5-
| ---------- the found opaque type
6-
...
7-
LL | foo::<C>();
8-
| ^ expected `u32`, found opaque type
5+
| ^^^^^^^^^^
96
|
10-
= note: expected type `u32`
11-
found opaque type `Foo`
7+
note: ...which requires computing type of opaque `Foo::{opaque#0}`...
8+
--> $DIR/opaque_types2.rs:3:12
9+
|
10+
LL | type Foo = impl Sized;
11+
| ^^^^^^^^^^
12+
note: ...which requires type-checking `bar`...
13+
--> $DIR/opaque_types2.rs:9:1
14+
|
15+
LL | / fn bar()
16+
LL | | where
17+
LL | | Foo:,
18+
| |_________^
19+
note: ...which requires evaluating type-level constant...
20+
--> $DIR/opaque_types2.rs:7:1
21+
|
22+
LL | const C: Foo = 42;
23+
| ^^^^^^^^^^^^
24+
note: ...which requires const-evaluating + checking `C`...
25+
--> $DIR/opaque_types2.rs:7:1
26+
|
27+
LL | const C: Foo = 42;
28+
| ^^^^^^^^^^^^
29+
note: ...which requires caching mir of `C` for CTFE...
30+
--> $DIR/opaque_types2.rs:7:1
31+
|
32+
LL | const C: Foo = 42;
33+
| ^^^^^^^^^^^^
34+
note: ...which requires elaborating drops for `C`...
35+
--> $DIR/opaque_types2.rs:7:1
36+
|
37+
LL | const C: Foo = 42;
38+
| ^^^^^^^^^^^^
39+
= note: ...which requires normalizing `Foo`...
40+
= note: ...which again requires computing type of `Foo::{opaque#0}`, completing the cycle
41+
note: cycle used when checking that `Foo::{opaque#0}` is well-formed
42+
--> $DIR/opaque_types2.rs:3:12
43+
|
44+
LL | type Foo = impl Sized;
45+
| ^^^^^^^^^^
46+
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
1247

1348
error: aborting due to 1 previous error
1449

15-
For more information about this error, try `rustc --explain E0308`.
50+
For more information about this error, try `rustc --explain E0391`.

tests/ui/generics/generic-function-item-where-type.stderr

-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ error[E0747]: constant provided when a type was expected
33
|
44
LL | foo::<main>()
55
| ^^^^
6-
|
7-
= help: `main` is a function item, not a type
8-
= help: function item types cannot be named directly
96

107
error: aborting due to 1 previous error
118

tests/ui/privacy/privacy-ns1.stderr

-3
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ error[E0747]: constant provided when a type was expected
5757
|
5858
LL | let _x: Box<Bar>;
5959
| ^^^
60-
|
61-
= help: `Bar` is a function item, not a type
62-
= help: function item types cannot be named directly
6360

6461
error: aborting due to 4 previous errors
6562

tests/ui/privacy/privacy-ns2.stderr

-6
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,12 @@ error[E0747]: constant provided when a type was expected
8383
|
8484
LL | let _x : Box<Bar>;
8585
| ^^^
86-
|
87-
= help: `Bar` is a function item, not a type
88-
= help: function item types cannot be named directly
8986

9087
error[E0747]: constant provided when a type was expected
9188
--> $DIR/privacy-ns2.rs:48:17
9289
|
9390
LL | let _x: Box<Bar>;
9491
| ^^^
95-
|
96-
= help: `Bar` is a function item, not a type
97-
= help: function item types cannot be named directly
9892

9993
error: aborting due to 8 previous errors
10094

0 commit comments

Comments
 (0)