Skip to content

Commit 034661e

Browse files
committed
wip
1 parent 94a8092 commit 034661e

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

+62-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use rustc_errors::{
2929
Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed, FatalError, struct_span_code_err,
3030
};
3131
use rustc_hir as hir;
32-
use rustc_hir::def::{CtorOf, DefKind, Namespace, Res};
32+
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Namespace, Res};
3333
use rustc_hir::def_id::{DefId, LocalDefId};
3434
use rustc_hir::{GenericArg, GenericArgs, HirId};
3535
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
@@ -2032,12 +2032,31 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
20322032
qpath.span(),
20332033
"fn's cannot be used as const args",
20342034
),
2035-
hir::QPath::Resolved(_, path @ &hir::Path { res: Res::Def(_, did), .. }) => {
2036-
let (item_segment, _) = path.segments.split_last().unwrap();
2037-
let args = self.lower_generic_args_of_path_segment(path.span, did, item_segment);
2038-
ty::Const::new_unevaluated(tcx, ty::UnevaluatedConst::new(did, args))
2035+
// hir::QPath::Resolved(_, path @ &hir::Path { res: Res::Def(_, did), .. }) => {
2036+
// let (item_segment, _) = path.segments.split_last().unwrap();
2037+
// let args = self.lower_generic_args_of_path_segment(path.span, did, item_segment);
2038+
// ty::Const::new_unevaluated(tcx, ty::UnevaluatedConst::new(did, args))
2039+
// }
2040+
// // TODO: type-relative paths
2041+
// _ => ty::Const::new_error_with_message(
2042+
// tcx,
2043+
// qpath.span(),
2044+
// "Const::lower_const_arg_path: invalid qpath",
2045+
// ),
2046+
hir::QPath::Resolved(maybe_qself, path) => {
2047+
debug!(?maybe_qself, ?path);
2048+
let opt_self_ty = maybe_qself.as_ref().map(|qself| self.lower_ty(qself));
2049+
self.lower_const_path_resolved(opt_self_ty, path, hir_id)
20392050
}
2051+
20402052
// TODO: type-relative paths
2053+
// hir::QPath::TypeRelative(qself, segment) => {
2054+
// debug!(?qself, ?segment);
2055+
// let ty = self.lower_ty(qself);
2056+
// self.lower_assoc_path(hir_ty.hir_id, hir_ty.span, ty, qself, segment, false)
2057+
// .map(|(ty, _, _)| ty)
2058+
// .unwrap_or_else(|guar| Ty::new_error(tcx, guar))
2059+
// }
20412060
_ => ty::Const::new_error_with_message(
20422061
tcx,
20432062
qpath.span(),
@@ -2046,6 +2065,44 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
20462065
}
20472066
}
20482067

2068+
fn lower_const_path_resolved(
2069+
&self,
2070+
opt_self_ty: Option<Ty<'tcx>>,
2071+
path: &hir::Path<'tcx>,
2072+
hir_id: HirId,
2073+
) -> Const<'tcx> {
2074+
let tcx = self.tcx();
2075+
let span = path.span;
2076+
match path.res {
2077+
Res::Def(DefKind::ConstParam, def_id) => {
2078+
assert_eq!(opt_self_ty, None);
2079+
let _ = self.prohibit_generic_args(
2080+
path.segments.iter(),
2081+
GenericsArgsErrExtend::Param(def_id),
2082+
);
2083+
self.lower_const_arg_param(def_id, hir_id)
2084+
}
2085+
Res::Def(DefKind::Const | DefKind::Ctor(_, CtorKind::Const), did) => {
2086+
assert_eq!(opt_self_ty, None);
2087+
let _ = self.prohibit_generic_args(
2088+
path.segments.split_last().unwrap().1.iter(),
2089+
GenericsArgsErrExtend::None,
2090+
);
2091+
let args = self.lower_generic_args_of_path_segment(
2092+
span,
2093+
did,
2094+
path.segments.last().unwrap(),
2095+
);
2096+
ty::Const::new_unevaluated(tcx, ty::UnevaluatedConst::new(did, args))
2097+
}
2098+
// TODO: DefKind::AssocConst?
2099+
_ => Const::new_error(
2100+
tcx,
2101+
tcx.dcx().span_delayed_bug(span, "invalid Res for const path"),
2102+
),
2103+
}
2104+
}
2105+
20492106
/// Lower a const param to a [`Const`]. This is only meant as a helper for [`Self::lower_const_arg_path`].
20502107
/// FIXME: dedup with lower_const_param
20512108
fn lower_const_arg_param(&self, param_def_id: DefId, path_hir_id: HirId) -> Const<'tcx> {

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

+2-11
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

+2-11
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

+1-15
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

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`.
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)