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
6 changes: 5 additions & 1 deletion crates/hir-def/src/expr_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ impl ExpressionStore {
visitor.on_expr_opt(*end);
}
Pat::Lit(expr) | Pat::ConstBlock(expr) | Pat::Expr(expr) => visitor.on_expr(*expr),
Pat::Path(_) | Pat::Wild | Pat::Missing | Pat::Rest => {}
Pat::Path(_) | Pat::Wild | Pat::Missing | Pat::Rest | Pat::NotNull => {}
&Pat::Bind { subpat, id: _ } => visitor.on_pat_opt(subpat),
Pat::Or(args) | Pat::Tuple { args, ellipsis: _ } => visitor.on_pats(args),
Pat::TupleStruct { args, ellipsis: _, path } => {
Expand Down Expand Up @@ -855,6 +855,10 @@ impl ExpressionStore {
pub fn visit_type_ref_children(&self, type_ref: TypeRefId, mut visitor: impl StoreVisitor) {
match &self[type_ref] {
TypeRef::Never | TypeRef::Placeholder | TypeRef::TypeParam(_) | TypeRef::Error => {}
&TypeRef::PatternType(ty, pat) => {
visitor.on_type(ty);
visitor.on_pat(pat)
}
TypeRef::Tuple(types) => visitor.on_types(types),
TypeRef::Path(path) => visitor.on_path(path),
TypeRef::RawPtr(inner, _) | TypeRef::Slice(inner) => visitor.on_type(*inner),
Expand Down
6 changes: 3 additions & 3 deletions crates/hir-def/src/expr_store/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use syntax::ast;
use triomphe::Arc;

use crate::{
DefWithBodyId, HasModule,
DefWithBodyId, ExpressionStoreOwnerId, HasModule,
db::DefDatabase,
expr_store::{
ExpressionStore, ExpressionStoreSourceMap, SelfParamPtr, lower::lower_body, pretty,
Expand Down Expand Up @@ -160,12 +160,12 @@ impl Body {
pub fn pretty_print_pat(
&self,
db: &dyn DefDatabase,
owner: DefWithBodyId,
owner: ExpressionStoreOwnerId,
pat: PatId,
oneline: bool,
edition: Edition,
) -> String {
pretty::print_pat_hir(db, self, owner.into(), pat, oneline, edition)
pretty::print_pat_hir(db, self, owner, pat, oneline, edition)
}
}

Expand Down
5 changes: 5 additions & 0 deletions crates/hir-def/src/expr_store/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,10 @@ impl<'db> ExprCollector<'db> {
ast::Type::DynTraitType(inner) => TypeRef::DynTrait(
self.type_bounds_from_ast(inner.type_bound_list(), impl_trait_lower_fn),
),
ast::Type::PatternType(inner) => TypeRef::PatternType(
self.lower_type_ref_opt(inner.ty(), impl_trait_lower_fn),
self.collect_pat_top(inner.pat()),
),
ast::Type::MacroType(mt) => match mt.macro_call() {
Some(mcall) => {
let macro_ptr = AstPtr::new(&mcall);
Expand Down Expand Up @@ -2782,6 +2786,7 @@ impl<'db> ExprCollector<'db> {
let inner = self.collect_pat_opt(inner.pat(), binding_list);
Pat::Deref { inner }
}
ast::Pat::NotNull(_) => Pat::NotNull,
ast::Pat::ConstBlockPat(const_block_pat) => {
if let Some(block) = const_block_pat.block_expr() {
let expr_id = self.with_label_rib(RibKind::Constant, |this| {
Expand Down
6 changes: 6 additions & 0 deletions crates/hir-def/src/expr_store/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,7 @@ impl Printer<'_> {
Pat::Missing => w!(self, "�"),
Pat::Rest => w!(self, ".."),
Pat::Wild => w!(self, "_"),
Pat::NotNull => w!(self, "!null"),
Pat::Tuple { args, ellipsis } => {
w!(self, "(");
for (i, pat) in args.iter().enumerate() {
Expand Down Expand Up @@ -1346,6 +1347,11 @@ impl Printer<'_> {
w!(self, "dyn ");
self.print_type_bounds(bounds);
}
TypeRef::PatternType(ty, pat) => {
self.print_type_ref(*ty);
w!(self, " is ");
self.print_pat(*pat);
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion crates/hir-def/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,7 @@ pub enum Pat {
Deref {
inner: PatId,
},
NotNull,
ConstBlock(ExprId),
/// An expression inside a pattern. That can only occur inside assignments.
///
Expand All @@ -734,7 +735,8 @@ impl Pat {
| Pat::Wild
| Pat::Missing
| Pat::Rest
| Pat::Expr(_) => {}
| Pat::Expr(_)
| Pat::NotNull => {}
Pat::Bind { subpat, .. } => {
subpat.iter().copied().for_each(f);
}
Expand Down
3 changes: 2 additions & 1 deletion crates/hir-def/src/hir/type_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use thin_vec::ThinVec;
use crate::{
LifetimeParamId, TypeParamId,
expr_store::{ExpressionStore, path::Path},
hir::ExprId,
hir::{ExprId, PatId},
};

#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
Expand Down Expand Up @@ -139,6 +139,7 @@ pub enum TypeRef {
ImplTrait(ThinVec<TypeBound>),
DynTrait(ThinVec<TypeBound>),
TypeParam(TypeParamId),
PatternType(TypeRefId, PatId),
Error,
}

Expand Down
6 changes: 6 additions & 0 deletions crates/hir-def/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,12 @@ impl From<VariantId> for ExpressionStoreOwnerId {
}
}

impl From<ImplId> for ExpressionStoreOwnerId {
fn from(id: ImplId) -> Self {
ExpressionStoreOwnerId::Signature(id.into())
}
}

impl GenericDefId {
pub fn file_id_and_params_of(
self,
Expand Down
3 changes: 3 additions & 0 deletions crates/hir-expand/src/builtin/derive_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1494,6 +1494,9 @@ fn coerce_pointee_expand(
ast::Type::TupleType(ty) => any_long(ty.fields(), |ty| {
substitute_type_in_bound(editor, ty, param_name, replacement)
}),
ast::Type::PatternType(ty) => ty
.ty()
.is_some_and(|ty| substitute_type_in_bound(editor, ty, param_name, replacement)),
ast::Type::InferType(_) | ast::Type::MacroType(_) | ast::Type::NeverType(_) => false,
};

Expand Down
13 changes: 13 additions & 0 deletions crates/hir-expand/src/builtin/fn_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ register_builtin! {
(const_format_args, ConstFormatArgs) => format_args_expand,
(format_args_nl, FormatArgsNl) => format_args_nl_expand,
(quote, Quote) => quote_expand,
(pattern_type, PatternType) => pattern_type_expand,

EagerExpander:
(compile_error, CompileError) => compile_error_expand,
Expand Down Expand Up @@ -994,3 +995,15 @@ fn unescape_str(s: &str) -> Cow<'_, str> {
Cow::Borrowed(s)
}
}

fn pattern_type_expand(
_db: &dyn ExpandDatabase,
_arg_id: MacroCallId,
tt: &tt::TopSubtree,
call_site: Span,
) -> ExpandResult<tt::TopSubtree> {
let mut tt = tt.clone();
tt.set_top_subtree_delimiter_kind(tt::DelimiterKind::Invisible);
let pound = mk_pound(call_site);
ExpandResult::ok(quote! {call_site => builtin #pound pattern_type ( #tt ) })
}
5 changes: 2 additions & 3 deletions crates/hir-ty/src/diagnostics/unsafe_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,8 @@ impl<'db> UnsafeVisitor<'db> {
| Pat::Box { .. }
| Pat::Deref { .. }
| Pat::Expr(..)
| Pat::ConstBlock(..) => {
self.on_unsafe_op(current.into(), UnsafetyReason::UnionField)
}
| Pat::ConstBlock(..)
| Pat::NotNull => self.on_unsafe_op(current.into(), UnsafetyReason::UnionField),
// `Or` only wraps other patterns, and `Missing`/`Wild` do not constitute a read.
Pat::Missing | Pat::Rest | Pat::Wild | Pat::Or(_) => {}
}
Expand Down
Loading
Loading