Skip to content

Commit f9351a8

Browse files
authored
Merge pull request rust-lang#18619 from ShoyuVanilla/issue-18613
fix: Panic when displaying generic params with defaults
2 parents 4883a12 + 7038073 commit f9351a8

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/tools/rust-analyzer/crates/hir-ty/src/display.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1047,10 +1047,14 @@ impl HirDisplay for Ty {
10471047
);
10481048
// We print all params except implicit impl Trait params. Still a bit weird; should we leave out parent and self?
10491049
if parameters.len() - impl_ > 0 {
1050+
let params_len = parameters.len();
10501051
// `parameters` are in the order of fn's params (including impl traits), fn's lifetimes
10511052
let parameters =
10521053
generic_args_sans_defaults(f, Some(generic_def_id), parameters);
1053-
let without_impl = self_param as usize + type_ + const_ + lifetime;
1054+
assert!(params_len >= parameters.len());
1055+
let defaults = params_len - parameters.len();
1056+
let without_impl =
1057+
self_param as usize + type_ + const_ + lifetime - defaults;
10541058
// parent's params (those from enclosing impl or trait, if any).
10551059
let (fn_params, parent_params) = parameters.split_at(without_impl + impl_);
10561060

src/tools/rust-analyzer/crates/ide/src/hover/tests.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9416,3 +9416,53 @@ fn f<T: UnCompat$0>
94169416
"#]],
94179417
);
94189418
}
9419+
9420+
#[test]
9421+
fn issue_18613() {
9422+
check(
9423+
r#"
9424+
fn main() {
9425+
struct S<T, D = bool>();
9426+
let x$0 = S::<()>;
9427+
}"#,
9428+
expect![[r#"
9429+
*x*
9430+
9431+
```rust
9432+
let x: fn S<()>() -> S<()>
9433+
```
9434+
9435+
---
9436+
9437+
size = 0, align = 1
9438+
"#]],
9439+
);
9440+
9441+
check(
9442+
r#"
9443+
pub struct Global;
9444+
pub struct Box<T, A = Global>(T, A);
9445+
9446+
impl<T> Box<T> {
9447+
pub fn new(x: T) -> Self { loop {} }
9448+
}
9449+
9450+
pub struct String;
9451+
9452+
fn main() {
9453+
let box_value$0 = Box::<String>new();
9454+
}
9455+
"#,
9456+
expect![[r#"
9457+
*box_value*
9458+
9459+
```rust
9460+
let box_value: fn Box<String>(String, Global) -> Box<String>
9461+
```
9462+
9463+
---
9464+
9465+
size = 0, align = 1
9466+
"#]],
9467+
);
9468+
}

0 commit comments

Comments
 (0)