Skip to content

Commit 94b526f

Browse files
committed
Auto merge of #18119 - ChayimFriedman2:signed-const, r=HKalbasi
fix: Fix printing of constants greater than `i128::MAX` Fixes #18116.
2 parents 4221354 + 798c963 commit 94b526f

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

crates/hir/src/lib.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ use nameres::diagnostics::DefDiagnosticKind;
8080
use rustc_hash::FxHashSet;
8181
use smallvec::SmallVec;
8282
use span::{Edition, EditionedFileId, FileId, MacroCallId, SyntaxContextId};
83-
use stdx::{impl_from, never};
83+
use stdx::{format_to, impl_from, never};
8484
use syntax::{
8585
ast::{self, HasAttrs as _, HasGenericParams, HasName},
8686
format_smolstr, AstNode, AstPtr, SmolStr, SyntaxNode, SyntaxNodePtr, TextRange, ToSmolStr, T,
@@ -2578,10 +2578,16 @@ impl Const {
25782578
let value = u128::from_le_bytes(mir::pad16(b, false));
25792579
let value_signed =
25802580
i128::from_le_bytes(mir::pad16(b, matches!(s, Scalar::Int(_))));
2581+
let mut result = if let Scalar::Int(_) = s {
2582+
value_signed.to_string()
2583+
} else {
2584+
value.to_string()
2585+
};
25812586
if value >= 10 {
2582-
return Ok(format!("{value_signed} ({value:#X})"));
2587+
format_to!(result, " ({value:#X})");
2588+
return Ok(result);
25832589
} else {
2584-
return Ok(format!("{value_signed}"));
2590+
return Ok(result);
25852591
}
25862592
}
25872593
}

crates/ide/src/hover/tests.rs

+18
Original file line numberDiff line numberDiff line change
@@ -1496,6 +1496,24 @@ const foo$0: u32 = {
14961496
);
14971497
}
14981498

1499+
#[test]
1500+
fn hover_unsigned_max_const() {
1501+
check(
1502+
r#"const $0A: u128 = -1_i128 as u128;"#,
1503+
expect![[r#"
1504+
*A*
1505+
1506+
```rust
1507+
test
1508+
```
1509+
1510+
```rust
1511+
const A: u128 = 340282366920938463463374607431768211455 (0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)
1512+
```
1513+
"#]],
1514+
);
1515+
}
1516+
14991517
#[test]
15001518
fn hover_eval_complex_constants() {
15011519
check(

0 commit comments

Comments
 (0)