Skip to content

Commit 2c6f0fc

Browse files
authored
Merge pull request #20697 from Oblarg/fix-negative-const-generic-literals
fix negative const generic integer literals
2 parents adf6c0d + 30031b8 commit 2c6f0fc

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-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/hir-ty/src/lower_nextsolver.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,29 @@ impl<'db, 'a> TyLoweringContext<'db, 'a> {
285285
const_type,
286286
self.resolver.krate(),
287287
),
288+
hir_def::hir::Expr::UnaryOp { expr: inner_expr, op: hir_def::hir::UnaryOp::Neg } => {
289+
if let hir_def::hir::Expr::Literal(literal) = &self.store[*inner_expr] {
290+
// Only handle negation for signed integers and floats
291+
match literal {
292+
hir_def::hir::Literal::Int(_, _) | hir_def::hir::Literal::Float(_, _) => {
293+
if let Some(negated_literal) = literal.clone().negate() {
294+
intern_const_ref(
295+
self.db,
296+
&negated_literal.into(),
297+
const_type,
298+
self.resolver.krate(),
299+
)
300+
} else {
301+
unknown_const(const_type)
302+
}
303+
}
304+
// For unsigned integers, chars, bools, etc., negation is not meaningful
305+
_ => unknown_const(const_type),
306+
}
307+
} else {
308+
unknown_const(const_type)
309+
}
310+
}
288311
_ => unknown_const(const_type),
289312
}
290313
}

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)