Skip to content

Commit d664a7a

Browse files
authored
Merge pull request #942 from Y-Nak/func-ty
Func ty
2 parents 4643ced + 055129e commit d664a7a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+2508
-266
lines changed

crates/common2/src/diagnostics.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ pub enum DiagnosticPass {
148148
TraitDefinition,
149149
ImplTraitDefinition,
150150
TraitSatisfaction,
151+
MethodDefinition,
151152

152153
ExternalAnalysis(ExternalAnalysisKey),
153154
}
@@ -161,6 +162,7 @@ impl DiagnosticPass {
161162
Self::TraitDefinition => 4,
162163
Self::ImplTraitDefinition => 5,
163164
Self::TraitSatisfaction => 6,
165+
Self::MethodDefinition => 7,
164166

165167
Self::ExternalAnalysis(_) => std::u16::MAX,
166168
}

crates/driver2/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ use hir::{
1717
};
1818
use hir_analysis::{
1919
name_resolution::{DefConflictAnalysisPass, ImportAnalysisPass, PathAnalysisPass},
20-
ty::{ImplTraitAnalysisPass, TraitAnalysisPass, TypeAliasAnalysisPass, TypeDefAnalysisPass},
20+
ty::{
21+
FuncAnalysisPass, ImplAnalysisPass, ImplTraitAnalysisPass, TraitAnalysisPass,
22+
TypeAliasAnalysisPass, TypeDefAnalysisPass,
23+
},
2124
HirAnalysisDb,
2225
};
2326

@@ -148,6 +151,8 @@ fn initialize_analysis_pass(db: &DriverDataBase) -> AnalysisPassManager<'_> {
148151
pass_manager.add_module_pass(Box::new(TypeDefAnalysisPass::new(db)));
149152
pass_manager.add_module_pass(Box::new(TypeAliasAnalysisPass::new(db)));
150153
pass_manager.add_module_pass(Box::new(TraitAnalysisPass::new(db)));
154+
pass_manager.add_module_pass(Box::new(ImplAnalysisPass::new(db)));
151155
pass_manager.add_module_pass(Box::new(ImplTraitAnalysisPass::new(db)));
156+
pass_manager.add_module_pass(Box::new(FuncAnalysisPass::new(db)));
152157
pass_manager
153158
}

crates/hir-analysis/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ pub struct Jar(
1111
ty::ty_def::TyId,
1212
ty::ty_def::ty_kind,
1313
ty::ty_def::free_inference_keys,
14+
ty::ty_def::collect_type_params,
1415
ty::ty_def::pretty_print_ty,
1516
ty::ty_def::AdtDef,
17+
ty::ty_def::FuncDef,
1618
ty::ty_def::AdtRefId,
1719
/// Type lowering.
1820
ty::ty_lower::lower_hir_ty,
1921
ty::ty_lower::lower_adt,
22+
ty::ty_lower::lower_func,
2023
ty::ty_lower::lower_type_alias,
2124
ty::ty_lower::collect_generic_params,
2225
ty::ty_lower::GenericParamOwnerId,
@@ -25,12 +28,17 @@ pub struct Jar(
2528
ty::trait_lower::lower_trait_ref,
2629
ty::trait_lower::collect_trait_impls,
2730
ty::trait_lower::lower_impl_trait,
31+
ty::trait_lower::collect_implementor_methods,
32+
/// Method collection.
33+
ty::method_table::collect_methods,
2834
/// Item Definition analysis.
2935
ty::def_analysis::check_recursive_adt,
3036
ty::def_analysis::analyze_adt,
3137
ty::def_analysis::analyze_type_alias,
3238
ty::def_analysis::analyze_trait,
39+
ty::def_analysis::analyze_impl,
3340
ty::def_analysis::analyze_impl_trait,
41+
ty::def_analysis::analyze_func,
3442
/// Trait system.
3543
ty::trait_def::TraitDef,
3644
ty::trait_def::TraitInstId,
@@ -41,6 +49,8 @@ pub struct Jar(
4149
ty::constraint::collect_trait_constraints,
4250
ty::constraint::collect_adt_constraints,
4351
ty::constraint::collect_implementor_constraints,
52+
ty::constraint::collect_impl_block_constraints,
53+
ty::constraint::collect_func_def_constraints,
4454
ty::constraint::super_trait_insts,
4555
ty::constraint::compute_super_assumptions,
4656
ty::constraint::ty_constraints,
@@ -55,6 +65,8 @@ pub struct Jar(
5565
ty::diagnostics::TypeAliasDefDiagAccumulator,
5666
ty::diagnostics::TraitDefDiagAccumulator,
5767
ty::diagnostics::ImplTraitDefDiagAccumulator,
68+
ty::diagnostics::ImplDefDiagAccumulator,
69+
ty::diagnostics::FuncDefDiagAccumulator,
5870
);
5971

6072
pub trait HirAnalysisDb: salsa::DbWithJar<Jar> + HirDb {

crates/hir-analysis/src/name_resolution/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,11 @@ impl<'db, 'a> Visitor for EarlyPathVisitor<'db, 'a> {
340340
return;
341341
}
342342

343-
let scope = ScopeId::from_item(item);
344-
// We don't need to check impl/impl-trait blocks for conflicts because they
343+
// We don't need to check impl blocks for conflicts because they
345344
// needs ingot granularity analysis, the conflict checks for them is done by the
346345
// `ImplCollector`.
347-
if !matches!(item, ItemKind::Impl(_) | ItemKind::ImplTrait(_)) {
346+
if !matches!(item, ItemKind::Impl(_)) {
347+
let scope = ScopeId::from_item(item);
348348
self.check_conflict(scope);
349349
}
350350

crates/hir-analysis/src/ty/constraint.rs

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,25 @@
33
44
use std::collections::BTreeSet;
55

6-
use hir::hir_def::{scope_graph::ScopeId, GenericParam, GenericParamOwner, IngotId, TypeBound};
6+
use hir::hir_def::{
7+
scope_graph::ScopeId, GenericParam, GenericParamOwner, Impl, IngotId, ItemKind, TypeBound,
8+
};
79
use rustc_hash::FxHashMap;
810
use salsa::function::Configuration;
911

1012
use crate::{
11-
ty::{trait_lower::lower_trait, unify::InferenceKey},
13+
ty::{
14+
trait_lower::{lower_impl_trait, lower_trait},
15+
unify::InferenceKey,
16+
},
1217
HirAnalysisDb,
1318
};
1419

1520
use super::{
1621
constraint_solver::{is_goal_satisfiable, GoalSatisfiability},
1722
trait_def::{Implementor, TraitDef, TraitInstId},
1823
trait_lower::lower_trait_ref,
19-
ty_def::{AdtDef, Subst, TyConcrete, TyData, TyId},
24+
ty_def::{AdtDef, FuncDef, Subst, TyBase, TyData, TyId},
2025
ty_lower::{collect_generic_params, lower_hir_ty, GenericParamOwnerId},
2126
};
2227

@@ -40,30 +45,36 @@ pub(crate) fn ty_constraints(
4045
assert!(ty.free_inference_keys(db).is_empty());
4146

4247
let (base, args) = ty.decompose_ty_app(db);
43-
let TyData::TyCon(TyConcrete::Adt(adt)) = base.data(db) else {
44-
return (
45-
AssumptionListId::empty_list(db),
46-
ConstraintListId::empty_list(db),
47-
);
48+
let (params, base_constraints) = match base.data(db) {
49+
TyData::TyBase(TyBase::Adt(adt)) => (adt.params(db), collect_adt_constraints(db, adt)),
50+
TyData::TyBase(TyBase::Func(func_def)) => (
51+
func_def.params(db),
52+
collect_func_def_constraints(db, func_def),
53+
),
54+
_ => {
55+
return (
56+
AssumptionListId::empty_list(db),
57+
ConstraintListId::empty_list(db),
58+
);
59+
}
4860
};
4961

5062
let mut subst = FxHashMap::default();
5163
let mut arg_idx = 0;
52-
for (&param, arg) in adt.params(db).iter().zip(args) {
64+
for (&param, arg) in params.iter().zip(args) {
5365
subst.insert(param, arg);
5466
arg_idx += 1;
5567
}
5668

5769
// Generalize unbound type parameters.
58-
for &arg in adt.params(db).iter().skip(arg_idx) {
70+
for &arg in params.iter().skip(arg_idx) {
5971
let key = InferenceKey(arg_idx as u32);
6072
let ty_var = TyId::ty_var(db, arg.kind(db).clone(), key);
6173
subst.insert(arg, ty_var);
6274
arg_idx += 1;
6375
}
6476

65-
// Substitute type parameters.
66-
let constraints = collect_adt_constraints(db, adt).apply_subst(db, &mut subst);
77+
let constraints = base_constraints.apply_subst(db, &mut subst);
6778

6879
// If the predicate type is a type variable, collect it as an assumption
6980
// and remove it from the constraint.
@@ -182,19 +193,56 @@ pub(crate) fn collect_adt_constraints(db: &dyn HirAnalysisDb, adt: AdtDef) -> Co
182193
collector.collect()
183194
}
184195

196+
#[salsa::tracked]
197+
pub(crate) fn collect_impl_block_constraints(
198+
db: &dyn HirAnalysisDb,
199+
impl_: Impl,
200+
) -> ConstraintListId {
201+
let owner = GenericParamOwnerId::new(db, impl_.into());
202+
ConstraintCollector::new(db, owner).collect()
203+
}
204+
185205
/// Collect constraints that are specified by the given implementor(i.e., impl
186206
/// trait).
187207
#[salsa::tracked]
188208
pub(crate) fn collect_implementor_constraints(
189209
db: &dyn HirAnalysisDb,
190210
implementor: Implementor,
191211
) -> ConstraintListId {
192-
let impl_trait = implementor.impl_trait(db);
212+
let impl_trait = implementor.hir_impl_trait(db);
193213
let collector = ConstraintCollector::new(db, GenericParamOwnerId::new(db, impl_trait.into()));
194214

195215
collector.collect()
196216
}
197217

218+
#[salsa::tracked]
219+
pub(crate) fn collect_func_def_constraints(
220+
db: &dyn HirAnalysisDb,
221+
func: FuncDef,
222+
) -> ConstraintListId {
223+
let hir_func = func.hir_func(db);
224+
225+
let func_constraints =
226+
ConstraintCollector::new(db, GenericParamOwnerId::new(db, hir_func.into())).collect();
227+
228+
let parent_constraints = match hir_func.scope().parent_item(db.as_hir_db()) {
229+
Some(ItemKind::Trait(trait_)) => collect_trait_constraints(db, lower_trait(db, trait_)),
230+
231+
Some(ItemKind::Impl(impl_)) => collect_impl_block_constraints(db, impl_),
232+
233+
Some(ItemKind::ImplTrait(impl_trait)) => {
234+
let Some(implementor) = lower_impl_trait(db, impl_trait) else {
235+
return func_constraints;
236+
};
237+
collect_implementor_constraints(db, implementor)
238+
}
239+
240+
_ => return func_constraints,
241+
};
242+
243+
func_constraints.merge(db, parent_constraints)
244+
}
245+
198246
/// Returns a list of assumptions derived from the given assumptions by looking
199247
/// up super traits.
200248
#[salsa::tracked(return_ref)]
@@ -231,6 +279,12 @@ impl PredicateId {
231279
let trait_ = self.trait_inst(db).apply_subst(db, subst);
232280
Self::new(db, ty, trait_)
233281
}
282+
283+
pub fn pretty_print(self, db: &dyn HirAnalysisDb) -> String {
284+
let ty = self.ty(db);
285+
let trait_ = self.trait_inst(db);
286+
format!("{}: {}", ty.pretty_print(db), trait_.pretty_print(db))
287+
}
234288
}
235289

236290
/// The list of predicates.

0 commit comments

Comments
 (0)