diff options
| author | Oblarg <emichaelbrnett@gmail.com> | 2025-09-18 18:15:19 -0400 |
|---|---|---|
| committer | Oblarg <emichaelbrnett@gmail.com> | 2025-09-18 18:55:13 -0400 |
| commit | 47d6936c7d37898a2de5549dcba8b50b25be21dd (patch) | |
| tree | b5a330e09d9ec517083aab0b13737d7478c13772 /src | |
| parent | c68acff6eb9e3bdb2b87c093bd6abac64e403a1d (diff) | |
| download | rust-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.rs | 23 | ||||
| -rw-r--r-- | src/tools/rust-analyzer/crates/ide/src/hover/tests.rs | 2 |
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> ``` --- |
