Skip to content

Commit c6d9565

Browse files
bors[bot]SkiFire13
andauthored
Merge #10305
10305: Move `GenericParams`'s handling of `impl Trait` into `fn_data_query` r=flodiebold a=SkiFire13 Fixes #10286 Co-authored-by: Giacomo Stevanato <[email protected]>
2 parents dd21ad6 + 49ba313 commit c6d9565

File tree

6 files changed

+73
-28
lines changed

6 files changed

+73
-28
lines changed

crates/hir_def/src/generics.rs

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,24 @@ use base_db::FileId;
77
use either::Either;
88
use hir_expand::{
99
name::{AsName, Name},
10-
HirFileId, InFile,
10+
ExpandResult, HirFileId, InFile,
1111
};
1212
use la_arena::{Arena, ArenaMap};
13+
use once_cell::unsync::Lazy;
14+
use std::ops::DerefMut;
1315
use syntax::ast::{self, HasGenericParams, HasName, HasTypeBounds};
1416

1517
use crate::{
16-
body::LowerCtx,
18+
body::{Expander, LowerCtx},
1719
child_by_source::ChildBySource,
1820
db::DefDatabase,
1921
dyn_map::DynMap,
2022
intern::Interned,
2123
keys,
2224
src::{HasChildSource, HasSource},
2325
type_ref::{LifetimeRef, TypeBound, TypeRef},
24-
AdtId, ConstParamId, GenericDefId, LifetimeParamId, LocalConstParamId, LocalLifetimeParamId,
25-
LocalTypeParamId, Lookup, TypeParamId,
26+
AdtId, ConstParamId, GenericDefId, HasModule, LifetimeParamId, LocalConstParamId,
27+
LocalLifetimeParamId, LocalTypeParamId, Lookup, TypeParamId,
2628
};
2729

2830
/// Data about a generic type parameter (to a function, struct, impl, ...).
@@ -99,10 +101,23 @@ impl GenericParams {
99101

100102
match def {
101103
GenericDefId::FunctionId(id) => {
102-
let id = id.lookup(db).id;
103-
let tree = id.item_tree(db);
104-
let item = &tree[id.value];
105-
item.generic_params.clone()
104+
let loc = id.lookup(db);
105+
let tree = loc.id.item_tree(db);
106+
let item = &tree[loc.id.value];
107+
108+
let mut generic_params = GenericParams::clone(&item.explicit_generic_params);
109+
110+
let module = loc.container.module(db);
111+
let func_data = db.function_data(id);
112+
113+
// Don't create an `Expander` nor call `loc.source(db)` if not needed since this
114+
// causes a reparse after the `ItemTree` has been created.
115+
let mut expander = Lazy::new(|| Expander::new(db, loc.source(db).file_id, module));
116+
for param in &func_data.params {
117+
generic_params.fill_implicit_impl_trait_args(db, &mut expander, param);
118+
}
119+
120+
Interned::new(generic_params)
106121
}
107122
GenericDefId::AdtId(AdtId::StructId(id)) => {
108123
let id = id.lookup(db).id;
@@ -259,7 +274,12 @@ impl GenericParams {
259274
self.where_predicates.push(predicate);
260275
}
261276

262-
pub(crate) fn fill_implicit_impl_trait_args(&mut self, type_ref: &TypeRef) {
277+
pub(crate) fn fill_implicit_impl_trait_args(
278+
&mut self,
279+
db: &dyn DefDatabase,
280+
expander: &mut impl DerefMut<Target = Expander>,
281+
type_ref: &TypeRef,
282+
) {
263283
type_ref.walk(&mut |type_ref| {
264284
if let TypeRef::ImplTrait(bounds) = type_ref {
265285
let param = TypeParamData {
@@ -275,6 +295,18 @@ impl GenericParams {
275295
});
276296
}
277297
}
298+
if let TypeRef::Macro(mc) = type_ref {
299+
let macro_call = mc.to_node(db.upcast());
300+
match expander.enter_expand::<ast::Type>(db, macro_call) {
301+
Ok(ExpandResult { value: Some((mark, expanded)), .. }) => {
302+
let ctx = LowerCtx::new(db, mc.file_id);
303+
let type_ref = TypeRef::from_ast(&ctx, expanded);
304+
self.fill_implicit_impl_trait_args(db, expander, &type_ref);
305+
expander.exit(db, mark);
306+
}
307+
_ => {}
308+
}
309+
}
278310
});
279311
}
280312

crates/hir_def/src/item_tree.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ pub struct ExternBlock {
605605
pub struct Function {
606606
pub name: Name,
607607
pub visibility: RawVisibilityId,
608-
pub generic_params: Interned<GenericParams>,
608+
pub explicit_generic_params: Interned<GenericParams>,
609609
pub abi: Option<Interned<str>>,
610610
pub params: IdRange<Param>,
611611
pub ret_type: Interned<TypeRef>,

crates/hir_def/src/item_tree/lower.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -401,15 +401,16 @@ impl<'a> Ctx<'a> {
401401
let mut res = Function {
402402
name,
403403
visibility,
404-
generic_params: Interned::new(GenericParams::default()),
404+
explicit_generic_params: Interned::new(GenericParams::default()),
405405
abi,
406406
params,
407407
ret_type: Interned::new(ret_type),
408408
async_ret_type: async_ret_type.map(Interned::new),
409409
ast_id,
410410
flags,
411411
};
412-
res.generic_params = self.lower_generic_params(GenericsOwner::Function(&res), func);
412+
res.explicit_generic_params =
413+
self.lower_generic_params(GenericsOwner::Function(&res), func);
413414

414415
Some(id(self.data().functions.alloc(res)))
415416
}
@@ -664,16 +665,8 @@ impl<'a> Ctx<'a> {
664665
) -> Interned<GenericParams> {
665666
let mut generics = GenericParams::default();
666667
match owner {
667-
GenericsOwner::Function(func) => {
668-
generics.fill(&self.body_ctx, node);
669-
// lower `impl Trait` in arguments
670-
for id in func.params.clone() {
671-
if let Param::Normal(ty) = &self.data().params[id] {
672-
generics.fill_implicit_impl_trait_args(ty);
673-
}
674-
}
675-
}
676-
GenericsOwner::Struct
668+
GenericsOwner::Function(_)
669+
| GenericsOwner::Struct
677670
| GenericsOwner::Enum
678671
| GenericsOwner::Union
679672
| GenericsOwner::TypeAlias => {

crates/hir_def/src/item_tree/pretty.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ impl<'a> Printer<'a> {
234234
let Function {
235235
name,
236236
visibility,
237-
generic_params,
237+
explicit_generic_params,
238238
abi,
239239
params,
240240
ret_type,
@@ -250,7 +250,7 @@ impl<'a> Printer<'a> {
250250
w!(self, "extern \"{}\" ", abi);
251251
}
252252
w!(self, "fn {}", name);
253-
self.print_generic_params(generic_params);
253+
self.print_generic_params(explicit_generic_params);
254254
w!(self, "(");
255255
if !params.is_empty() {
256256
self.indented(|this| {
@@ -271,7 +271,7 @@ impl<'a> Printer<'a> {
271271
}
272272
w!(self, ") -> ");
273273
self.print_type_ref(ret_type);
274-
self.print_where_clause(generic_params);
274+
self.print_where_clause(explicit_generic_params);
275275
wln!(self, ";");
276276
}
277277
ModItem::Struct(it) => {

crates/hir_def/src/item_tree/tests.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,12 +340,11 @@ trait Tr<'a, T: 'a>: Super where Self: for<'a> Tr<'a, T> {}
340340
T: 'b
341341
{
342342
// flags = 0x2
343-
pub(self) fn f<G, _anon_1>(
343+
pub(self) fn f<G>(
344344
_: impl Copy,
345345
) -> impl Copy
346346
where
347-
G: 'a,
348-
_anon_1: Copy;
347+
G: 'a;
349348
}
350349
351350
pub(self) enum Enum<'a, T, const U: u8> {

crates/hir_ty/src/tests/regression.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,3 +1177,24 @@ fn multiexp_inner() {
11771177
"#,
11781178
);
11791179
}
1180+
1181+
#[test]
1182+
fn macro_expands_to_impl_trait() {
1183+
check_no_mismatches(
1184+
r#"
1185+
trait Foo {}
1186+
1187+
macro_rules! ty {
1188+
() => {
1189+
impl Foo
1190+
}
1191+
}
1192+
1193+
fn foo(_: ty!()) {}
1194+
1195+
fn bar() {
1196+
foo(());
1197+
}
1198+
"#,
1199+
)
1200+
}

0 commit comments

Comments
 (0)