Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions crates/ide/src/hover/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4796,6 +4796,48 @@ fn main() {
);
}

#[test]
fn const_generic_negative_literal_macro_expansion() {
// Test that negative literals work correctly in const generics
// when used through macro expansion. This ensures the transcriber
// doesn't wrap negative literals in parentheses, which would create
// invalid syntax like Foo::<(-1)> instead of Foo::<-1>.
check(
r#"
struct Foo<const I: i16> {
pub value: i16,
}

impl<const I: i16> Foo<I> {
pub fn new(value: i16) -> Self {
Self { value }
}
}

macro_rules! create_foo {
($val:expr) => {
Foo::<$val>::new($val)
};
}

fn main() {
let v$0alue = create_foo!(-1);
}
"#,
expect![[r#"
*value*

```rust
let value: Foo<-1>
```

---

size = 2, align = 2, no Drop
"#]],
);
}

#[test]
fn hover_self_param_shows_type() {
check(
Expand Down
14 changes: 13 additions & 1 deletion crates/mbe/src/expander/transcriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,19 @@ fn expand_var(
let sub = sub.strip_invisible();
let mut span = id;
marker(&mut span);
let wrap_in_parens = !matches!(sub.flat_tokens(), [tt::TokenTree::Leaf(_)])

// Check if this is a simple negative literal (MINUS + LITERAL)
// that should not be wrapped in parentheses
let is_negative_literal = matches!(
sub.flat_tokens(),
[
tt::TokenTree::Leaf(tt::Leaf::Punct(tt::Punct { char: '-', .. })),
tt::TokenTree::Leaf(tt::Leaf::Literal(_))
]
);

let wrap_in_parens = !is_negative_literal
&& !matches!(sub.flat_tokens(), [tt::TokenTree::Leaf(_)])
&& sub.try_into_subtree().is_none_or(|it| {
it.top_subtree().delimiter.kind == tt::DelimiterKind::Invisible
});
Expand Down
Loading