Skip to content

Commit e844784

Browse files
committed
Simplify
1 parent d6b908e commit e844784

File tree

11 files changed

+106
-106
lines changed

11 files changed

+106
-106
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub(super) fn print_body_hir(db: &dyn DefDatabase, body: &Body, owner: DefWithBo
5454
let mut p = Printer { db, body, buf: header, indent_level: 0, needs_indent: false };
5555
if let DefWithBodyId::FunctionId(it) = owner {
5656
p.buf.push('(');
57-
body.params.iter().zip(&db.function_data(it).params).for_each(|(&param, ty)| {
57+
body.params.iter().zip(db.function_data(it).params.iter()).for_each(|(&param, ty)| {
5858
p.print_pat(param);
5959
p.buf.push(':');
6060
p.print_type_ref(ty);

crates/hir-def/src/data.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use crate::{
3434
#[derive(Debug, Clone, PartialEq, Eq)]
3535
pub struct FunctionData {
3636
pub name: Name,
37-
pub params: Vec<Interned<TypeRef>>,
37+
pub params: Box<[Interned<TypeRef>]>,
3838
pub ret_type: Interned<TypeRef>,
3939
pub attrs: Attrs,
4040
pub visibility: RawVisibility,
@@ -177,7 +177,7 @@ pub struct TypeAliasData {
177177
pub rustc_has_incoherent_inherent_impls: bool,
178178
pub rustc_allow_incoherent_impl: bool,
179179
/// Bounds restricting the type alias itself (eg. `type Ty: Bound;` in a trait or impl).
180-
pub bounds: Vec<Interned<TypeBound>>,
180+
pub bounds: Box<[Interned<TypeBound>]>,
181181
}
182182

183183
impl TypeAliasData {
@@ -210,7 +210,7 @@ impl TypeAliasData {
210210
is_extern: matches!(loc.container, ItemContainerId::ExternBlockId(_)),
211211
rustc_has_incoherent_inherent_impls,
212212
rustc_allow_incoherent_impl,
213-
bounds: typ.bounds.to_vec(),
213+
bounds: typ.bounds.clone(),
214214
})
215215
}
216216
}

crates/hir-def/src/generics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ impl GenericParams {
227227
let mut expander = Lazy::new(|| {
228228
(module.def_map(db), Expander::new(db, loc.source(db).file_id, module))
229229
});
230-
for param in &func_data.params {
230+
for param in func_data.params.iter() {
231231
generic_params.fill_implicit_impl_trait_args(db, &mut expander, param);
232232
}
233233

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -396,14 +396,7 @@ impl<'a> Ctx<'a> {
396396
let bounds = self.lower_type_bounds(type_alias);
397397
let generic_params = self.lower_generic_params(HasImplicitSelf::No, type_alias);
398398
let ast_id = self.source_ast_id_map.ast_id(type_alias);
399-
let res = TypeAlias {
400-
name,
401-
visibility,
402-
bounds: bounds.into_boxed_slice(),
403-
generic_params,
404-
type_ref,
405-
ast_id,
406-
};
399+
let res = TypeAlias { name, visibility, bounds, generic_params, type_ref, ast_id };
407400
Some(id(self.data().type_aliases.alloc(res)))
408401
}
409402

@@ -637,13 +630,13 @@ impl<'a> Ctx<'a> {
637630
Interned::new(generics)
638631
}
639632

640-
fn lower_type_bounds(&mut self, node: &dyn ast::HasTypeBounds) -> Vec<Interned<TypeBound>> {
633+
fn lower_type_bounds(&mut self, node: &dyn ast::HasTypeBounds) -> Box<[Interned<TypeBound>]> {
641634
match node.type_bound_list() {
642635
Some(bound_list) => bound_list
643636
.bounds()
644637
.map(|it| Interned::new(TypeBound::from_ast(&self.body_ctx, it)))
645638
.collect(),
646-
None => Vec::new(),
639+
None => Box::default(),
647640
}
648641
}
649642

crates/hir-ty/src/db.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ use crate::{
2020
method_resolution::{InherentImpls, TraitImpls, TyFingerprint},
2121
mir::{BorrowckResult, MirBody, MirLowerError},
2222
Binders, CallableDefId, ClosureId, Const, FnDefId, GenericArg, ImplTraitId, InferenceResult,
23-
Interner, PolyFnSig, QuantifiedWhereClause, ReturnTypeImplTraits, Substitution, TraitRef, Ty,
24-
TyDefId, ValueTyDefId,
23+
Interner, PolyFnSig, QuantifiedWhereClause, ReturnTypeImplTraits, Substitution,
24+
TraitEnvironment, TraitRef, Ty, TyDefId, ValueTyDefId,
2525
};
2626
use hir_expand::name::Name;
2727

@@ -47,15 +47,15 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
4747
&self,
4848
def: DefWithBodyId,
4949
subst: Substitution,
50-
env: Arc<crate::TraitEnvironment>,
50+
env: Arc<TraitEnvironment>,
5151
) -> Result<Arc<MirBody>, MirLowerError>;
5252

5353
#[salsa::invoke(crate::mir::monomorphized_mir_body_for_closure_query)]
5454
fn monomorphized_mir_body_for_closure(
5555
&self,
5656
def: ClosureId,
5757
subst: Substitution,
58-
env: Arc<crate::TraitEnvironment>,
58+
env: Arc<TraitEnvironment>,
5959
) -> Result<Arc<MirBody>, MirLowerError>;
6060

6161
#[salsa::invoke(crate::mir::borrowck_query)]
@@ -81,7 +81,7 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
8181
&self,
8282
def: GeneralConstId,
8383
subst: Substitution,
84-
trait_env: Option<Arc<crate::TraitEnvironment>>,
84+
trait_env: Option<Arc<TraitEnvironment>>,
8585
) -> Result<Const, ConstEvalError>;
8686

8787
#[salsa::invoke(crate::consteval::const_eval_static_query)]
@@ -104,24 +104,20 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
104104
&self,
105105
def: AdtId,
106106
subst: Substitution,
107-
env: Arc<crate::TraitEnvironment>,
107+
env: Arc<TraitEnvironment>,
108108
) -> Result<Arc<Layout>, LayoutError>;
109109

110110
#[salsa::invoke(crate::layout::layout_of_ty_query)]
111111
#[salsa::cycle(crate::layout::layout_of_ty_recover)]
112-
fn layout_of_ty(
113-
&self,
114-
ty: Ty,
115-
env: Arc<crate::TraitEnvironment>,
116-
) -> Result<Arc<Layout>, LayoutError>;
112+
fn layout_of_ty(&self, ty: Ty, env: Arc<TraitEnvironment>) -> Result<Arc<Layout>, LayoutError>;
117113

118114
#[salsa::invoke(crate::layout::target_data_layout_query)]
119115
fn target_data_layout(&self, krate: CrateId) -> Option<Arc<TargetDataLayout>>;
120116

121117
#[salsa::invoke(crate::method_resolution::lookup_impl_method_query)]
122118
fn lookup_impl_method(
123119
&self,
124-
env: Arc<crate::TraitEnvironment>,
120+
env: Arc<TraitEnvironment>,
125121
func: FunctionId,
126122
fn_subst: Substitution,
127123
) -> (FunctionId, Substitution);
@@ -149,10 +145,10 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
149145

150146
#[salsa::invoke(crate::lower::trait_environment_for_body_query)]
151147
#[salsa::transparent]
152-
fn trait_environment_for_body(&self, def: DefWithBodyId) -> Arc<crate::TraitEnvironment>;
148+
fn trait_environment_for_body(&self, def: DefWithBodyId) -> Arc<TraitEnvironment>;
153149

154150
#[salsa::invoke(crate::lower::trait_environment_query)]
155-
fn trait_environment(&self, def: GenericDefId) -> Arc<crate::TraitEnvironment>;
151+
fn trait_environment(&self, def: GenericDefId) -> Arc<TraitEnvironment>;
156152

157153
#[salsa::invoke(crate::lower::generic_defaults_query)]
158154
#[salsa::cycle(crate::lower::generic_defaults_recover)]
@@ -249,7 +245,7 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
249245
fn normalize_projection(
250246
&self,
251247
projection: crate::ProjectionTy,
252-
env: Arc<crate::TraitEnvironment>,
248+
env: Arc<TraitEnvironment>,
253249
) -> Ty;
254250

255251
#[salsa::invoke(trait_solve_wait)]

crates/hir-ty/src/lower.rs

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,51 +1383,50 @@ pub(crate) fn generic_predicates_for_param_query(
13831383
let ctx = TyLoweringContext::new(db, &resolver, def.into())
13841384
.with_type_param_mode(ParamLoweringMode::Variable);
13851385
let generics = generics(db.upcast(), def);
1386-
let mut predicates: Vec<_> = resolver
1387-
.where_predicates_in_scope()
1388-
// we have to filter out all other predicates *first*, before attempting to lower them
1389-
.filter(|pred| match pred {
1390-
WherePredicate::ForLifetime { target, bound, .. }
1391-
| WherePredicate::TypeBound { target, bound, .. } => {
1392-
match target {
1393-
WherePredicateTypeTarget::TypeRef(type_ref) => {
1394-
if ctx.lower_ty_only_param(type_ref) != Some(param_id) {
1395-
return false;
1396-
}
1397-
}
1398-
&WherePredicateTypeTarget::TypeOrConstParam(local_id) => {
1399-
let target_id = TypeOrConstParamId { parent: def, local_id };
1400-
if target_id != param_id {
1401-
return false;
1402-
}
1403-
}
1404-
};
14051386

1406-
match &**bound {
1407-
TypeBound::ForLifetime(_, path) | TypeBound::Path(path, _) => {
1408-
// Only lower the bound if the trait could possibly define the associated
1409-
// type we're looking for.
1387+
// we have to filter out all other predicates *first*, before attempting to lower them
1388+
let predicate = |pred: &&_| match pred {
1389+
WherePredicate::ForLifetime { target, bound, .. }
1390+
| WherePredicate::TypeBound { target, bound, .. } => {
1391+
let invalid_target = match target {
1392+
WherePredicateTypeTarget::TypeRef(type_ref) => {
1393+
ctx.lower_ty_only_param(type_ref) != Some(param_id)
1394+
}
1395+
&WherePredicateTypeTarget::TypeOrConstParam(local_id) => {
1396+
let target_id = TypeOrConstParamId { parent: def, local_id };
1397+
target_id != param_id
1398+
}
1399+
};
1400+
if invalid_target {
1401+
return false;
1402+
}
1403+
1404+
match &**bound {
1405+
TypeBound::ForLifetime(_, path) | TypeBound::Path(path, _) => {
1406+
// Only lower the bound if the trait could possibly define the associated
1407+
// type we're looking for.
14101408

1411-
let assoc_name = match &assoc_name {
1412-
Some(it) => it,
1413-
None => return true,
1414-
};
1415-
let tr = match resolver.resolve_path_in_type_ns_fully(db.upcast(), path) {
1416-
Some(TypeNs::TraitId(tr)) => tr,
1417-
_ => return false,
1418-
};
1409+
let Some(assoc_name) = &assoc_name else { return true };
1410+
let Some(TypeNs::TraitId(tr)) =
1411+
resolver.resolve_path_in_type_ns_fully(db.upcast(), path)
1412+
else {
1413+
return false;
1414+
};
14191415

1420-
all_super_traits(db.upcast(), tr).iter().any(|tr| {
1421-
db.trait_data(*tr).items.iter().any(|(name, item)| {
1422-
matches!(item, AssocItemId::TypeAliasId(_)) && name == assoc_name
1423-
})
1416+
all_super_traits(db.upcast(), tr).iter().any(|tr| {
1417+
db.trait_data(*tr).items.iter().any(|(name, item)| {
1418+
matches!(item, AssocItemId::TypeAliasId(_)) && name == assoc_name
14241419
})
1425-
}
1426-
TypeBound::Lifetime(_) | TypeBound::Error => false,
1420+
})
14271421
}
1422+
TypeBound::Lifetime(_) | TypeBound::Error => false,
14281423
}
1429-
WherePredicate::Lifetime { .. } => false,
1430-
})
1424+
}
1425+
WherePredicate::Lifetime { .. } => false,
1426+
};
1427+
let mut predicates: Vec<_> = resolver
1428+
.where_predicates_in_scope()
1429+
.filter(predicate)
14311430
.flat_map(|pred| {
14321431
ctx.lower_where_predicate(pred, true).map(|p| make_binders(db, &generics, p))
14331432
})
@@ -1519,7 +1518,12 @@ pub(crate) fn trait_environment_query(
15191518

15201519
let env = chalk_ir::Environment::new(Interner).add_clauses(Interner, clauses);
15211520

1522-
Arc::new(TraitEnvironment { krate, block: None, traits_from_clauses: traits_in_scope, env })
1521+
Arc::new(TraitEnvironment {
1522+
krate,
1523+
block: None,
1524+
traits_from_clauses: traits_in_scope.into_boxed_slice(),
1525+
env,
1526+
})
15231527
}
15241528

15251529
/// Resolve the where clause(s) of an item with generics.

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

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4439,42 +4439,42 @@ fn test(v: S<i32>) {
44394439
fn associated_type_in_argument() {
44404440
check(
44414441
r#"
4442-
trait A {
4443-
fn m(&self) -> i32;
4444-
}
4442+
trait A {
4443+
fn m(&self) -> i32;
4444+
}
44454445
4446-
fn x<T: B>(k: &<T as B>::Ty) {
4447-
k.m();
4448-
}
4446+
fn x<T: B>(k: &<T as B>::Ty) {
4447+
k.m();
4448+
}
44494449
4450-
struct X;
4451-
struct Y;
4450+
struct X;
4451+
struct Y;
44524452
4453-
impl A for X {
4454-
fn m(&self) -> i32 {
4455-
8
4456-
}
4453+
impl A for X {
4454+
fn m(&self) -> i32 {
4455+
8
44574456
}
4457+
}
44584458
4459-
impl A for Y {
4460-
fn m(&self) -> i32 {
4461-
32
4462-
}
4459+
impl A for Y {
4460+
fn m(&self) -> i32 {
4461+
32
44634462
}
4463+
}
44644464
4465-
trait B {
4466-
type Ty: A;
4467-
}
4465+
trait B {
4466+
type Ty: A;
4467+
}
44684468
4469-
impl B for u16 {
4470-
type Ty = X;
4471-
}
4469+
impl B for u16 {
4470+
type Ty = X;
4471+
}
44724472
4473-
fn ttt() {
4474-
let inp = Y;
4475-
x::<u16>(&inp);
4476-
//^^^^ expected &X, got &Y
4477-
}
4478-
"#,
4473+
fn ttt() {
4474+
let inp = Y;
4475+
x::<u16>(&inp);
4476+
//^^^^ expected &X, got &Y
4477+
}
4478+
"#,
44794479
);
44804480
}

crates/hir-ty/src/traits.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub struct TraitEnvironment {
4848
pub krate: CrateId,
4949
pub block: Option<BlockId>,
5050
// FIXME make this a BTreeMap
51-
pub(crate) traits_from_clauses: Vec<(Ty, TraitId)>,
51+
pub(crate) traits_from_clauses: Box<[(Ty, TraitId)]>,
5252
pub env: chalk_ir::Environment<Interner>,
5353
}
5454

@@ -57,7 +57,7 @@ impl TraitEnvironment {
5757
TraitEnvironment {
5858
krate,
5959
block: None,
60-
traits_from_clauses: Vec::new(),
60+
traits_from_clauses: Box::default(),
6161
env: chalk_ir::Environment::new(Interner),
6262
}
6363
}

crates/hir/src/display.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ impl HirDisplay for TypeAlias {
616616
write_where_clause(def_id, f)?;
617617
if !data.bounds.is_empty() {
618618
f.write_str(": ")?;
619-
f.write_joined(&data.bounds, " + ")?;
619+
f.write_joined(data.bounds.iter(), " + ")?;
620620
}
621621
if let Some(ty) = &data.type_ref {
622622
f.write_str(" = ")?;

crates/ide-diagnostics/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,9 @@ pub fn diagnostics(
301301
)
302302
}));
303303

304-
let parse = sema.parse(file_id);
304+
let parse = parse.syntax_node();
305305

306-
for node in parse.syntax().descendants() {
306+
for node in parse.descendants() {
307307
handlers::useless_braces::useless_braces(&mut res, file_id, &node);
308308
handlers::field_shorthand::field_shorthand(&mut res, file_id, &node);
309309
handlers::json_is_not_rust::json_in_items(&sema, &mut res, file_id, &node, config);
@@ -386,7 +386,7 @@ pub fn diagnostics(
386386

387387
handle_lint_attributes(
388388
&ctx.sema,
389-
parse.syntax(),
389+
&parse,
390390
&mut rustc_stack,
391391
&mut clippy_stack,
392392
&mut diagnostics_of_range,

0 commit comments

Comments
 (0)