Skip to content

Commit 7b72ba8

Browse files
committed
properly convert scalar to string
1 parent a262e3e commit 7b72ba8

File tree

5 files changed

+106
-31
lines changed

5 files changed

+106
-31
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ dependencies = [
276276

277277
[[package]]
278278
name = "cargo"
279-
version = "0.59.0"
279+
version = "0.60.0"
280280
dependencies = [
281281
"anyhow",
282282
"atty",
@@ -419,7 +419,7 @@ dependencies = [
419419

420420
[[package]]
421421
name = "cargo-util"
422-
version = "0.1.1"
422+
version = "0.1.2"
423423
dependencies = [
424424
"anyhow",
425425
"core-foundation",
@@ -768,7 +768,7 @@ checksum = "8aebca1129a03dc6dc2b127edd729435bbc4a37e1d5f4d7513165089ceb02634"
768768

769769
[[package]]
770770
name = "crates-io"
771-
version = "0.33.0"
771+
version = "0.33.1"
772772
dependencies = [
773773
"anyhow",
774774
"curl",

compiler/rustc_middle/src/mir/interpret/value.rs

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_apfloat::{
88
use rustc_macros::HashStable;
99
use rustc_target::abi::{HasDataLayout, Size};
1010

11-
use crate::ty::{Lift, ParamEnv, ScalarInt, Ty, TyCtxt};
11+
use crate::ty::{self, Lift, ParamEnv, ScalarInt, Ty, TyCtxt};
1212

1313
use super::{
1414
AllocId, AllocRange, Allocation, InterpResult, Pointer, PointerArithmetic, Provenance,
@@ -456,6 +456,78 @@ impl<'tcx, Tag: Provenance> Scalar<Tag> {
456456
// Going through `u64` to check size and truncation.
457457
Ok(Double::from_bits(self.to_u64()?.into()))
458458
}
459+
460+
#[inline]
461+
pub fn try_to_string(self, t: Ty<'tcx>) -> Option<String> {
462+
#[cfg(target_pointer_width = "32")]
463+
fn usize_to_string<Tag: Provenance>(scalar: Scalar<Tag>) -> Option<String> {
464+
scalar.to_u32().map_or_else(|_| None, |v| Some(format!("{}", v)))
465+
}
466+
467+
#[cfg(target_pointer_width = "64")]
468+
fn usize_to_string<Tag: Provenance>(scalar: Scalar<Tag>) -> Option<String> {
469+
scalar.to_u64().map_or_else(|_| None, |v| Some(format!("{}", v)))
470+
}
471+
472+
#[cfg(target_pointer_width = "32")]
473+
fn isize_to_string<Tag: Provenance>(scalar: Scalar<Tag>) -> Option<String> {
474+
scalar.to_i32().map_or_else(|_| None, |v| Some(format!("{}", v)))
475+
}
476+
477+
#[cfg(target_pointer_width = "64")]
478+
fn isize_to_string<Tag: Provenance>(scalar: Scalar<Tag>) -> Option<String> {
479+
scalar.to_i64().map_or_else(|_| None, |v| Some(format!("{}", v)))
480+
}
481+
482+
#[cfg(target_pointer_width = "128")]
483+
fn isize_to_string<Tag: Provenance>(scalar: Scalar<Tag>) -> Option<String> {
484+
scalar.to_i128().map_or_else(|_| None, |v| Some(format!("{}", v)))
485+
}
486+
487+
#[cfg(target_pointer_width = "128")]
488+
fn usize_to_string<Tag: Provenance>(scalar: Scalar<Tag>) -> Option<String> {
489+
scalar.to_u128().map_or_else(|_| None, |v| Some(format!("{}", v)))
490+
}
491+
492+
match self {
493+
Scalar::Int(_) => match t.kind() {
494+
ty::Int(ty::IntTy::Isize) => isize_to_string(self),
495+
ty::Int(ty::IntTy::I8) => {
496+
self.to_i8().map_or_else(|_| None, |v| Some(format!("{}", v)))
497+
}
498+
ty::Int(ty::IntTy::I16) => {
499+
self.to_i16().map_or_else(|_| None, |v| Some(format!("{}", v)))
500+
}
501+
ty::Int(ty::IntTy::I32) => {
502+
self.to_i32().map_or_else(|_| None, |v| Some(format!("{}", v)))
503+
}
504+
ty::Int(ty::IntTy::I64) => {
505+
self.to_i64().map_or_else(|_| None, |v| Some(format!("{}", v)))
506+
}
507+
ty::Int(ty::IntTy::I128) => {
508+
self.to_i128().map_or_else(|_| None, |v| Some(format!("{}", v)))
509+
}
510+
ty::Uint(ty::UintTy::Usize) => usize_to_string(self),
511+
ty::Uint(ty::UintTy::U8) => {
512+
self.to_u8().map_or_else(|_| None, |v| Some(format!("{}", v)))
513+
}
514+
ty::Uint(ty::UintTy::U16) => {
515+
self.to_u16().map_or_else(|_| None, |v| Some(format!("{}", v)))
516+
}
517+
ty::Uint(ty::UintTy::U32) => {
518+
self.to_u32().map_or_else(|_| None, |v| Some(format!("{}", v)))
519+
}
520+
ty::Uint(ty::UintTy::U64) => {
521+
self.to_u64().map_or_else(|_| None, |v| Some(format!("{}", v)))
522+
}
523+
ty::Uint(ty::UintTy::U128) => {
524+
self.to_u128().map_or_else(|_| None, |v| Some(format!("{}", v)))
525+
}
526+
_ => None,
527+
},
528+
Scalar::Ptr(_, _) => Some(format!("{}", self)),
529+
}
530+
}
459531
}
460532

461533
#[derive(Clone, Copy, Eq, PartialEq, TyEncodable, TyDecodable, HashStable, Hash)]

compiler/rustc_trait_selection/src/traits/const_evaluatable.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -284,18 +284,22 @@ impl<'tcx> AbstractConst<'tcx> {
284284
return Some(PrintStyle::NoBraces(s));
285285
}
286286
}
287-
ty::ConstKind::Value(ConstValue::Scalar(scalar)) => match scalar.to_i64() {
288-
Ok(s) => {
289-
let s = format!("{}", s);
290-
291-
if applied_subst {
292-
return Some(PrintStyle::Braces(None, s));
293-
} else {
294-
return Some(PrintStyle::NoBraces(s));
287+
ty::ConstKind::Value(ConstValue::Scalar(scalar)) => {
288+
debug!("ct.ty: {:?}", ct.ty);
289+
let test_string = scalar.try_to_string(ct.ty);
290+
debug!(?test_string);
291+
292+
match scalar.try_to_string(ct.ty) {
293+
Some(s) => {
294+
if applied_subst {
295+
return Some(PrintStyle::Braces(None, s));
296+
} else {
297+
return Some(PrintStyle::NoBraces(s));
298+
}
295299
}
300+
None => return None,
296301
}
297-
Err(_) => return None,
298-
},
302+
}
299303
_ => return None,
300304
},
301305
abstract_const::Node::Binop(op, l, r) => {

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ pub trait InferCtxtExt<'tcx> {
8888
found_args: Vec<ArgKind>,
8989
is_closure: bool,
9090
) -> DiagnosticBuilder<'tcx>;
91+
92+
fn try_create_suggestion_for_mismatched_const(
93+
&self,
94+
expected_found: ExpectedFound<&'tcx ty::Const<'tcx>>,
95+
) -> Option<String>;
9196
}
9297

9398
impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
@@ -1091,9 +1096,19 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
10911096

10921097
err
10931098
}
1099+
1100+
fn try_create_suggestion_for_mismatched_const(
1101+
&self,
1102+
expected_found: ExpectedFound<&'tcx ty::Const<'tcx>>,
1103+
) -> Option<String> {
1104+
match AbstractConst::from_const(self.tcx, expected_found.found) {
1105+
Ok(Some(f_abstract)) => f_abstract.try_print_with_replacing_substs(self.tcx),
1106+
_ => None,
1107+
}
1108+
}
10941109
}
10951110

1096-
trait InferCtxtPrivExt<'hir, 'tcx> {
1111+
pub trait InferCtxtPrivExt<'hir, 'tcx> {
10971112
// returns if `cond` not occurring implies that `error` does not occur - i.e., that
10981113
// `error` occurring implies that `cond` occurs.
10991114
fn error_implies(&self, cond: ty::Predicate<'tcx>, error: ty::Predicate<'tcx>) -> bool;
@@ -1202,11 +1217,6 @@ trait InferCtxtPrivExt<'hir, 'tcx> {
12021217
obligated_types: &mut Vec<&ty::TyS<'tcx>>,
12031218
cause_code: &ObligationCauseCode<'tcx>,
12041219
) -> bool;
1205-
1206-
fn try_create_suggestion_for_mismatched_const(
1207-
&self,
1208-
expected_found: ExpectedFound<&'tcx ty::Const<'tcx>>,
1209-
) -> Option<String>;
12101220
}
12111221

12121222
impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
@@ -1528,16 +1538,6 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
15281538
}
15291539
}
15301540

1531-
fn try_create_suggestion_for_mismatched_const(
1532-
&self,
1533-
expected_found: ExpectedFound<&'tcx ty::Const<'tcx>>,
1534-
) -> Option<String> {
1535-
match AbstractConst::from_const(self.tcx, expected_found.found) {
1536-
Ok(Some(f_abstract)) => f_abstract.try_print_with_replacing_substs(self.tcx),
1537-
_ => None,
1538-
}
1539-
}
1540-
15411541
fn report_similar_impl_candidates(
15421542
&self,
15431543
impl_candidates: Vec<ty::TraitRef<'tcx>>,

compiler/rustc_trait_selection/src/traits/fulfill.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ use super::{FulfillmentError, FulfillmentErrorCode};
2727
use super::{ObligationCause, PredicateObligation};
2828

2929
use crate::traits::error_reporting::InferCtxtExt as _;
30-
use crate::traits::error_reporting::InferCtxtPrivExt;
3130
use crate::traits::project::PolyProjectionObligation;
3231
use crate::traits::project::ProjectionCacheKeyExt as _;
3332
use crate::traits::query::evaluate_obligation::InferCtxtExt as _;

0 commit comments

Comments
 (0)