Skip to content

Commit d0bdedd

Browse files
author
Oblarg
committed
fix negative const generic integer literals
1 parent 958a8d0 commit d0bdedd

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

crates/hir-ty/src/lower.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,29 @@ impl<'a> TyLoweringContext<'a> {
299299
const_type,
300300
self.resolver.krate(),
301301
),
302+
hir_def::hir::Expr::UnaryOp { expr: inner_expr, op: hir_def::hir::UnaryOp::Neg } => {
303+
if let hir_def::hir::Expr::Literal(literal) = &self.store[*inner_expr] {
304+
// Only handle negation for signed integers and floats
305+
match literal {
306+
hir_def::hir::Literal::Int(_, _) | hir_def::hir::Literal::Float(_, _) => {
307+
if let Some(negated_literal) = literal.clone().negate() {
308+
intern_const_ref(
309+
self.db,
310+
&negated_literal.into(),
311+
const_type,
312+
self.resolver.krate(),
313+
)
314+
} else {
315+
unknown_const(const_type)
316+
}
317+
}
318+
// For unsigned integers, chars, bools, etc., negation is not meaningful
319+
_ => unknown_const(const_type),
320+
}
321+
} else {
322+
unknown_const(const_type)
323+
}
324+
}
302325
_ => unknown_const(const_type),
303326
}
304327
}

crates/ide/src/hover/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4738,7 +4738,7 @@ fn main() {
47384738
*value*
47394739
47404740
```rust
4741-
let value: Const<_>
4741+
let value: Const<-1>
47424742
```
47434743
47444744
---

0 commit comments

Comments
 (0)