Skip to content

Commit c904df0

Browse files
authored
Merge pull request #21059 from ChayimFriedman2/trait-env
fix: Derive ParamEnv from GenericPredicates
2 parents e8e02d5 + 9ceff9f commit c904df0

File tree

5 files changed

+54
-110
lines changed

5 files changed

+54
-110
lines changed

crates/hir-ty/src/generics.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,6 @@ impl Generics {
6060
self.params.where_predicates().iter()
6161
}
6262

63-
pub(crate) fn has_no_predicates(&self) -> bool {
64-
self.params.has_no_predicates()
65-
&& self.parent_generics.as_ref().is_none_or(|g| g.params.has_no_predicates())
66-
}
67-
6863
pub(crate) fn is_empty(&self) -> bool {
6964
self.params.is_empty() && self.parent_generics.as_ref().is_none_or(|g| g.params.is_empty())
7065
}

crates/hir-ty/src/lower.rs

Lines changed: 15 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,105 +1624,23 @@ pub(crate) fn trait_environment_query<'db>(
16241624
db: &'db dyn HirDatabase,
16251625
def: GenericDefId,
16261626
) -> Arc<TraitEnvironment<'db>> {
1627-
let generics = generics(db, def);
1628-
if generics.has_no_predicates() && generics.is_empty() {
1629-
return TraitEnvironment::empty(def.krate(db));
1630-
}
1631-
1632-
let resolver = def.resolver(db);
1633-
let mut ctx = TyLoweringContext::new(
1634-
db,
1635-
&resolver,
1636-
generics.store(),
1637-
def,
1638-
LifetimeElisionKind::AnonymousReportError,
1639-
);
1640-
let mut traits_in_scope = Vec::new();
1641-
let mut clauses = Vec::new();
1642-
for maybe_parent_generics in
1643-
std::iter::successors(Some(&generics), |generics| generics.parent_generics())
1644-
{
1645-
ctx.store = maybe_parent_generics.store();
1646-
for pred in maybe_parent_generics.where_predicates() {
1647-
for pred in ctx.lower_where_predicate(pred, false, &generics, PredicateFilter::All) {
1648-
if let rustc_type_ir::ClauseKind::Trait(tr) = pred.kind().skip_binder() {
1649-
traits_in_scope.push((tr.self_ty(), tr.def_id().0));
1650-
}
1651-
clauses.push(pred);
1652-
}
1653-
}
1654-
1655-
push_const_arg_has_type_predicates(db, &mut clauses, maybe_parent_generics);
1656-
}
1657-
1658-
if let Some(trait_id) = def.assoc_trait_container(db) {
1659-
// add `Self: Trait<T1, T2, ...>` to the environment in trait
1660-
// function default implementations (and speculative code
1661-
// inside consts or type aliases)
1662-
cov_mark::hit!(trait_self_implements_self);
1663-
let trait_ref = TraitRef::identity(ctx.interner, trait_id.into());
1664-
let clause = Clause(Predicate::new(
1665-
ctx.interner,
1666-
Binder::dummy(rustc_type_ir::PredicateKind::Clause(rustc_type_ir::ClauseKind::Trait(
1667-
TraitPredicate { trait_ref, polarity: rustc_type_ir::PredicatePolarity::Positive },
1668-
))),
1669-
));
1670-
clauses.push(clause);
1671-
}
1672-
1673-
let explicitly_unsized_tys = ctx.unsized_types;
1674-
1675-
let sized_trait = LangItem::Sized.resolve_trait(db, resolver.krate());
1676-
if let Some(sized_trait) = sized_trait {
1677-
let (mut generics, mut def_id) =
1678-
(crate::next_solver::generics::generics(db, def.into()), def);
1679-
loop {
1680-
let self_idx = trait_self_param_idx(db, def_id);
1681-
for (idx, p) in generics.own_params.iter().enumerate() {
1682-
if let Some(self_idx) = self_idx
1683-
&& p.index() as usize == self_idx
1684-
{
1685-
continue;
1686-
}
1687-
let GenericParamId::TypeParamId(param_id) = p.id else {
1688-
continue;
1689-
};
1690-
let idx = idx as u32 + generics.parent_count as u32;
1691-
let param_ty = Ty::new_param(ctx.interner, param_id, idx);
1692-
if explicitly_unsized_tys.contains(&param_ty) {
1693-
continue;
1694-
}
1695-
let trait_ref = TraitRef::new_from_args(
1696-
ctx.interner,
1697-
sized_trait.into(),
1698-
GenericArgs::new_from_iter(ctx.interner, [param_ty.into()]),
1699-
);
1700-
let clause = Clause(Predicate::new(
1701-
ctx.interner,
1702-
Binder::dummy(rustc_type_ir::PredicateKind::Clause(
1703-
rustc_type_ir::ClauseKind::Trait(TraitPredicate {
1704-
trait_ref,
1705-
polarity: rustc_type_ir::PredicatePolarity::Positive,
1706-
}),
1707-
)),
1708-
));
1709-
clauses.push(clause);
1710-
}
1711-
1712-
if let Some(g) = generics.parent {
1713-
generics = crate::next_solver::generics::generics(db, g.into());
1714-
def_id = g;
1715-
} else {
1716-
break;
1717-
}
1718-
}
1719-
}
1720-
1721-
let clauses = rustc_type_ir::elaborate::elaborate(ctx.interner, clauses);
1722-
let clauses = Clauses::new_from_iter(ctx.interner, clauses);
1627+
let module = def.module(db);
1628+
let interner = DbInterner::new_with(db, Some(module.krate()), module.containing_block());
1629+
let predicates = GenericPredicates::query_all(db, def);
1630+
let traits_in_scope = predicates
1631+
.iter_identity_copied()
1632+
.filter_map(|pred| match pred.kind().skip_binder() {
1633+
ClauseKind::Trait(tr) => Some((tr.self_ty(), tr.def_id().0)),
1634+
_ => None,
1635+
})
1636+
.collect();
1637+
let clauses = rustc_type_ir::elaborate::elaborate(interner, predicates.iter_identity_copied());
1638+
let clauses = Clauses::new_from_iter(interner, clauses);
17231639
let env = ParamEnv { clauses };
17241640

1725-
TraitEnvironment::new(resolver.krate(), None, traits_in_scope.into_boxed_slice(), env)
1641+
// FIXME: We should normalize projections here, like rustc does.
1642+
1643+
TraitEnvironment::new(module.krate(), module.containing_block(), traits_in_scope, env)
17261644
}
17271645

17281646
#[derive(Copy, Clone, Debug, PartialEq, Eq)]

crates/hir-ty/src/tests/incremental.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@ fn foo() -> i32 {
4444
"body_shim",
4545
"body_with_source_map_shim",
4646
"trait_environment_shim",
47+
"GenericPredicates < 'db >::query_with_diagnostics_",
48+
"lang_item",
49+
"crate_lang_items",
4750
"ImplTraits < 'db >::return_type_impl_traits_",
4851
"expr_scopes_shim",
4952
"lang_item",
50-
"crate_lang_items",
5153
]
5254
"#]],
5355
);
@@ -131,18 +133,21 @@ fn baz() -> i32 {
131133
"body_shim",
132134
"body_with_source_map_shim",
133135
"trait_environment_shim",
134-
"ImplTraits < 'db >::return_type_impl_traits_",
135-
"expr_scopes_shim",
136+
"GenericPredicates < 'db >::query_with_diagnostics_",
136137
"lang_item",
137138
"crate_lang_items",
138139
"attrs_shim",
139140
"attrs_shim",
141+
"ImplTraits < 'db >::return_type_impl_traits_",
142+
"expr_scopes_shim",
143+
"lang_item",
140144
"infer_shim",
141145
"function_signature_shim",
142146
"function_signature_with_source_map_shim",
143147
"body_shim",
144148
"body_with_source_map_shim",
145149
"trait_environment_shim",
150+
"GenericPredicates < 'db >::query_with_diagnostics_",
146151
"ImplTraits < 'db >::return_type_impl_traits_",
147152
"expr_scopes_shim",
148153
"infer_shim",
@@ -151,6 +156,7 @@ fn baz() -> i32 {
151156
"body_shim",
152157
"body_with_source_map_shim",
153158
"trait_environment_shim",
159+
"GenericPredicates < 'db >::query_with_diagnostics_",
154160
"ImplTraits < 'db >::return_type_impl_traits_",
155161
"expr_scopes_shim",
156162
]
@@ -581,6 +587,7 @@ fn main() {
581587
"body_shim",
582588
"body_with_source_map_shim",
583589
"trait_environment_shim",
590+
"GenericPredicates < 'db >::query_with_diagnostics_",
584591
"lang_item",
585592
"crate_lang_items",
586593
"attrs_shim",
@@ -591,6 +598,7 @@ fn main() {
591598
"function_signature_shim",
592599
"function_signature_with_source_map_shim",
593600
"trait_environment_shim",
601+
"GenericPredicates < 'db >::query_with_diagnostics_",
594602
"ImplTraits < 'db >::return_type_impl_traits_",
595603
"expr_scopes_shim",
596604
"struct_signature_shim",
@@ -609,7 +617,6 @@ fn main() {
609617
"impl_trait_with_diagnostics_shim",
610618
"impl_self_ty_with_diagnostics_shim",
611619
"GenericPredicates < 'db >::query_with_diagnostics_",
612-
"GenericPredicates < 'db >::query_with_diagnostics_",
613620
"lang_item",
614621
]
615622
"#]],
@@ -677,7 +684,7 @@ fn main() {
677684
"function_signature_shim",
678685
"body_with_source_map_shim",
679686
"body_shim",
680-
"trait_environment_shim",
687+
"GenericPredicates < 'db >::query_with_diagnostics_",
681688
"crate_lang_items",
682689
"attrs_shim",
683690
"attrs_shim",
@@ -686,6 +693,7 @@ fn main() {
686693
"ImplTraits < 'db >::return_type_impl_traits_",
687694
"infer_shim",
688695
"function_signature_with_source_map_shim",
696+
"GenericPredicates < 'db >::query_with_diagnostics_",
689697
"ImplTraits < 'db >::return_type_impl_traits_",
690698
"expr_scopes_shim",
691699
"struct_signature_with_source_map_shim",
@@ -699,7 +707,6 @@ fn main() {
699707
"impl_trait_with_diagnostics_shim",
700708
"impl_self_ty_with_diagnostics_shim",
701709
"GenericPredicates < 'db >::query_with_diagnostics_",
702-
"GenericPredicates < 'db >::query_with_diagnostics_",
703710
]
704711
"#]],
705712
);

crates/hir-ty/src/tests/traits.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,6 @@ fn test() {
349349

350350
#[test]
351351
fn trait_default_method_self_bound_implements_trait() {
352-
cov_mark::check!(trait_self_implements_self);
353352
check(
354353
r#"
355354
trait Trait {
@@ -5032,3 +5031,28 @@ fn main() {
50325031
"#]],
50335032
);
50345033
}
5034+
5035+
#[test]
5036+
fn implicit_sized_bound_on_param() {
5037+
check(
5038+
r#"
5039+
//- minicore: sized
5040+
struct PBox<T, A>(T, A);
5041+
5042+
impl<T, A> PBox<T, A> {
5043+
fn token_with(self) {}
5044+
}
5045+
5046+
trait MoveMessage {
5047+
fn token<A>(self, alloc: A)
5048+
where
5049+
Self: Sized,
5050+
{
5051+
let b = PBox::<Self, A>(self, alloc);
5052+
b.token_with();
5053+
// ^^^^^^^^^^^^^^ type: ()
5054+
}
5055+
}
5056+
"#,
5057+
);
5058+
}

crates/ide-completion/src/tests/flyimport.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,9 +780,9 @@ fn main() {
780780
}
781781
"#,
782782
expect![[r#"
783-
me random_method(…) (use dep::test_mod::TestTrait) fn(&self) DEPRECATED
784783
ct SPECIAL_CONST (use dep::test_mod::TestTrait) u8 DEPRECATED
785784
fn weird_function() (use dep::test_mod::TestTrait) fn() DEPRECATED
785+
me random_method(…) (use dep::test_mod::TestTrait) fn(&self) DEPRECATED
786786
"#]],
787787
);
788788
}

0 commit comments

Comments
 (0)