Skip to content

Commit 2893153

Browse files
committed
Don't attempt to compute implict sized clauses for empty generics
1 parent 5cbaa3f commit 2893153

File tree

1 file changed

+46
-37
lines changed
  • src/tools/rust-analyzer/crates/hir-ty/src

1 file changed

+46
-37
lines changed

src/tools/rust-analyzer/crates/hir-ty/src/lower.rs

+46-37
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ use intern::Interned;
4848
use la_arena::{Arena, ArenaMap};
4949
use once_cell::unsync::OnceCell;
5050
use rustc_hash::FxHashSet;
51+
use rustc_pattern_analysis::Captures;
5152
use smallvec::SmallVec;
5253
use stdx::{impl_from, never};
5354
use syntax::ast;
@@ -1595,14 +1596,20 @@ pub(crate) fn generic_predicates_for_param_query(
15951596
.collect();
15961597

15971598
let subst = generics.bound_vars_subst(db, DebruijnIndex::INNERMOST);
1598-
let explicitly_unsized_tys = ctx.unsized_types.into_inner();
1599-
if let Some(implicitly_sized_predicates) =
1600-
implicitly_sized_clauses(db, param_id.parent, &explicitly_unsized_tys, &subst, &resolver)
1601-
{
1602-
predicates.extend(
1603-
implicitly_sized_predicates
1604-
.map(|p| make_binders(db, &generics, crate::wrap_empty_binders(p))),
1605-
);
1599+
if !subst.is_empty(Interner) {
1600+
let explicitly_unsized_tys = ctx.unsized_types.into_inner();
1601+
if let Some(implicitly_sized_predicates) = implicitly_sized_clauses(
1602+
db,
1603+
param_id.parent,
1604+
&explicitly_unsized_tys,
1605+
&subst,
1606+
&resolver,
1607+
) {
1608+
predicates.extend(
1609+
implicitly_sized_predicates
1610+
.map(|p| make_binders(db, &generics, crate::wrap_empty_binders(p))),
1611+
);
1612+
};
16061613
}
16071614
predicates.into()
16081615
}
@@ -1665,14 +1672,17 @@ pub(crate) fn trait_environment_query(
16651672
}
16661673

16671674
let subst = generics(db.upcast(), def).placeholder_subst(db);
1668-
let explicitly_unsized_tys = ctx.unsized_types.into_inner();
1669-
if let Some(implicitly_sized_clauses) =
1670-
implicitly_sized_clauses(db, def, &explicitly_unsized_tys, &subst, &resolver)
1671-
{
1672-
clauses.extend(
1673-
implicitly_sized_clauses
1674-
.map(|pred| pred.cast::<ProgramClause>(Interner).into_from_env_clause(Interner)),
1675-
);
1675+
if !subst.is_empty(Interner) {
1676+
let explicitly_unsized_tys = ctx.unsized_types.into_inner();
1677+
if let Some(implicitly_sized_clauses) =
1678+
implicitly_sized_clauses(db, def, &explicitly_unsized_tys, &subst, &resolver)
1679+
{
1680+
clauses.extend(
1681+
implicitly_sized_clauses.map(|pred| {
1682+
pred.cast::<ProgramClause>(Interner).into_from_env_clause(Interner)
1683+
}),
1684+
);
1685+
};
16761686
}
16771687

16781688
let env = chalk_ir::Environment::new(Interner).add_clauses(Interner, clauses);
@@ -1714,27 +1724,29 @@ pub(crate) fn generic_predicates_query(
17141724
.collect::<Vec<_>>();
17151725

17161726
let subst = generics.bound_vars_subst(db, DebruijnIndex::INNERMOST);
1717-
let explicitly_unsized_tys = ctx.unsized_types.into_inner();
1718-
if let Some(implicitly_sized_predicates) =
1719-
implicitly_sized_clauses(db, def, &explicitly_unsized_tys, &subst, &resolver)
1720-
{
1721-
predicates.extend(
1722-
implicitly_sized_predicates
1723-
.map(|p| make_binders(db, &generics, crate::wrap_empty_binders(p))),
1724-
);
1727+
if !subst.is_empty(Interner) {
1728+
let explicitly_unsized_tys = ctx.unsized_types.into_inner();
1729+
if let Some(implicitly_sized_predicates) =
1730+
implicitly_sized_clauses(db, def, &explicitly_unsized_tys, &subst, &resolver)
1731+
{
1732+
predicates.extend(
1733+
implicitly_sized_predicates
1734+
.map(|p| make_binders(db, &generics, crate::wrap_empty_binders(p))),
1735+
);
1736+
};
17251737
}
17261738
GenericPredicates(predicates.is_empty().not().then(|| predicates.into()))
17271739
}
17281740

17291741
/// Generate implicit `: Sized` predicates for all generics that has no `?Sized` bound.
17301742
/// Exception is Self of a trait def.
1731-
fn implicitly_sized_clauses<'a>(
1743+
fn implicitly_sized_clauses<'a, 'subst: 'a>(
17321744
db: &dyn HirDatabase,
17331745
def: GenericDefId,
17341746
explicitly_unsized_tys: &'a FxHashSet<Ty>,
1735-
substitution: &'a Substitution,
1747+
substitution: &'subst Substitution,
17361748
resolver: &Resolver,
1737-
) -> Option<impl Iterator<Item = WhereClause> + 'a> {
1749+
) -> Option<impl Iterator<Item = WhereClause> + Captures<'a> + Captures<'subst>> {
17381750
let is_trait_def = matches!(def, GenericDefId::TraitId(..));
17391751
let generic_args = &substitution.as_slice(Interner)[is_trait_def as usize..];
17401752
let sized_trait = db
@@ -1761,12 +1773,13 @@ pub(crate) fn generic_defaults_query(
17611773
def: GenericDefId,
17621774
) -> Arc<[Binders<crate::GenericArg>]> {
17631775
let resolver = def.resolver(db.upcast());
1764-
let ctx = TyLoweringContext::new(db, &resolver, def.into())
1765-
.with_type_param_mode(ParamLoweringMode::Variable);
17661776
let generic_params = generics(db.upcast(), def);
17671777
let parent_start_idx = generic_params.len_self();
17681778

1769-
let defaults = Arc::from_iter(generic_params.iter().enumerate().map(|(idx, (id, p))| {
1779+
let ctx = TyLoweringContext::new(db, &resolver, def.into())
1780+
.with_impl_trait_mode(ImplTraitLoweringMode::Disallowed)
1781+
.with_type_param_mode(ParamLoweringMode::Variable);
1782+
Arc::from_iter(generic_params.iter().enumerate().map(|(idx, (id, p))| {
17701783
match p {
17711784
GenericParamDataRef::TypeParamData(p) => {
17721785
let mut ty =
@@ -1797,9 +1810,7 @@ pub(crate) fn generic_defaults_query(
17971810
make_binders(db, &generic_params, error_lifetime().cast(Interner))
17981811
}
17991812
}
1800-
}));
1801-
1802-
defaults
1813+
}))
18031814
}
18041815

18051816
pub(crate) fn generic_defaults_recover(
@@ -1810,16 +1821,14 @@ pub(crate) fn generic_defaults_recover(
18101821
let generic_params = generics(db.upcast(), *def);
18111822
// FIXME: this code is not covered in tests.
18121823
// we still need one default per parameter
1813-
let defaults = Arc::from_iter(generic_params.iter_id().map(|id| {
1824+
Arc::from_iter(generic_params.iter_id().map(|id| {
18141825
let val = match id {
18151826
GenericParamId::TypeParamId(_) => TyKind::Error.intern(Interner).cast(Interner),
18161827
GenericParamId::ConstParamId(id) => unknown_const_as_generic(db.const_param_ty(id)),
18171828
GenericParamId::LifetimeParamId(_) => error_lifetime().cast(Interner),
18181829
};
18191830
crate::make_binders(db, &generic_params, val)
1820-
}));
1821-
1822-
defaults
1831+
}))
18231832
}
18241833

18251834
fn fn_sig_for_fn(db: &dyn HirDatabase, def: FunctionId) -> PolyFnSig {

0 commit comments

Comments
 (0)