mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-28 11:07:42 +00:00
fix: address suggestions
This commit is contained in:
parent
15f73008f8
commit
83177a7cfe
@ -24,7 +24,7 @@ use syntax::{ast, AstNode, AstPtr, SyntaxNodePtr};
|
|||||||
use crate::{
|
use crate::{
|
||||||
attr::{Attrs, RawAttrs},
|
attr::{Attrs, RawAttrs},
|
||||||
db::DefDatabase,
|
db::DefDatabase,
|
||||||
expr::{Expr, ExprId, Label, LabelId, Pat, PatId},
|
expr::{dummy_expr_id, Expr, ExprId, Label, LabelId, Pat, PatId},
|
||||||
item_scope::BuiltinShadowMode,
|
item_scope::BuiltinShadowMode,
|
||||||
macro_id_to_def_id,
|
macro_id_to_def_id,
|
||||||
nameres::DefMap,
|
nameres::DefMap,
|
||||||
@ -238,7 +238,7 @@ pub struct Mark {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// The body of an item (function, const etc.).
|
/// The body of an item (function, const etc.).
|
||||||
#[derive(Debug, Default, Eq, PartialEq)]
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub struct Body {
|
pub struct Body {
|
||||||
pub exprs: Arena<Expr>,
|
pub exprs: Arena<Expr>,
|
||||||
pub pats: Arena<Pat>,
|
pub pats: Arena<Pat>,
|
||||||
@ -389,6 +389,21 @@ impl Body {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for Body {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
body_expr: dummy_expr_id(),
|
||||||
|
exprs: Default::default(),
|
||||||
|
pats: Default::default(),
|
||||||
|
or_pats: Default::default(),
|
||||||
|
labels: Default::default(),
|
||||||
|
params: Default::default(),
|
||||||
|
block_scopes: Default::default(),
|
||||||
|
_c: Default::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Index<ExprId> for Body {
|
impl Index<ExprId> for Body {
|
||||||
type Output = Expr;
|
type Output = Expr;
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
//! Transforms `ast::Expr` into an equivalent `hir_def::expr::Expr`
|
//! Transforms `ast::Expr` into an equivalent `hir_def::expr::Expr`
|
||||||
//! representation.
|
//! representation.
|
||||||
|
|
||||||
use std::{collections::HashMap, mem, sync::Arc};
|
use std::{mem, sync::Arc};
|
||||||
|
|
||||||
use either::Either;
|
use either::Either;
|
||||||
use hir_expand::{
|
use hir_expand::{
|
||||||
@ -10,6 +10,8 @@ use hir_expand::{
|
|||||||
name::{name, AsName, Name},
|
name::{name, AsName, Name},
|
||||||
ExpandError, HirFileId, InFile,
|
ExpandError, HirFileId, InFile,
|
||||||
};
|
};
|
||||||
|
use la_arena::Arena;
|
||||||
|
use profile::Count;
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
use syntax::{
|
use syntax::{
|
||||||
ast::{
|
ast::{
|
||||||
@ -26,8 +28,8 @@ use crate::{
|
|||||||
builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint},
|
builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint},
|
||||||
db::DefDatabase,
|
db::DefDatabase,
|
||||||
expr::{
|
expr::{
|
||||||
Array, BindingAnnotation, Expr, ExprId, FloatTypeWrapper, Label, LabelId, Literal,
|
dummy_expr_id, Array, BindingAnnotation, Expr, ExprId, FloatTypeWrapper, Label, LabelId,
|
||||||
MatchArm, Pat, PatId, RecordFieldPat, RecordLitField, Statement,
|
Literal, MatchArm, Pat, PatId, RecordFieldPat, RecordLitField, Statement,
|
||||||
},
|
},
|
||||||
intern::Interned,
|
intern::Interned,
|
||||||
item_scope::BuiltinShadowMode,
|
item_scope::BuiltinShadowMode,
|
||||||
@ -80,7 +82,24 @@ pub(super) fn lower(
|
|||||||
params: Option<ast::ParamList>,
|
params: Option<ast::ParamList>,
|
||||||
body: Option<ast::Expr>,
|
body: Option<ast::Expr>,
|
||||||
) -> (Body, BodySourceMap) {
|
) -> (Body, BodySourceMap) {
|
||||||
ExprCollector::new(db, expander).collect(params, body)
|
ExprCollector {
|
||||||
|
db,
|
||||||
|
source_map: BodySourceMap::default(),
|
||||||
|
body: Body {
|
||||||
|
exprs: Arena::default(),
|
||||||
|
pats: Arena::default(),
|
||||||
|
labels: Arena::default(),
|
||||||
|
params: Vec::new(),
|
||||||
|
body_expr: dummy_expr_id(),
|
||||||
|
block_scopes: Vec::new(),
|
||||||
|
_c: Count::new(),
|
||||||
|
or_pats: Default::default(),
|
||||||
|
},
|
||||||
|
expander,
|
||||||
|
name_to_pat_grouping: Default::default(),
|
||||||
|
is_lowering_inside_or_pat: false,
|
||||||
|
}
|
||||||
|
.collect(params, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ExprCollector<'a> {
|
struct ExprCollector<'a> {
|
||||||
@ -93,18 +112,7 @@ struct ExprCollector<'a> {
|
|||||||
is_lowering_inside_or_pat: bool,
|
is_lowering_inside_or_pat: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> ExprCollector<'a> {
|
impl ExprCollector<'_> {
|
||||||
pub(crate) fn new(db: &'a dyn DefDatabase, expander: Expander) -> Self {
|
|
||||||
Self {
|
|
||||||
db,
|
|
||||||
expander,
|
|
||||||
body: Body::default(),
|
|
||||||
source_map: BodySourceMap::default(),
|
|
||||||
name_to_pat_grouping: HashMap::default(),
|
|
||||||
is_lowering_inside_or_pat: false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn collect(
|
fn collect(
|
||||||
mut self,
|
mut self,
|
||||||
param_list: Option<ast::ParamList>,
|
param_list: Option<ast::ParamList>,
|
||||||
@ -681,6 +689,7 @@ impl<'a> ExprCollector<'a> {
|
|||||||
};
|
};
|
||||||
let prev_def_map = mem::replace(&mut self.expander.def_map, def_map);
|
let prev_def_map = mem::replace(&mut self.expander.def_map, def_map);
|
||||||
let prev_local_module = mem::replace(&mut self.expander.module, module);
|
let prev_local_module = mem::replace(&mut self.expander.module, module);
|
||||||
|
|
||||||
let mut statements: Vec<_> =
|
let mut statements: Vec<_> =
|
||||||
block.statements().filter_map(|s| self.collect_stmt(s)).collect();
|
block.statements().filter_map(|s| self.collect_stmt(s)).collect();
|
||||||
let tail = block.tail_expr().and_then(|e| self.maybe_collect_expr(e));
|
let tail = block.tail_expr().and_then(|e| self.maybe_collect_expr(e));
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
//! See also a neighboring `body` module.
|
//! See also a neighboring `body` module.
|
||||||
|
|
||||||
use hir_expand::name::Name;
|
use hir_expand::name::Name;
|
||||||
use la_arena::Idx;
|
use la_arena::{Idx, RawIdx};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint},
|
builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint},
|
||||||
@ -27,6 +27,11 @@ pub use syntax::ast::{ArithOp, BinaryOp, CmpOp, LogicOp, Ordering, RangeOp, Unar
|
|||||||
|
|
||||||
pub type ExprId = Idx<Expr>;
|
pub type ExprId = Idx<Expr>;
|
||||||
|
|
||||||
|
/// FIXME: this is a hacky function which should be removed
|
||||||
|
pub(crate) fn dummy_expr_id() -> ExprId {
|
||||||
|
ExprId::from_raw(RawIdx::from(u32::MAX))
|
||||||
|
}
|
||||||
|
|
||||||
pub type PatId = Idx<Pat>;
|
pub type PatId = Idx<Pat>;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
//! Transforms syntax into `Path` objects, ideally with accounting for hygiene
|
//! Transforms syntax into `Path` objects, ideally with accounting for hygiene
|
||||||
|
|
||||||
use crate::{
|
use crate::{intern::Interned, type_ref::ConstScalarOrPath};
|
||||||
intern::Interned,
|
|
||||||
type_ref::{ConstScalar, ConstScalarOrPath},
|
|
||||||
};
|
|
||||||
|
|
||||||
use either::Either;
|
use either::Either;
|
||||||
use hir_expand::name::{name, AsName};
|
use hir_expand::name::{name, AsName};
|
||||||
@ -184,10 +181,7 @@ pub(super) fn lower_generic_args(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
ast::GenericArg::ConstArg(arg) => {
|
ast::GenericArg::ConstArg(arg) => {
|
||||||
let arg = arg.expr().map_or(
|
let arg = ConstScalarOrPath::from_expr_opt(arg.expr());
|
||||||
ConstScalarOrPath::Scalar(ConstScalar::Unknown),
|
|
||||||
ConstScalarOrPath::from_expr,
|
|
||||||
);
|
|
||||||
args.push(GenericArg::Const(arg))
|
args.push(GenericArg::Const(arg))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
//! HIR for references to types. Paths in these are not yet resolved. They can
|
//! HIR for references to types. Paths in these are not yet resolved. They can
|
||||||
//! be directly created from an ast::TypeRef, without further queries.
|
//! be directly created from an ast::TypeRef, without further queries.
|
||||||
|
|
||||||
|
use std::fmt::Write;
|
||||||
|
|
||||||
use hir_expand::{
|
use hir_expand::{
|
||||||
name::{AsName, Name},
|
name::{AsName, Name},
|
||||||
AstId, InFile,
|
AstId, InFile,
|
||||||
@ -182,11 +184,7 @@ impl TypeRef {
|
|||||||
// `hir_def::body::lower` to lower this into an `Expr` and then evaluate it at the
|
// `hir_def::body::lower` to lower this into an `Expr` and then evaluate it at the
|
||||||
// `hir_ty` level, which would allow knowing the type of:
|
// `hir_ty` level, which would allow knowing the type of:
|
||||||
// let v: [u8; 2 + 2] = [0u8; 4];
|
// let v: [u8; 2 + 2] = [0u8; 4];
|
||||||
let len = inner.expr().map_or(
|
let len = ConstScalarOrPath::from_expr_opt(inner.expr());
|
||||||
ConstScalarOrPath::Scalar(ConstScalar::Unknown),
|
|
||||||
ConstScalarOrPath::from_expr,
|
|
||||||
);
|
|
||||||
|
|
||||||
TypeRef::Array(Box::new(TypeRef::from_ast_opt(ctx, inner.ty())), len)
|
TypeRef::Array(Box::new(TypeRef::from_ast_opt(ctx, inner.ty())), len)
|
||||||
}
|
}
|
||||||
ast::Type::SliceType(inner) => {
|
ast::Type::SliceType(inner) => {
|
||||||
@ -394,9 +392,16 @@ impl std::fmt::Display for ConstScalarOrPath {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ConstScalarOrPath {
|
impl ConstScalarOrPath {
|
||||||
|
pub(crate) fn from_expr_opt(expr: Option<ast::Expr>) -> Self {
|
||||||
|
match expr {
|
||||||
|
Some(x) => Self::from_expr(x),
|
||||||
|
None => Self::Scalar(ConstScalar::Unknown),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// FIXME: as per the comments on `TypeRef::Array`, this evaluation should not happen at this
|
// FIXME: as per the comments on `TypeRef::Array`, this evaluation should not happen at this
|
||||||
// parse stage.
|
// parse stage.
|
||||||
pub(crate) fn from_expr(expr: ast::Expr) -> Self {
|
fn from_expr(expr: ast::Expr) -> Self {
|
||||||
match expr {
|
match expr {
|
||||||
ast::Expr::PathExpr(p) => {
|
ast::Expr::PathExpr(p) => {
|
||||||
match p.path().and_then(|x| x.segment()).and_then(|x| x.name_ref()) {
|
match p.path().and_then(|x| x.segment()).and_then(|x| x.name_ref()) {
|
||||||
@ -480,7 +485,7 @@ impl std::fmt::Display for ConstScalar {
|
|||||||
ConstScalar::UInt(num) => num.fmt(f),
|
ConstScalar::UInt(num) => num.fmt(f),
|
||||||
ConstScalar::Bool(flag) => flag.fmt(f),
|
ConstScalar::Bool(flag) => flag.fmt(f),
|
||||||
ConstScalar::Char(c) => write!(f, "'{c}'"),
|
ConstScalar::Char(c) => write!(f, "'{c}'"),
|
||||||
ConstScalar::Unknown => f.write_str("{unknown}"),
|
ConstScalar::Unknown => f.write_char('_'),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -395,22 +395,14 @@ pub fn unknown_const_as_generic(ty: Ty) -> GenericArg {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Interns a constant scalar with the given type
|
/// Interns a constant scalar with the given type
|
||||||
pub fn intern_const_scalar_with_type(value: ConstScalar, ty: Ty) -> Const {
|
pub fn intern_const_scalar(value: ConstScalar, ty: Ty) -> Const {
|
||||||
ConstData { ty, value: ConstValue::Concrete(chalk_ir::ConcreteConst { interned: value }) }
|
ConstData { ty, value: ConstValue::Concrete(chalk_ir::ConcreteConst { interned: value }) }
|
||||||
.intern(Interner)
|
.intern(Interner)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Interns a possibly-unknown target usize
|
/// Interns a possibly-unknown target usize
|
||||||
pub fn usize_const(value: Option<u128>) -> Const {
|
pub fn usize_const(value: Option<u128>) -> Const {
|
||||||
intern_const_scalar_with_type(
|
intern_const_scalar(value.map_or(ConstScalar::Unknown, ConstScalar::UInt), TyBuilder::usize())
|
||||||
value.map(ConstScalar::UInt).unwrap_or(ConstScalar::Unknown),
|
|
||||||
TyBuilder::usize(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Interns a constant scalar with the default type
|
|
||||||
pub fn intern_const_scalar(value: ConstScalar) -> Const {
|
|
||||||
intern_const_scalar_with_type(value, TyBuilder::builtin(value.builtin_type()))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn const_eval_recover(
|
pub(crate) fn const_eval_recover(
|
||||||
@ -470,7 +462,7 @@ pub(crate) fn eval_to_const<'a>(
|
|||||||
Ok(ComputedExpr::Literal(literal)) => literal.into(),
|
Ok(ComputedExpr::Literal(literal)) => literal.into(),
|
||||||
_ => ConstScalar::Unknown,
|
_ => ConstScalar::Unknown,
|
||||||
};
|
};
|
||||||
intern_const_scalar_with_type(const_scalar, TyBuilder::usize())
|
intern_const_scalar(const_scalar, TyBuilder::usize())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -273,6 +273,7 @@ impl<'a> InferenceContext<'a> {
|
|||||||
elem_ty.clone(),
|
elem_ty.clone(),
|
||||||
intern_const_scalar(
|
intern_const_scalar(
|
||||||
len.map_or(ConstScalar::Unknown, |len| ConstScalar::UInt(len)),
|
len.map_or(ConstScalar::Unknown, |len| ConstScalar::UInt(len)),
|
||||||
|
TyBuilder::usize(),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -257,7 +257,7 @@ impl chalk_ir::interner::Interner for Interner {
|
|||||||
c1: &Self::InternedConcreteConst,
|
c1: &Self::InternedConcreteConst,
|
||||||
c2: &Self::InternedConcreteConst,
|
c2: &Self::InternedConcreteConst,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
c1 == c2
|
(c1 == &ConstScalar::Unknown) || (c2 == &ConstScalar::Unknown) || (c1 == c2)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn intern_generic_arg(
|
fn intern_generic_arg(
|
||||||
|
@ -44,9 +44,7 @@ use syntax::{ast, SmolStr};
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
all_super_traits,
|
all_super_traits,
|
||||||
consteval::{
|
consteval::{intern_const_scalar, path_to_const, unknown_const, unknown_const_as_generic},
|
||||||
intern_const_scalar_with_type, path_to_const, unknown_const, unknown_const_as_generic,
|
|
||||||
},
|
|
||||||
db::HirDatabase,
|
db::HirDatabase,
|
||||||
make_binders,
|
make_binders,
|
||||||
mapping::ToChalk,
|
mapping::ToChalk,
|
||||||
@ -1744,7 +1742,7 @@ pub(crate) fn const_or_path_to_chalk(
|
|||||||
debruijn: DebruijnIndex,
|
debruijn: DebruijnIndex,
|
||||||
) -> Const {
|
) -> Const {
|
||||||
match value {
|
match value {
|
||||||
ConstScalarOrPath::Scalar(s) => intern_const_scalar_with_type(s.clone(), expected_ty),
|
ConstScalarOrPath::Scalar(s) => intern_const_scalar(s.clone(), expected_ty),
|
||||||
ConstScalarOrPath::Path(n) => {
|
ConstScalarOrPath::Path(n) => {
|
||||||
let path = ModPath::from_segments(PathKind::Plain, Some(n.clone()));
|
let path = ModPath::from_segments(PathKind::Plain, Some(n.clone()));
|
||||||
path_to_const(db, resolver, &path, mode, args, debruijn)
|
path_to_const(db, resolver, &path, mode, args, debruijn)
|
||||||
|
@ -3011,14 +3011,14 @@ struct TS(usize);
|
|||||||
fn main() {
|
fn main() {
|
||||||
let x;
|
let x;
|
||||||
[x,] = &[1,];
|
[x,] = &[1,];
|
||||||
//^^^^expected &[i32; 1], got [{unknown}; {unknown}]
|
//^^^^expected &[i32; 1], got [{unknown}; _]
|
||||||
|
|
||||||
// FIXME we only want the outermost error, but this matches the current
|
// FIXME we only want the outermost error, but this matches the current
|
||||||
// behavior of slice patterns
|
// behavior of slice patterns
|
||||||
let x;
|
let x;
|
||||||
[(x,),] = &[(1,),];
|
[(x,),] = &[(1,),];
|
||||||
// ^^^^expected {unknown}, got ({unknown},)
|
// ^^^^expected {unknown}, got ({unknown},)
|
||||||
//^^^^^^^expected &[(i32,); 1], got [{unknown}; {unknown}]
|
//^^^^^^^expected &[(i32,); 1], got [{unknown}; _]
|
||||||
|
|
||||||
let x;
|
let x;
|
||||||
((x,),) = &((1,),);
|
((x,),) = &((1,),);
|
||||||
|
@ -328,7 +328,7 @@ fn div(x: i32, y: i32) -> Option<i32> {
|
|||||||
}
|
}
|
||||||
fn main() {
|
fn main() {
|
||||||
run(f()) // FIXME: remove this error
|
run(f()) // FIXME: remove this error
|
||||||
//^^^ error: expected Rate<5>, found Rate<{unknown}>
|
//^^^ error: expected Rate<5>, found Rate<_>
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
|
@ -17,12 +17,6 @@ pub use map::ArenaMap;
|
|||||||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
pub struct RawIdx(u32);
|
pub struct RawIdx(u32);
|
||||||
|
|
||||||
impl Default for RawIdx {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self(u32::MAX)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<RawIdx> for u32 {
|
impl From<RawIdx> for u32 {
|
||||||
fn from(raw: RawIdx) -> u32 {
|
fn from(raw: RawIdx) -> u32 {
|
||||||
raw.0
|
raw.0
|
||||||
@ -53,12 +47,6 @@ pub struct Idx<T> {
|
|||||||
_ty: PhantomData<fn() -> T>,
|
_ty: PhantomData<fn() -> T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Default for Idx<T> {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self::from_raw(RawIdx::default())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> Clone for Idx<T> {
|
impl<T> Clone for Idx<T> {
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
*self
|
*self
|
||||||
|
Loading…
Reference in New Issue
Block a user