Skip to content

Commit

Permalink
properly convert scalar to string
Browse files Browse the repository at this point in the history
  • Loading branch information
b-naber committed Dec 28, 2021
1 parent a262e3e commit 7b72ba8
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 31 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ dependencies = [

[[package]]
name = "cargo"
version = "0.59.0"
version = "0.60.0"
dependencies = [
"anyhow",
"atty",
Expand Down Expand Up @@ -419,7 +419,7 @@ dependencies = [

[[package]]
name = "cargo-util"
version = "0.1.1"
version = "0.1.2"
dependencies = [
"anyhow",
"core-foundation",
Expand Down Expand Up @@ -768,7 +768,7 @@ checksum = "8aebca1129a03dc6dc2b127edd729435bbc4a37e1d5f4d7513165089ceb02634"

[[package]]
name = "crates-io"
version = "0.33.0"
version = "0.33.1"
dependencies = [
"anyhow",
"curl",
Expand Down
74 changes: 73 additions & 1 deletion compiler/rustc_middle/src/mir/interpret/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rustc_apfloat::{
use rustc_macros::HashStable;
use rustc_target::abi::{HasDataLayout, Size};

use crate::ty::{Lift, ParamEnv, ScalarInt, Ty, TyCtxt};
use crate::ty::{self, Lift, ParamEnv, ScalarInt, Ty, TyCtxt};

use super::{
AllocId, AllocRange, Allocation, InterpResult, Pointer, PointerArithmetic, Provenance,
Expand Down Expand Up @@ -456,6 +456,78 @@ impl<'tcx, Tag: Provenance> Scalar<Tag> {
// Going through `u64` to check size and truncation.
Ok(Double::from_bits(self.to_u64()?.into()))
}

#[inline]
pub fn try_to_string(self, t: Ty<'tcx>) -> Option<String> {
#[cfg(target_pointer_width = "32")]
fn usize_to_string<Tag: Provenance>(scalar: Scalar<Tag>) -> Option<String> {
scalar.to_u32().map_or_else(|_| None, |v| Some(format!("{}", v)))
}

#[cfg(target_pointer_width = "64")]
fn usize_to_string<Tag: Provenance>(scalar: Scalar<Tag>) -> Option<String> {
scalar.to_u64().map_or_else(|_| None, |v| Some(format!("{}", v)))
}

#[cfg(target_pointer_width = "32")]
fn isize_to_string<Tag: Provenance>(scalar: Scalar<Tag>) -> Option<String> {
scalar.to_i32().map_or_else(|_| None, |v| Some(format!("{}", v)))
}

#[cfg(target_pointer_width = "64")]
fn isize_to_string<Tag: Provenance>(scalar: Scalar<Tag>) -> Option<String> {
scalar.to_i64().map_or_else(|_| None, |v| Some(format!("{}", v)))
}

#[cfg(target_pointer_width = "128")]
fn isize_to_string<Tag: Provenance>(scalar: Scalar<Tag>) -> Option<String> {
scalar.to_i128().map_or_else(|_| None, |v| Some(format!("{}", v)))
}

#[cfg(target_pointer_width = "128")]
fn usize_to_string<Tag: Provenance>(scalar: Scalar<Tag>) -> Option<String> {
scalar.to_u128().map_or_else(|_| None, |v| Some(format!("{}", v)))
}

match self {
Scalar::Int(_) => match t.kind() {
ty::Int(ty::IntTy::Isize) => isize_to_string(self),
ty::Int(ty::IntTy::I8) => {
self.to_i8().map_or_else(|_| None, |v| Some(format!("{}", v)))
}
ty::Int(ty::IntTy::I16) => {
self.to_i16().map_or_else(|_| None, |v| Some(format!("{}", v)))
}
ty::Int(ty::IntTy::I32) => {
self.to_i32().map_or_else(|_| None, |v| Some(format!("{}", v)))
}
ty::Int(ty::IntTy::I64) => {
self.to_i64().map_or_else(|_| None, |v| Some(format!("{}", v)))
}
ty::Int(ty::IntTy::I128) => {
self.to_i128().map_or_else(|_| None, |v| Some(format!("{}", v)))
}
ty::Uint(ty::UintTy::Usize) => usize_to_string(self),
ty::Uint(ty::UintTy::U8) => {
self.to_u8().map_or_else(|_| None, |v| Some(format!("{}", v)))
}
ty::Uint(ty::UintTy::U16) => {
self.to_u16().map_or_else(|_| None, |v| Some(format!("{}", v)))
}
ty::Uint(ty::UintTy::U32) => {
self.to_u32().map_or_else(|_| None, |v| Some(format!("{}", v)))
}
ty::Uint(ty::UintTy::U64) => {
self.to_u64().map_or_else(|_| None, |v| Some(format!("{}", v)))
}
ty::Uint(ty::UintTy::U128) => {
self.to_u128().map_or_else(|_| None, |v| Some(format!("{}", v)))
}
_ => None,
},
Scalar::Ptr(_, _) => Some(format!("{}", self)),
}
}
}

#[derive(Clone, Copy, Eq, PartialEq, TyEncodable, TyDecodable, HashStable, Hash)]
Expand Down
24 changes: 14 additions & 10 deletions compiler/rustc_trait_selection/src/traits/const_evaluatable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,18 +284,22 @@ impl<'tcx> AbstractConst<'tcx> {
return Some(PrintStyle::NoBraces(s));
}
}
ty::ConstKind::Value(ConstValue::Scalar(scalar)) => match scalar.to_i64() {
Ok(s) => {
let s = format!("{}", s);

if applied_subst {
return Some(PrintStyle::Braces(None, s));
} else {
return Some(PrintStyle::NoBraces(s));
ty::ConstKind::Value(ConstValue::Scalar(scalar)) => {
debug!("ct.ty: {:?}", ct.ty);
let test_string = scalar.try_to_string(ct.ty);
debug!(?test_string);

match scalar.try_to_string(ct.ty) {
Some(s) => {
if applied_subst {
return Some(PrintStyle::Braces(None, s));
} else {
return Some(PrintStyle::NoBraces(s));
}
}
None => return None,
}
Err(_) => return None,
},
}
_ => return None,
},
abstract_const::Node::Binop(op, l, r) => {
Expand Down
32 changes: 16 additions & 16 deletions compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ pub trait InferCtxtExt<'tcx> {
found_args: Vec<ArgKind>,
is_closure: bool,
) -> DiagnosticBuilder<'tcx>;

fn try_create_suggestion_for_mismatched_const(
&self,
expected_found: ExpectedFound<&'tcx ty::Const<'tcx>>,
) -> Option<String>;
}

impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
Expand Down Expand Up @@ -1091,9 +1096,19 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {

err
}

fn try_create_suggestion_for_mismatched_const(
&self,
expected_found: ExpectedFound<&'tcx ty::Const<'tcx>>,
) -> Option<String> {
match AbstractConst::from_const(self.tcx, expected_found.found) {
Ok(Some(f_abstract)) => f_abstract.try_print_with_replacing_substs(self.tcx),
_ => None,
}
}
}

trait InferCtxtPrivExt<'hir, 'tcx> {
pub trait InferCtxtPrivExt<'hir, 'tcx> {
// returns if `cond` not occurring implies that `error` does not occur - i.e., that
// `error` occurring implies that `cond` occurs.
fn error_implies(&self, cond: ty::Predicate<'tcx>, error: ty::Predicate<'tcx>) -> bool;
Expand Down Expand Up @@ -1202,11 +1217,6 @@ trait InferCtxtPrivExt<'hir, 'tcx> {
obligated_types: &mut Vec<&ty::TyS<'tcx>>,
cause_code: &ObligationCauseCode<'tcx>,
) -> bool;

fn try_create_suggestion_for_mismatched_const(
&self,
expected_found: ExpectedFound<&'tcx ty::Const<'tcx>>,
) -> Option<String>;
}

impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
Expand Down Expand Up @@ -1528,16 +1538,6 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
}
}

fn try_create_suggestion_for_mismatched_const(
&self,
expected_found: ExpectedFound<&'tcx ty::Const<'tcx>>,
) -> Option<String> {
match AbstractConst::from_const(self.tcx, expected_found.found) {
Ok(Some(f_abstract)) => f_abstract.try_print_with_replacing_substs(self.tcx),
_ => None,
}
}

fn report_similar_impl_candidates(
&self,
impl_candidates: Vec<ty::TraitRef<'tcx>>,
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_trait_selection/src/traits/fulfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use super::{FulfillmentError, FulfillmentErrorCode};
use super::{ObligationCause, PredicateObligation};

use crate::traits::error_reporting::InferCtxtExt as _;
use crate::traits::error_reporting::InferCtxtPrivExt;
use crate::traits::project::PolyProjectionObligation;
use crate::traits::project::ProjectionCacheKeyExt as _;
use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
Expand Down

0 comments on commit 7b72ba8

Please sign in to comment.