Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions crates/hir-def/src/expr_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -750,10 +750,6 @@ impl ExpressionStore {
visitor.on_expr(*lhs);
visitor.on_expr(*rhs);
}
Expr::Range { lhs, rhs, range_type: _ } => {
visitor.on_expr_opt(*lhs);
visitor.on_expr_opt(*rhs);
}
Expr::Index { base, index } => {
visitor.on_expr(*base);
visitor.on_expr(*index);
Expand Down
101 changes: 91 additions & 10 deletions crates/hir-def/src/expr_store/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ use crate::{
ArrayType, ConstRef, FnType, LifetimeRef, LifetimeRefId, Mutability, PathId, Rawness,
RefType, TraitBoundModifier, TraitRef, TypeBound, TypeRef, TypeRefId, UseArgRef,
},
unstable_features::UnstableFeatures,
};

pub use self::path::hir_segment_to_ast_segment;
Expand Down Expand Up @@ -1829,16 +1830,7 @@ impl<'db> ExprCollector<'db> {
let index = self.collect_expr_opt(e.index());
self.alloc_expr(Expr::Index { base, index }, syntax_ptr)
}
ast::Expr::RangeExpr(e) => {
let lhs = e.start().map(|lhs| self.collect_expr(lhs));
let rhs = e.end().map(|rhs| self.collect_expr(rhs));
match e.op_kind() {
Some(range_type) => {
self.alloc_expr(Expr::Range { lhs, rhs, range_type }, syntax_ptr)
}
None => self.alloc_expr(Expr::Missing, syntax_ptr),
}
}
ast::Expr::RangeExpr(e) => self.collect_range_expr(e, syntax_ptr),
ast::Expr::MacroExpr(e) => {
let e = e.macro_call()?;
let macro_ptr = AstPtr::new(&e);
Expand Down Expand Up @@ -1870,6 +1862,87 @@ impl<'db> ExprCollector<'db> {
})
}

fn collect_range_expr(&mut self, e: ast::RangeExpr, syntax_ptr: AstPtr<ast::Expr>) -> ExprId {
let lhs = e.start().map(|lhs| self.collect_expr(lhs));
let rhs = e.end().map(|rhs| self.collect_expr(rhs));
let kind = e.op_kind().unwrap_or(ast::RangeOp::Exclusive);
let new_range = self.features().new_range;
let lang_items = self.lang_items();
let lang_item = match (lhs, rhs, kind) {
(None, None, _) => lang_items.RangeFull,
(Some(..), None, ast::RangeOp::Exclusive) => {
if new_range {
lang_items.RangeFromCopy
} else {
lang_items.RangeFrom
}
}
(None, Some(..), ast::RangeOp::Exclusive) => lang_items.RangeTo,
(Some(..), Some(..), ast::RangeOp::Exclusive) => {
if new_range {
lang_items.RangeCopy
} else {
lang_items.Range
}
}
(None, Some(..), ast::RangeOp::Inclusive) => {
if new_range {
lang_items.RangeToInclusiveCopy
} else {
lang_items.RangeToInclusive
}
}
(Some(lhs), Some(rhs), ast::RangeOp::Inclusive) => {
if new_range {
lang_items.RangeInclusiveCopy
} else {
return self.collect_inclusive_range(syntax_ptr, lang_items, lhs, rhs);
}
}
(Some(..), None, ast::RangeOp::Inclusive) => {
if new_range {
lang_items.RangeFromCopy
} else {
lang_items.RangeFrom
}
}
};
let Some(struct_path) = self.lang_path(lang_item) else {
return self.alloc_expr(Expr::Missing, syntax_ptr);
};
let lhs = lhs.map(|lhs| (lhs, sym::start));
let rhs = rhs.map(|rhs| {
(
rhs,
if lang_item == lang_items.RangeInclusiveCopy
|| lang_item == lang_items.RangeToInclusiveCopy
{
sym::last
} else {
sym::end
},
)
});
let fields = std::iter::chain(lhs, rhs)
.map(|(expr, name)| RecordLitField { name: Name::new_symbol_root(name), expr })
.collect();
self.alloc_expr(
Expr::RecordLit { path: struct_path, fields, spread: RecordSpread::None },
syntax_ptr,
)
}

fn collect_inclusive_range(
&mut self,
syntax_ptr: AstPtr<ast::Expr>,
lang_items: &LangItems,
lhs: ExprId,
rhs: ExprId,
) -> ExprId {
let fn_path = self.alloc_expr_desugared(self.lang_path_expr(lang_items.RangeInclusiveNew));
self.alloc_expr(Expr::Call { callee: fn_path, args: Box::new([lhs, rhs]) }, syntax_ptr)
}

fn collect_expr_path(&mut self, e: ast::PathExpr) -> Option<(Path, HygieneId)> {
e.path().and_then(|path| {
let path = self.lower_path(path, &mut Self::impl_trait_error_allocator)?;
Expand Down Expand Up @@ -3327,6 +3400,10 @@ impl<'db> ExprCollector<'db> {
Some(Path::LangItem(lang?.into(), None))
}

fn lang_path_expr(&self, lang: Option<impl Into<LangItemTarget>>) -> Expr {
self.lang_path(lang).map_or(Expr::Missing, Expr::Path)
}

fn ty_rel_lang_path(
&self,
lang: Option<impl Into<LangItemTarget>>,
Expand Down Expand Up @@ -3354,6 +3431,10 @@ fn pat_literal_to_hir(lit: &ast::LiteralPat) -> Option<(Literal, ast::Literal)>
}

impl<'db> ExprCollector<'db> {
fn features(&self) -> &'db UnstableFeatures {
self.def_map.features()
}

fn with_fresh_binding_expr_root(&mut self, f: impl FnOnce(&mut Self) -> ExprId) -> ExprId {
self.with_expr_root(|this| this.with_binding_owner(f))
}
Expand Down
12 changes: 0 additions & 12 deletions crates/hir-def/src/expr_store/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,18 +746,6 @@ impl Printer<'_> {
self.whitespace();
self.print_expr_in(prec, *rhs);
}
Expr::Range { lhs, rhs, range_type } => {
if let Some(lhs) = lhs {
self.print_expr_in(prec, *lhs);
}
match range_type {
RangeOp::Exclusive => w!(self, ".."),
RangeOp::Inclusive => w!(self, "..="),
};
if let Some(rhs) = rhs {
self.print_expr_in(prec, *rhs);
}
}
Expr::Index { base, index } => {
self.print_expr_in(prec, *base);
w!(self, "[");
Expand Down
39 changes: 21 additions & 18 deletions crates/hir-def/src/expr_store/tests/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ mod m {
fn desugar_for_loop() {
pretty_print(
r#"
//- minicore: iterator
//- minicore: iterator, range
fn main() {
for ident in 0..10 {
foo();
Expand All @@ -173,23 +173,26 @@ fn main() {
}
"#,
expect![[r#"
fn main() {
match builtin#lang(into_iter)(
0..10,
) {
mut <ra@gennew>0 => loop {
match builtin#lang(next)(
&mut <ra@gennew>0,
) {
builtin#lang(None) => break,
builtin#lang(Some)(ident) => {
foo();
bar()
},
}
},
}
}"#]],
fn main() {
match builtin#lang(into_iter)(
builtin#lang(Range){
start: 0,
end: 10,
},
) {
mut <ra@gennew>0 => loop {
match builtin#lang(next)(
&mut <ra@gennew>0,
) {
builtin#lang(None) => break,
builtin#lang(Some)(ident) => {
foo();
bar()
},
}
},
}
}"#]],
);
}

Expand Down
7 changes: 0 additions & 7 deletions crates/hir-def/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,11 +370,6 @@ pub enum Expr {
target: PatId,
value: ExprId,
},
Range {
lhs: Option<ExprId>,
rhs: Option<ExprId>,
range_type: RangeOp,
},
Index {
base: ExprId,
index: ExprId,
Expand Down Expand Up @@ -459,8 +454,6 @@ impl Expr {
| Expr::Yield { .. } => ExprPrecedence::Jump,

Expr::Continue { .. } => ExprPrecedence::Unambiguous,

Expr::Range { .. } => ExprPrecedence::Range,
}
}
}
Expand Down
50 changes: 0 additions & 50 deletions crates/hir-ty/src/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2673,56 +2673,6 @@ impl<'db> InferenceContext<'db> {
}
}

fn resolve_range_full(&self) -> Option<AdtId> {
let struct_ = self.lang_items.RangeFull?;
Some(struct_.into())
}

fn has_new_range_feature(&self) -> bool {
self.features.new_range
}

fn resolve_range(&self) -> Option<AdtId> {
let struct_ = if self.has_new_range_feature() {
self.lang_items.RangeCopy?
} else {
self.lang_items.Range?
};
Some(struct_.into())
}

fn resolve_range_inclusive(&self) -> Option<AdtId> {
let struct_ = if self.has_new_range_feature() {
self.lang_items.RangeInclusiveCopy?
} else {
self.lang_items.RangeInclusiveStruct?
};
Some(struct_.into())
}

fn resolve_range_from(&self) -> Option<AdtId> {
let struct_ = if self.has_new_range_feature() {
self.lang_items.RangeFromCopy?
} else {
self.lang_items.RangeFrom?
};
Some(struct_.into())
}

fn resolve_range_to(&self) -> Option<AdtId> {
let struct_ = self.lang_items.RangeTo?;
Some(struct_.into())
}

fn resolve_range_to_inclusive(&self) -> Option<AdtId> {
let struct_ = if self.has_new_range_feature() {
self.lang_items.RangeToInclusiveCopy?
} else {
self.lang_items.RangeToInclusive?
};
Some(struct_.into())
}

fn resolve_va_list(&self) -> Option<AdtId> {
let struct_ = self.lang_items.VaList?;
Some(struct_.into())
Expand Down
9 changes: 0 additions & 9 deletions crates/hir-ty/src/infer/closure/analysis/expr_use_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,15 +684,6 @@ impl<'a, 'db, D: Delegate<'db>> ExprUseVisitor<'a, 'db, D> {
}
}

Expr::Range { lhs, rhs, .. } => {
if let Some(lhs) = lhs {
self.consume_expr(lhs)?;
}
if let Some(rhs) = rhs {
self.consume_expr(rhs)?;
}
}

Expr::IncludeBytes => {}
}
Ok(())
Expand Down
50 changes: 1 addition & 49 deletions crates/hir-ty/src/infer/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use rustc_type_ir::{
inherent::{IntoKind, Ty as _},
};
use stdx::never;
use syntax::ast::RangeOp;
use tracing::debug;

use crate::{
Expand All @@ -33,7 +32,7 @@ use crate::{
lower::lower_mutability,
method_resolution::{self, CandidateId, MethodCallee, MethodError},
next_solver::{
ClauseKind, FnSig, GenericArg, GenericArgs, Ty, TyKind, TypeError,
ClauseKind, FnSig, Ty, TyKind, TypeError,
infer::{
BoundRegionConversionTime, InferOk,
traits::{Obligation, ObligationCause},
Expand Down Expand Up @@ -269,7 +268,6 @@ impl<'db> InferenceContext<'db> {
| Expr::Unsafe { .. }
| Expr::Await { .. }
| Expr::Ref { .. }
| Expr::Range { .. }
| Expr::RecordLit { .. }
| Expr::Yeet { .. }
| Expr::Missing
Expand Down Expand Up @@ -677,52 +675,6 @@ impl<'db> InferenceContext<'db> {
self.types.types.unit
}
}
Expr::Range { lhs, rhs, range_type } => {
let lhs_ty =
lhs.map(|e| self.infer_expr_inner(e, &Expectation::none(), ExprIsRead::Yes));
let rhs_expect = lhs_ty.map_or_else(Expectation::none, Expectation::has_type);
let rhs_ty = rhs.map(|e| self.infer_expr(e, &rhs_expect, ExprIsRead::Yes));
let single_arg_adt = |adt, ty: Ty<'db>| {
Ty::new_adt(
self.interner(),
adt,
GenericArgs::new_from_slice(&[GenericArg::from(ty)]),
)
};
match (range_type, lhs_ty, rhs_ty) {
(RangeOp::Exclusive, None, None) => match self.resolve_range_full() {
Some(adt) => {
Ty::new_adt(self.interner(), adt, self.types.empty.generic_args)
}
None => self.err_ty(),
},
(RangeOp::Exclusive, None, Some(ty)) => match self.resolve_range_to() {
Some(adt) => single_arg_adt(adt, ty),
None => self.err_ty(),
},
(RangeOp::Inclusive, None, Some(ty)) => {
match self.resolve_range_to_inclusive() {
Some(adt) => single_arg_adt(adt, ty),
None => self.err_ty(),
}
}
(RangeOp::Exclusive, Some(_), Some(ty)) => match self.resolve_range() {
Some(adt) => single_arg_adt(adt, ty),
None => self.err_ty(),
},
(RangeOp::Inclusive, Some(_), Some(ty)) => {
match self.resolve_range_inclusive() {
Some(adt) => single_arg_adt(adt, ty),
None => self.err_ty(),
}
}
(RangeOp::Exclusive, Some(ty), None) => match self.resolve_range_from() {
Some(adt) => single_arg_adt(adt, ty),
None => self.err_ty(),
},
(RangeOp::Inclusive, _, None) => self.err_ty(),
}
}
Expr::Index { base, index } => {
let base_t = self.infer_expr_no_expect(*base, ExprIsRead::Yes);
let idx_t = self.infer_expr_no_expect(*index, ExprIsRead::Yes);
Expand Down
Loading