about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorOblarg <emichaelbrnett@gmail.com>2025-09-18 18:15:19 -0400
committerOblarg <emichaelbrnett@gmail.com>2025-09-18 18:55:13 -0400
commit47d6936c7d37898a2de5549dcba8b50b25be21dd (patch)
treeb5a330e09d9ec517083aab0b13737d7478c13772 /src
parentc68acff6eb9e3bdb2b87c093bd6abac64e403a1d (diff)
downloadrust-47d6936c7d37898a2de5549dcba8b50b25be21dd.tar.gz
rust-47d6936c7d37898a2de5549dcba8b50b25be21dd.zip
fix negative const generic integer literals
Diffstat (limited to 'src')
-rw-r--r--src/tools/rust-analyzer/crates/hir-ty/src/lower.rs23
-rw-r--r--src/tools/rust-analyzer/crates/ide/src/hover/tests.rs2
2 files changed, 24 insertions, 1 deletions
diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/lower.rs b/src/tools/rust-analyzer/crates/hir-ty/src/lower.rs
index 79f78c545e5..4d5172fd4f2 100644
--- a/src/tools/rust-analyzer/crates/hir-ty/src/lower.rs
+++ b/src/tools/rust-analyzer/crates/hir-ty/src/lower.rs
@@ -299,6 +299,29 @@ impl<'a> TyLoweringContext<'a> {
                 const_type,
                 self.resolver.krate(),
             ),
+            hir_def::hir::Expr::UnaryOp { expr: inner_expr, op: hir_def::hir::UnaryOp::Neg } => {
+                if let hir_def::hir::Expr::Literal(literal) = &self.store[*inner_expr] {
+                    // Only handle negation for signed integers and floats
+                    match literal {
+                        hir_def::hir::Literal::Int(_, _) | hir_def::hir::Literal::Float(_, _) => {
+                            if let Some(negated_literal) = literal.clone().negate() {
+                                intern_const_ref(
+                                    self.db,
+                                    &negated_literal.into(),
+                                    const_type,
+                                    self.resolver.krate(),
+                                )
+                            } else {
+                                unknown_const(const_type)
+                            }
+                        }
+                        // For unsigned integers, chars, bools, etc., negation is not meaningful
+                        _ => unknown_const(const_type),
+                    }
+                } else {
+                    unknown_const(const_type)
+                }
+            }
             _ => unknown_const(const_type),
         }
     }
diff --git a/src/tools/rust-analyzer/crates/ide/src/hover/tests.rs b/src/tools/rust-analyzer/crates/ide/src/hover/tests.rs
index 45aec387747..1ea11a215f8 100644
--- a/src/tools/rust-analyzer/crates/ide/src/hover/tests.rs
+++ b/src/tools/rust-analyzer/crates/ide/src/hover/tests.rs
@@ -4738,7 +4738,7 @@ fn main() {
             *value*
 
             ```rust
-            let value: Const<_>
+            let value: Const<-1>
             ```
 
             ---