Skip to content

Commit 6431612

Browse files
committed
wip
1 parent 9be4e56 commit 6431612

File tree

6 files changed

+78
-67
lines changed

6 files changed

+78
-67
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use rustc_errors::{
3030
Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed, FatalError, struct_span_code_err,
3131
};
3232
use rustc_hir as hir;
33-
use rustc_hir::def::{CtorOf, DefKind, Namespace, Res};
33+
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Namespace, Res};
3434
use rustc_hir::def_id::{DefId, LocalDefId};
3535
use rustc_hir::{GenericArg, GenericArgs, HirId};
3636
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
@@ -2076,12 +2076,31 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
20762076
qpath.span(),
20772077
"fn's cannot be used as const args",
20782078
),
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))
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+
hir::QPath::Resolved(maybe_qself, path) => {
2091+
debug!(?maybe_qself, ?path);
2092+
let opt_self_ty = maybe_qself.as_ref().map(|qself| self.lower_ty(qself));
2093+
self.lower_const_path_resolved(opt_self_ty, path, hir_id)
20832094
}
2095+
20842096
// TODO: type-relative paths
2097+
// hir::QPath::TypeRelative(qself, segment) => {
2098+
// debug!(?qself, ?segment);
2099+
// let ty = self.lower_ty(qself);
2100+
// self.lower_assoc_path(hir_ty.hir_id, hir_ty.span, ty, qself, segment, false)
2101+
// .map(|(ty, _, _)| ty)
2102+
// .unwrap_or_else(|guar| Ty::new_error(tcx, guar))
2103+
// }
20852104
_ => ty::Const::new_error_with_message(
20862105
tcx,
20872106
qpath.span(),
@@ -2090,6 +2109,44 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
20902109
}
20912110
}
20922111

2112+
fn lower_const_path_resolved(
2113+
&self,
2114+
opt_self_ty: Option<Ty<'tcx>>,
2115+
path: &hir::Path<'tcx>,
2116+
hir_id: HirId,
2117+
) -> Const<'tcx> {
2118+
let tcx = self.tcx();
2119+
let span = path.span;
2120+
match path.res {
2121+
Res::Def(DefKind::ConstParam, def_id) => {
2122+
assert_eq!(opt_self_ty, None);
2123+
let _ = self.prohibit_generic_args(
2124+
path.segments.iter(),
2125+
GenericsArgsErrExtend::Param(def_id),
2126+
);
2127+
self.lower_const_arg_param(def_id, hir_id)
2128+
}
2129+
Res::Def(DefKind::Const | DefKind::Ctor(_, CtorKind::Const), did) => {
2130+
assert_eq!(opt_self_ty, None);
2131+
let _ = self.prohibit_generic_args(
2132+
path.segments.split_last().unwrap().1.iter(),
2133+
GenericsArgsErrExtend::None,
2134+
);
2135+
let args = self.lower_generic_args_of_path_segment(
2136+
span,
2137+
did,
2138+
path.segments.last().unwrap(),
2139+
);
2140+
ty::Const::new_unevaluated(tcx, ty::UnevaluatedConst::new(did, args))
2141+
}
2142+
// TODO: DefKind::AssocConst?
2143+
_ => Const::new_error(
2144+
tcx,
2145+
tcx.dcx().span_delayed_bug(span, "invalid Res for const path"),
2146+
),
2147+
}
2148+
}
2149+
20932150
/// Lower a const param to a [`Const`]. This is only meant as a helper for [`Self::lower_const_arg_path`].
20942151
/// FIXME: dedup with lower_const_param
20952152
fn lower_const_arg_param(&self, param_def_id: DefId, path_hir_id: HirId) -> Const<'tcx> {

tests/ui/associated-consts/issue-58022.stderr

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
error[E0790]: cannot refer to the associated constant on trait without specifying the corresponding `impl` type
2-
--> $DIR/issue-58022.rs:4:25
3-
|
4-
LL | const SIZE: usize;
5-
| ------------------ `Foo::SIZE` defined here
6-
LL |
7-
LL | fn new(slice: &[u8; Foo::SIZE]) -> Self;
8-
| ^^^^^^^^^ cannot refer to the associated constant of trait
9-
101
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
112
--> $DIR/issue-58022.rs:13:41
123
|
@@ -27,7 +18,7 @@ error[E0423]: expected function, tuple struct or tuple variant, found trait `Foo
2718
LL | Foo(Box::new(*slice))
2819
| ^^^ not a function, tuple struct or tuple variant
2920

30-
error: aborting due to 3 previous errors
21+
error: aborting due to 2 previous errors
3122

32-
Some errors have detailed explanations: E0277, E0423, E0790.
23+
Some errors have detailed explanations: E0277, E0423.
3324
For more information about an error, try `rustc --explain E0277`.

tests/ui/associated-item/issue-48027.stderr

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,6 @@ LL | const X: usize;
1313
| ^ ...because it contains this associated `const`
1414
= help: consider moving `X` to another trait
1515

16-
error[E0790]: cannot refer to the associated constant on trait without specifying the corresponding `impl` type
17-
--> $DIR/issue-48027.rs:3:32
18-
|
19-
LL | const X: usize;
20-
| --------------- `Bar::X` defined here
21-
LL | fn return_n(&self) -> [u8; Bar::X];
22-
| ^^^^^^ cannot refer to the associated constant of trait
23-
24-
error: aborting due to 2 previous errors
16+
error: aborting due to 1 previous error
2517

26-
Some errors have detailed explanations: E0038, E0790.
27-
For more information about an error, try `rustc --explain E0038`.
18+
For more information about this error, try `rustc --explain E0038`.

tests/ui/const-generics/bad-subst-const-kind.stderr

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,5 @@ error: the constant `N` is not of type `usize`
44
LL | impl<const N: u64> Q for [u8; N] {
55
| ^^^^^^^ expected `usize`, found `u64`
66

7-
error: the constant `13` is not of type `u64`
8-
--> $DIR/bad-subst-const-kind.rs:13:24
9-
|
10-
LL | pub fn test() -> [u8; <[u8; 13] as Q>::ASSOC] {
11-
| ^^^^^^^^ expected `u64`, found `usize`
12-
|
13-
note: required for `[u8; 13]` to implement `Q`
14-
--> $DIR/bad-subst-const-kind.rs:8:20
15-
|
16-
LL | impl<const N: u64> Q for [u8; N] {
17-
| ------------ ^ ^^^^^^^
18-
| |
19-
| unsatisfied trait bound introduced here
20-
21-
error: aborting due to 2 previous errors
7+
error: aborting due to 1 previous error
228

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
error[E0275]: overflow evaluating the requirement `S<{ U }> well-formed`
2-
--> $DIR/adt_wf_hang.rs:11:5
1+
error[E0741]: `U` must implement `ConstParamTy` to be used as the type of a const generic parameter
2+
--> $DIR/adt_wf_hang.rs:9:19
33
|
4-
LL | S<{ U }>:;
5-
| ^^^^^^^^
4+
LL | struct S<const N: U>()
5+
| ^
66
|
7-
note: required by a bound in `S`
8-
--> $DIR/adt_wf_hang.rs:11:5
7+
help: add `#[derive(ConstParamTy)]` to the struct
8+
|
9+
LL + #[derive(ConstParamTy)]
10+
LL | struct U;
911
|
10-
LL | struct S<const N: U>()
11-
| - required by a bound in this struct
12-
LL | where
13-
LL | S<{ U }>:;
14-
| ^^^^^^^^ required by this bound in `S`
1512

1613
error: aborting due to 1 previous error
1714

18-
For more information about this error, try `rustc --explain E0275`.
15+
For more information about this error, try `rustc --explain E0741`.
Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
1-
error: unconstrained generic constant
2-
--> $DIR/variance-associated-consts.rs:14:12
3-
|
4-
LL | field: [u8; <T as Trait>::Const]
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6-
|
7-
help: try adding a `where` bound
8-
|
9-
LL | struct Foo<T: Trait> where [(); <T as Trait>::Const]: {
10-
| ++++++++++++++++++++++++++++++++
11-
12-
error: [T: o]
1+
error: [T: *]
132
--> $DIR/variance-associated-consts.rs:13:1
143
|
154
LL | struct Foo<T: Trait> {
165
| ^^^^^^^^^^^^^^^^^^^^
176

18-
error: aborting due to 2 previous errors
7+
error: aborting due to 1 previous error
198

0 commit comments

Comments
 (0)