Skip to content

Commit 89f1192

Browse files
committed
Begin to use ConstArgKind::Path for all paths, not just params
The test error changes are undesired and need to be investigated.
1 parent ee1adb3 commit 89f1192

File tree

8 files changed

+128
-132
lines changed

8 files changed

+128
-132
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2060,56 +2060,19 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20602060
ty_id: NodeId,
20612061
span: Span,
20622062
) -> &'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.
2078-
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);
2087-
2088-
let path_expr = Expr {
2089-
id: ty_id,
2090-
kind: ExprKind::Path(None, path.clone()),
2091-
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-
}
2108-
};
2063+
let qpath = self.lower_qpath(
2064+
ty_id,
2065+
&None,
2066+
path,
2067+
ParamMode::Optional,
2068+
AllowReturnTypeNotation::No,
2069+
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
2070+
None,
2071+
);
21092072

21102073
self.arena.alloc(hir::ConstArg {
21112074
hir_id: self.next_id(),
2112-
kind: ct_kind,
2075+
kind: hir::ConstArgKind::Path(qpath),
21132076
is_desugared_from_effects: false,
21142077
})
21152078
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,12 @@ fn generic_arg_mismatch_err(
115115
}
116116
}
117117
(GenericArg::Const(cnst), GenericParamDefKind::Type { .. }) => {
118-
// FIXME(min_generic_const_args): once ConstArgKind::Path is used for non-params too,
119-
// this should match against that instead of ::Anon
120-
if let hir::ConstArgKind::Anon(anon) = cnst.kind
121-
&& let body = tcx.hir().body(anon.body)
122-
&& let rustc_hir::ExprKind::Path(rustc_hir::QPath::Resolved(_, path)) =
123-
body.value.kind
118+
if let hir::ConstArgKind::Path(qpath) = cnst.kind
119+
&& let rustc_hir::QPath::Resolved(_, path) = qpath
120+
&& let Res::Def(DefKind::Fn { .. }, id) = path.res
124121
{
125-
if let Res::Def(DefKind::Fn { .. }, id) = path.res {
126-
err.help(format!("`{}` is a function item, not a type", tcx.item_name(id)));
127-
err.help("function item types cannot be named directly");
128-
}
122+
err.help(format!("`{}` is a function item, not a type", tcx.item_name(id)));
123+
err.help("function item types cannot be named directly");
129124
}
130125
}
131126
_ => {}

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2054,41 +2054,62 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
20542054
}
20552055

20562056
match const_arg.kind {
2057-
hir::ConstArgKind::Path(qpath) => {
2058-
// FIXME(min_generic_const_args): for now only params are lowered to ConstArgKind::Path
2059-
self.lower_const_arg_param(qpath, const_arg.hir_id)
2060-
}
2057+
hir::ConstArgKind::Path(qpath) => self.lower_const_arg_path(qpath, const_arg.hir_id),
20612058
hir::ConstArgKind::Anon(anon) => Const::from_anon_const(tcx, anon.def_id),
20622059
}
20632060
}
20642061

2065-
/// Lower a use of a const param to a [`Const`].
2066-
///
2067-
/// IMPORTANT: `qpath` must be a const param, otherwise this will panic
2068-
fn lower_const_arg_param(&self, qpath: hir::QPath<'tcx>, hir_id: HirId) -> Const<'tcx> {
2062+
/// Lower a const path to a [`Const`].
2063+
fn lower_const_arg_path(&self, qpath: hir::QPath<'tcx>, hir_id: HirId) -> Const<'tcx> {
20692064
let tcx = self.tcx();
20702065

2071-
let hir::QPath::Resolved(_, &hir::Path { res: Res::Def(DefKind::ConstParam, def_id), .. }) =
2072-
qpath
2073-
else {
2074-
span_bug!(qpath.span(), "non-param {qpath:?} passed to Const::from_param")
2075-
};
2066+
// TODO: handle path args properly
2067+
match qpath {
2068+
hir::QPath::Resolved(_, &hir::Path { res: Res::Def(DefKind::ConstParam, did), .. }) => {
2069+
self.lower_const_arg_param(did, hir_id)
2070+
}
2071+
hir::QPath::Resolved(
2072+
_,
2073+
&hir::Path { res: Res::Def(DefKind::Fn | DefKind::AssocFn, _), .. },
2074+
) => ty::Const::new_error_with_message(
2075+
tcx,
2076+
qpath.span(),
2077+
"fn's cannot be used as const args",
2078+
),
2079+
hir::QPath::Resolved(_, path @ &hir::Path { res: Res::Def(_, did), .. }) => {
2080+
let (item_segment, _) = path.segments.split_last().unwrap();
2081+
let args = self.lower_generic_args_of_path_segment(path.span, did, item_segment);
2082+
ty::Const::new_unevaluated(tcx, ty::UnevaluatedConst::new(did, args))
2083+
}
2084+
// TODO: type-relative paths
2085+
_ => ty::Const::new_error_with_message(
2086+
tcx,
2087+
qpath.span(),
2088+
"Const::lower_const_arg_path: invalid qpath",
2089+
),
2090+
}
2091+
}
20762092

2077-
match tcx.named_bound_var(hir_id) {
2093+
/// Lower a const param to a [`Const`]. This is only meant as a helper for [`Self::lower_const_arg_path`].
2094+
/// FIXME: dedup with lower_const_param
2095+
fn lower_const_arg_param(&self, param_def_id: DefId, path_hir_id: HirId) -> Const<'tcx> {
2096+
let tcx = self.tcx();
2097+
2098+
match tcx.named_bound_var(path_hir_id) {
20782099
Some(rbv::ResolvedArg::EarlyBound(_)) => {
20792100
// Find the name and index of the const parameter by indexing the generics of
20802101
// the parent item and construct a `ParamConst`.
2081-
let item_def_id = tcx.parent(def_id);
2102+
let item_def_id = tcx.parent(param_def_id);
20822103
let generics = tcx.generics_of(item_def_id);
2083-
let index = generics.param_def_id_to_index[&def_id];
2084-
let name = tcx.item_name(def_id);
2104+
let index = generics.param_def_id_to_index[&param_def_id];
2105+
let name = tcx.item_name(param_def_id);
20852106
ty::Const::new_param(tcx, ty::ParamConst::new(index, name))
20862107
}
20872108
Some(rbv::ResolvedArg::LateBound(debruijn, index, _)) => {
20882109
ty::Const::new_bound(tcx, debruijn, ty::BoundVar::from_u32(index))
20892110
}
20902111
Some(rbv::ResolvedArg::Error(guar)) => ty::Const::new_error(tcx, guar),
2091-
arg => bug!("unexpected bound var resolution for {:?}: {arg:?}", hir_id),
2112+
arg => bug!("unexpected bound var resolution for {:?}: {arg:?}", path_hir_id),
20922113
}
20932114
}
20942115

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

Lines changed: 3 additions & 14 deletions
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

Lines changed: 3 additions & 14 deletions
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

Lines changed: 2 additions & 14 deletions
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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,33 @@ help: add `#![feature(adt_const_params)]` to the crate attributes to enable more
7272
LL + #![feature(adt_const_params)]
7373
|
7474

75+
<<<<<<< HEAD
7576
error: maximum number of nodes exceeded in constant v20::v17::<v10, v2>::{constant#0}
7677
--> $DIR/unevaluated-const-ice-119731.rs:28:37
77-
|
78-
LL | impl<const v10: usize> v17<v10, v2> {
79-
| ^^
78+
||||||| parent of 883a4baff18 (Begin to use `ConstArgKind::Path` for all paths, not just params)
79+
error: maximum number of nodes exceeded in constant v20::v17::<v10, v2>::{constant#0}
80+
--> $DIR/unevaluated-const-ice-119731.rs:27:37
81+
=======
82+
error: maximum number of nodes exceeded in constant v20::v2
83+
--> $DIR/unevaluated-const-ice-119731.rs:11:5
84+
>>>>>>> 883a4baff18 (Begin to use `ConstArgKind::Path` for all paths, not just params)
85+
|
86+
LL | const v2: v11 = [[256; v4]; v4];
87+
| ^^^^^^^^^^^^^
8088

89+
<<<<<<< HEAD
8190
error: maximum number of nodes exceeded in constant v20::v17::<v10, v2>::{constant#0}
8291
--> $DIR/unevaluated-const-ice-119731.rs:28:37
83-
|
84-
LL | impl<const v10: usize> v17<v10, v2> {
85-
| ^^
92+
||||||| parent of 883a4baff18 (Begin to use `ConstArgKind::Path` for all paths, not just params)
93+
error: maximum number of nodes exceeded in constant v20::v17::<v10, v2>::{constant#0}
94+
--> $DIR/unevaluated-const-ice-119731.rs:27:37
95+
=======
96+
error: maximum number of nodes exceeded in constant v20::v2
97+
--> $DIR/unevaluated-const-ice-119731.rs:11:5
98+
>>>>>>> 883a4baff18 (Begin to use `ConstArgKind::Path` for all paths, not just params)
99+
|
100+
LL | const v2: v11 = [[256; v4]; v4];
101+
| ^^^^^^^^^^^^^
86102
|
87103
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
88104

Lines changed: 44 additions & 9 deletions
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`.

0 commit comments

Comments
 (0)