Skip to content

Commit de0ed98

Browse files
committed
Address final feedback
* rename ConstExtension->ConstExt * refactor a manual construction of a Const
1 parent 78d6b88 commit de0ed98

File tree

5 files changed

+26
-32
lines changed

5 files changed

+26
-32
lines changed

crates/hir/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use hir_def::{
5353
use hir_expand::{diagnostics::DiagnosticSink, name::name, MacroDefKind};
5454
use hir_ty::{
5555
autoderef,
56-
consteval::ConstExtension,
56+
consteval::ConstExt,
5757
could_unify,
5858
method_resolution::{self, def_crates, TyFingerprint},
5959
primitive::UintTy,

crates/hir_def/src/type_ref.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,14 @@ impl std::fmt::Display for ConstScalar {
336336
}
337337

338338
impl ConstScalar {
339+
/// Gets a target usize out of the ConstScalar
340+
pub fn as_usize(&self) -> Option<u64> {
341+
match self {
342+
&ConstScalar::Usize(us) => Some(us),
343+
_ => None,
344+
}
345+
}
346+
339347
// FIXME: as per the comments on `TypeRef::Array`, this evaluation should not happen at this
340348
// parse stage.
341349
fn usize_from_literal_expr(expr: ast::Expr) -> ConstScalar {

crates/hir_ty/src/consteval.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ use hir_def::{
1111
use crate::{Const, ConstData, ConstValue, Interner, TyKind};
1212

1313
/// Extension trait for [`Const`]
14-
pub trait ConstExtension {
14+
pub trait ConstExt {
1515
/// Is a [`Const`] unknown?
1616
fn is_unknown(&self) -> bool;
1717
}
1818

19-
impl ConstExtension for Const {
19+
impl ConstExt for Const {
2020
fn is_unknown(&self) -> bool {
2121
match self.data(&Interner).value {
2222
// interned Unknown
@@ -35,20 +35,12 @@ impl ConstExtension for Const {
3535
}
3636
}
3737

38-
/// Extension trait for [`Expr`]
39-
pub trait ExprEval {
40-
/// Attempts to evaluate the expression as a target usize.
41-
fn eval_usize(&self) -> Option<u64>;
42-
}
43-
44-
impl ExprEval for Expr {
45-
// FIXME: support more than just evaluating literals
46-
fn eval_usize(&self) -> Option<u64> {
47-
match self {
48-
Expr::Literal(Literal::Uint(v, None))
49-
| Expr::Literal(Literal::Uint(v, Some(BuiltinUint::Usize))) => (*v).try_into().ok(),
50-
_ => None,
51-
}
38+
// FIXME: support more than just evaluating literals
39+
pub fn eval_usize(expr: &Expr) -> Option<u64> {
40+
match expr {
41+
Expr::Literal(Literal::Uint(v, None))
42+
| Expr::Literal(Literal::Uint(v, Some(BuiltinUint::Usize))) => (*v).try_into().ok(),
43+
_ => None,
5244
}
5345
}
5446

crates/hir_ty/src/infer/expr.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ use stdx::always;
1515
use syntax::ast::RangeOp;
1616

1717
use crate::{
18-
autoderef,
19-
consteval::{self, ExprEval},
18+
autoderef, consteval,
2019
lower::lower_to_chalk_mutability,
2120
mapping::from_chalk,
2221
method_resolution, op,
@@ -738,7 +737,7 @@ impl<'a> InferenceContext<'a> {
738737
);
739738

740739
let repeat_expr = &self.body.exprs[*repeat];
741-
repeat_expr.eval_usize()
740+
consteval::eval_usize(repeat_expr)
742741
}
743742
};
744743

crates/hir_ty/src/lower.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ use std::cell::{Cell, RefCell};
99
use std::{iter, sync::Arc};
1010

1111
use base_db::CrateId;
12-
use chalk_ir::{
13-
cast::Cast, fold::Shift, interner::HasInterner, Mutability, Safety, Scalar, UintTy,
14-
};
12+
use chalk_ir::{cast::Cast, fold::Shift, interner::HasInterner, Mutability, Safety};
1513
use hir_def::{
1614
adt::StructKind,
1715
body::{Expander, LowerCtx},
@@ -31,16 +29,17 @@ use stdx::impl_from;
3129
use syntax::ast;
3230

3331
use crate::{
32+
consteval,
3433
db::HirDatabase,
3534
mapping::ToChalk,
3635
static_lifetime, to_assoc_type_id, to_chalk_trait_id, to_placeholder_idx,
3736
utils::{
3837
all_super_trait_refs, associated_type_by_name_including_super_traits, generics, Generics,
3938
},
40-
AliasEq, AliasTy, Binders, BoundVar, CallableSig, ConstData, ConstValue, DebruijnIndex, DynTy,
41-
FnPointer, FnSig, FnSubst, ImplTraitId, Interner, OpaqueTy, PolyFnSig, ProjectionTy,
42-
QuantifiedWhereClause, QuantifiedWhereClauses, ReturnTypeImplTrait, ReturnTypeImplTraits,
43-
Substitution, TraitEnvironment, TraitRef, TraitRefExt, Ty, TyBuilder, TyKind, WhereClause,
39+
AliasEq, AliasTy, Binders, BoundVar, CallableSig, DebruijnIndex, DynTy, FnPointer, FnSig,
40+
FnSubst, ImplTraitId, Interner, OpaqueTy, PolyFnSig, ProjectionTy, QuantifiedWhereClause,
41+
QuantifiedWhereClauses, ReturnTypeImplTrait, ReturnTypeImplTraits, Substitution,
42+
TraitEnvironment, TraitRef, TraitRefExt, Ty, TyBuilder, TyKind, WhereClause,
4443
};
4544

4645
#[derive(Debug)]
@@ -176,11 +175,7 @@ impl<'a> TyLoweringContext<'a> {
176175
TypeRef::Array(inner, len) => {
177176
let inner_ty = self.lower_ty(inner);
178177

179-
let const_len = ConstData {
180-
ty: TyKind::Scalar(Scalar::Uint(UintTy::Usize)).intern(&Interner),
181-
value: ConstValue::Concrete(chalk_ir::ConcreteConst { interned: *len }),
182-
}
183-
.intern(&Interner);
178+
let const_len = consteval::usize_const(len.as_usize());
184179

185180
TyKind::Array(inner_ty, const_len).intern(&Interner)
186181
}

0 commit comments

Comments
 (0)