Skip to content

Commit 9a36bc9

Browse files
feat: Expand const generic support to array lengths
1 parent 755e6c1 commit 9a36bc9

File tree

7 files changed

+53
-27
lines changed

7 files changed

+53
-27
lines changed

compiler/ui_tests/reflection/const_generics_are_supported/diagnostics.dot

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,17 @@ digraph "GET / - 0" {
2929
}
3030

3131
digraph "GET / - 1" {
32-
0 [ label = "0| app_e22d4bbe::character() -> app_e22d4bbe::CharConst<'a'>"]
33-
1 [ label = "1| app_e22d4bbe::boolean() -> app_e22d4bbe::BoolConst<true>"]
34-
2 [ label = "2| app_e22d4bbe::numeric() -> app_e22d4bbe::NumericConst<8>"]
35-
3 [ label = "3| app_e22d4bbe::handler(app_e22d4bbe::NumericConst<8>, app_e22d4bbe::BoolConst<true>, app_e22d4bbe::CharConst<'a'>) -> pavex::Response"]
36-
4 [ label = "4| <pavex::Response as pavex::IntoResponse>::into_response(pavex::Response) -> pavex::Response"]
37-
0 -> 3 [ ]
38-
1 -> 3 [ ]
39-
2 -> 3 [ ]
32+
0 [ label = "0| app_e22d4bbe::sized_array() -> app_e22d4bbe::SizedArray<4>"]
33+
1 [ label = "1| app_e22d4bbe::character() -> app_e22d4bbe::CharConst<'a'>"]
34+
2 [ label = "2| app_e22d4bbe::boolean() -> app_e22d4bbe::BoolConst<true>"]
35+
3 [ label = "3| app_e22d4bbe::numeric() -> app_e22d4bbe::NumericConst<8>"]
36+
4 [ label = "4| app_e22d4bbe::handler(app_e22d4bbe::NumericConst<8>, app_e22d4bbe::BoolConst<true>, app_e22d4bbe::CharConst<'a'>, app_e22d4bbe::SizedArray<4>) -> pavex::Response"]
37+
5 [ label = "5| <pavex::Response as pavex::IntoResponse>::into_response(pavex::Response) -> pavex::Response"]
38+
0 -> 4 [ ]
39+
1 -> 4 [ ]
40+
2 -> 4 [ ]
4041
3 -> 4 [ ]
42+
4 -> 5 [ ]
4143
}
4244

4345
digraph app_state {

compiler/ui_tests/reflection/const_generics_are_supported/expectations/app.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,12 @@ pub mod route_1 {
149149
<pavex::Response as pavex::IntoResponse>::into_response(v2)
150150
}
151151
async fn handler() -> pavex::Response {
152-
let v0 = app::character();
153-
let v1 = app::boolean();
154-
let v2 = app::numeric();
155-
let v3 = app::handler(v2, v1, v0);
156-
<pavex::Response as pavex::IntoResponse>::into_response(v3)
152+
let v0 = app::sized_array();
153+
let v1 = app::character();
154+
let v2 = app::boolean();
155+
let v3 = app::numeric();
156+
let v4 = app::handler(v3, v2, v1, v0);
157+
<pavex::Response as pavex::IntoResponse>::into_response(v4)
157158
}
158159
struct Next0<T>
159160
where

compiler/ui_tests/reflection/const_generics_are_supported/expectations/diagnostics.dot

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,17 @@ digraph "GET / - 0" {
2626
2 -> 3 [ ]
2727
}
2828
digraph "GET / - 1" {
29-
0 [ label = "0| app::character() -> app::CharConst<'a'>"]
30-
1 [ label = "1| app::boolean() -> app::BoolConst<true>"]
31-
2 [ label = "2| app::numeric() -> app::NumericConst<8>"]
32-
3 [ label = "3| app::handler(app::NumericConst<8>, app::BoolConst<true>, app::CharConst<'a'>) -> pavex::Response"]
33-
4 [ label = "4| <pavex::Response as pavex::IntoResponse>::into_response(pavex::Response) -> pavex::Response"]
34-
0 -> 3 [ ]
35-
1 -> 3 [ ]
36-
2 -> 3 [ ]
29+
0 [ label = "0| app::sized_array() -> app::SizedArray<4>"]
30+
1 [ label = "1| app::character() -> app::CharConst<'a'>"]
31+
2 [ label = "2| app::boolean() -> app::BoolConst<true>"]
32+
3 [ label = "3| app::numeric() -> app::NumericConst<8>"]
33+
4 [ label = "4| app::handler(app::NumericConst<8>, app::BoolConst<true>, app::CharConst<'a'>, app::SizedArray<4>) -> pavex::Response"]
34+
5 [ label = "5| <pavex::Response as pavex::IntoResponse>::into_response(pavex::Response) -> pavex::Response"]
35+
0 -> 4 [ ]
36+
1 -> 4 [ ]
37+
2 -> 4 [ ]
3738
3 -> 4 [ ]
39+
4 -> 5 [ ]
3840
}
3941
digraph app_state {
4042
0 [ label = "0| crate::ApplicationState() -> crate::ApplicationState"]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "application_e22d4bbe"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[package.metadata.px.generate]
7+
generator_type = "cargo_workspace_binary"
8+
generator_name = "app_e22d4bbe"

compiler/ui_tests/reflection/const_generics_are_supported/generated_app/src/lib.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,12 @@ pub mod route_1 {
149149
<pavex::Response as pavex::IntoResponse>::into_response(v2)
150150
}
151151
async fn handler() -> pavex::Response {
152-
let v0 = app_e22d4bbe::character();
153-
let v1 = app_e22d4bbe::boolean();
154-
let v2 = app_e22d4bbe::numeric();
155-
let v3 = app_e22d4bbe::handler(v2, v1, v0);
156-
<pavex::Response as pavex::IntoResponse>::into_response(v3)
152+
let v0 = app_e22d4bbe::sized_array();
153+
let v1 = app_e22d4bbe::character();
154+
let v2 = app_e22d4bbe::boolean();
155+
let v3 = app_e22d4bbe::numeric();
156+
let v4 = app_e22d4bbe::handler(v3, v2, v1, v0);
157+
<pavex::Response as pavex::IntoResponse>::into_response(v4)
157158
}
158159
struct Next0<T>
159160
where

compiler/ui_tests/reflection/const_generics_are_supported/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use pavex::{blueprint::from, Blueprint};
33
pub struct NumericConst<const N: usize>;
44
pub struct BoolConst<const B: bool>;
55
pub struct CharConst<const C: char>;
6+
pub struct SizedArray<const N: usize>(pub [u8; N]);
67

78
#[pavex::request_scoped]
89
pub fn numeric() -> NumericConst<8> {
@@ -19,11 +20,17 @@ pub fn character() -> CharConst<'a'> {
1920
CharConst
2021
}
2122

23+
#[pavex::request_scoped]
24+
pub fn sized_array() -> SizedArray<4> {
25+
SizedArray([0; 4])
26+
}
27+
2228
#[pavex::get(path = "/")]
2329
pub fn handler(
2430
_n: NumericConst<8>,
2531
_b: BoolConst<true>,
2632
_c: CharConst<'a'>,
33+
_s: SizedArray<4>,
2734
) -> pavex::Response {
2835
todo!()
2936
}

rustdoc/rustdoc_resolver/src/resolve_type.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,12 @@ fn _resolve_type<I: CrateIndexer>(
507507
}))
508508
}
509509
RustdocType::Array { type_, len } => {
510-
let len: usize = len.parse().map_err(|_| {
510+
let resolved_len = generic_bindings
511+
.consts
512+
.get(len)
513+
.map(|s| s.as_str())
514+
.unwrap_or(len);
515+
let len: usize = resolved_len.parse().map_err(|_| {
511516
TypeResolutionErrorDetails::UnsupportedArrayLength(UnsupportedArrayLength {
512517
len: len.clone(),
513518
})

0 commit comments

Comments
 (0)