Skip to content

Commit 4e280bb

Browse files
committed
Implement pattern_type macro
1 parent 251df51 commit 4e280bb

24 files changed

Lines changed: 413 additions & 88 deletions

File tree

crates/hir-def/src/expr_store/body.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use syntax::ast;
88
use triomphe::Arc;
99

1010
use crate::{
11-
DefWithBodyId, HasModule,
11+
DefWithBodyId, ExpressionStoreOwnerId, HasModule,
1212
db::DefDatabase,
1313
expr_store::{
1414
ExpressionStore, ExpressionStoreSourceMap, SelfParamPtr, lower::lower_body, pretty,
@@ -139,7 +139,7 @@ impl Body {
139139
pub fn pretty_print_pat(
140140
&self,
141141
db: &dyn DefDatabase,
142-
owner: DefWithBodyId,
142+
owner: ExpressionStoreOwnerId,
143143
pat: PatId,
144144
oneline: bool,
145145
edition: Edition,

crates/hir-def/src/expr_store/lower.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,10 @@ impl<'db> ExprCollector<'db> {
720720
ast::Type::DynTraitType(inner) => TypeRef::DynTrait(
721721
self.type_bounds_from_ast(inner.type_bound_list(), impl_trait_lower_fn),
722722
),
723+
ast::Type::PatternType(inner) => TypeRef::PatternType(
724+
self.lower_type_ref_opt(inner.ty(), impl_trait_lower_fn),
725+
self.collect_pat_top(inner.pat()),
726+
),
723727
ast::Type::MacroType(mt) => match mt.macro_call() {
724728
Some(mcall) => {
725729
let macro_ptr = AstPtr::new(&mcall);

crates/hir-def/src/expr_store/pretty.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ pub fn print_expr_hir(
420420
pub fn print_pat_hir(
421421
db: &dyn DefDatabase,
422422
store: &ExpressionStore,
423-
_owner: DefWithBodyId,
423+
_owner: ExpressionStoreOwnerId,
424424
pat: PatId,
425425
oneline: bool,
426426
edition: Edition,
@@ -1340,6 +1340,11 @@ impl Printer<'_> {
13401340
w!(self, "dyn ");
13411341
self.print_type_bounds(bounds);
13421342
}
1343+
TypeRef::PatternType(ty, pat) => {
1344+
self.print_type_ref(*ty);
1345+
w!(self, " is ");
1346+
self.print_pat(*pat);
1347+
}
13431348
}
13441349
}
13451350

crates/hir-def/src/hir/type_ref.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
ExpressionStore,
1313
path::{GenericArg, Path},
1414
},
15-
hir::ExprId,
15+
hir::{ExprId, PatId},
1616
};
1717

1818
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
@@ -142,6 +142,7 @@ pub enum TypeRef {
142142
ImplTrait(ThinVec<TypeBound>),
143143
DynTrait(ThinVec<TypeBound>),
144144
TypeParam(TypeParamId),
145+
PatternType(TypeRefId, PatId),
145146
Error,
146147
}
147148

@@ -221,6 +222,7 @@ impl TypeRef {
221222
}
222223
}
223224
TypeRef::Path(path) => go_path(path, f, map),
225+
TypeRef::PatternType(ty, _) => go(*ty, f, map),
224226
TypeRef::Never | TypeRef::Placeholder | TypeRef::Error | TypeRef::TypeParam(_) => {}
225227
};
226228
}

crates/hir-def/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,12 @@ impl From<VariantId> for ExpressionStoreOwnerId {
891891
}
892892
}
893893

894+
impl From<ImplId> for ExpressionStoreOwnerId {
895+
fn from(id: ImplId) -> Self {
896+
ExpressionStoreOwnerId::Signature(id.into())
897+
}
898+
}
899+
894900
impl GenericDefId {
895901
pub fn file_id_and_params_of(
896902
self,

crates/hir-expand/src/builtin/derive_macro.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,6 +1462,9 @@ fn coerce_pointee_expand(
14621462
ast::Type::TupleType(ty) => {
14631463
any_long(ty.fields(), |ty| substitute_type_in_bound(ty, param_name, replacement))
14641464
}
1465+
ast::Type::PatternType(ty) => {
1466+
ty.ty().is_some_and(|ty| substitute_type_in_bound(ty, param_name, replacement))
1467+
}
14651468
ast::Type::InferType(_) | ast::Type::MacroType(_) | ast::Type::NeverType(_) => false,
14661469
};
14671470

crates/hir-expand/src/builtin/fn_macro.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ register_builtin! {
137137
(const_format_args, ConstFormatArgs) => format_args_expand,
138138
(format_args_nl, FormatArgsNl) => format_args_nl_expand,
139139
(quote, Quote) => quote_expand,
140+
(pattern_type, PatternType) => pattern_type_expand,
140141

141142
EagerExpander:
142143
(compile_error, CompileError) => compile_error_expand,
@@ -994,3 +995,14 @@ fn unescape_str(s: &str) -> Cow<'_, str> {
994995
Cow::Borrowed(s)
995996
}
996997
}
998+
999+
fn pattern_type_expand(
1000+
_db: &dyn ExpandDatabase,
1001+
_arg_id: MacroCallId,
1002+
tt: &tt::TopSubtree,
1003+
call_site: Span,
1004+
) -> ExpandResult<tt::TopSubtree> {
1005+
let mut tt = tt.clone();
1006+
tt.set_top_subtree_delimiter_kind(tt::DelimiterKind::Invisible);
1007+
ExpandResult::ok(quote! {call_site => in #tt })
1008+
}

0 commit comments

Comments
 (0)