Merge pull request #20697 from Oblarg/fix-negative-const-generic-literals

fix negative const generic integer literals
This commit is contained in:
Shoyu Vanilla (Flint) 2025-09-19 14:13:54 +00:00 committed by GitHub
commit 2c6f0fc0df
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 47 additions and 1 deletions

View File

@ -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),
}
}

View File

@ -285,6 +285,29 @@ impl<'db, 'a> TyLoweringContext<'db, '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),
}
}

View File

@ -4738,7 +4738,7 @@ fn main() {
*value*
```rust
let value: Const<_>
let value: Const<-1>
```
---