Skip to content

Commit 90d9508

Browse files
authored
Correctly resolve type relative paths in methods (#1489)
1 parent ea86c6a commit 90d9508

File tree

9 files changed

+52
-40
lines changed

9 files changed

+52
-40
lines changed

crates/flux-desugar/src/desugar.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,10 +1698,7 @@ trait DesugarCtxt<'genv, 'tcx: 'genv>: ErrorEmitter + ErrorCollector<ErrorGuaran
16981698

16991699
impl<'genv, 'tcx> DesugarCtxt<'genv, 'tcx> for RustItemCtxt<'_, 'genv, 'tcx> {
17001700
fn next_fhir_id(&self) -> FhirId {
1701-
FhirId {
1702-
owner: FluxOwnerId::Rust(self.owner.local_id()),
1703-
local_id: self.local_id_gen.fresh(),
1704-
}
1701+
FhirId { owner: FluxOwnerId::Rust(self.owner), local_id: self.local_id_gen.fresh() }
17051702
}
17061703

17071704
fn genv(&self) -> GlobalEnv<'genv, 'tcx> {

crates/flux-desugar/src/desugar/lift.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -533,10 +533,7 @@ impl<'genv> RustItemCtxt<'_, 'genv, '_> {
533533
}
534534

535535
fn next_fhir_id(&self) -> FhirId {
536-
FhirId {
537-
owner: FluxOwnerId::Rust(self.owner.local_id()),
538-
local_id: self.local_id_gen.fresh(),
539-
}
536+
FhirId { owner: FluxOwnerId::Rust(self.owner), local_id: self.local_id_gen.fresh() }
540537
}
541538

542539
fn local_id(&self) -> LocalDefId {

crates/flux-fhir-analysis/src/conv/mod.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ pub(crate) fn conv_adt_sort_def(
295295
def_id: MaybeExternId,
296296
kind: &fhir::RefinementKind,
297297
) -> QueryResult<rty::AdtSortDef> {
298-
let wfckresults = &WfckResults::new(OwnerId { def_id: def_id.local_id() });
298+
let wfckresults = &WfckResults::new(def_id.map(|def_id| OwnerId { def_id }));
299299
let mut cx = AfterSortck::new(genv, wfckresults).into_conv_ctxt();
300300
match kind {
301301
fhir::RefinementKind::Refined(refined_by) => {
@@ -1502,7 +1502,8 @@ impl<'genv, 'tcx: 'genv, P: ConvPhase<'genv, 'tcx>> ConvCtxt<P> {
15021502
}
15031503
fhir::Res::Def(DefKind::TyParam, param_id)
15041504
| fhir::Res::SelfTyParam { trait_: param_id } => {
1505-
let predicates = type_param_predicates(tcx, param_id);
1505+
let item_def_id = self.owner().resolved_id().unwrap();
1506+
let predicates = type_param_predicates(tcx, item_def_id, param_id);
15061507
self.probe_single_bound_for_assoc_item(
15071508
|| {
15081509
tag.transitive_bounds_that_define_assoc_item(
@@ -1598,8 +1599,7 @@ impl<'genv, 'tcx: 'genv, P: ConvPhase<'genv, 'tcx>> ConvCtxt<P> {
15981599
fn refiner(&self) -> QueryResult<Refiner<'genv, 'tcx>> {
15991600
match self.owner() {
16001601
FluxOwnerId::Rust(owner_id) => {
1601-
let def_id = self.genv().maybe_extern_id(owner_id.def_id);
1602-
Refiner::default_for_item(self.genv(), def_id.resolved_id())
1602+
Refiner::default_for_item(self.genv(), owner_id.resolved_id())
16031603
}
16041604
FluxOwnerId::Flux(_) => Err(query_bug!("cannot refine types insicde flux item")),
16051605
}
@@ -2843,19 +2843,14 @@ impl AssocItemTag for AssocReftTag {
28432843
/// problem for us so we can use it instead of [`TyCtxt::type_param_predicates`].
28442844
fn type_param_predicates<'tcx>(
28452845
tcx: TyCtxt<'tcx>,
2846+
item_def_id: DefId,
28462847
param_id: DefId,
28472848
) -> impl Iterator<Item = ty::PolyTraitPredicate<'tcx>> {
2848-
let parent = if tcx.def_kind(param_id) == DefKind::Trait {
2849-
// If the param_id is a trait then this is the `Self` parameter and the parent is the trait itself
2850-
param_id
2851-
} else {
2852-
tcx.parent(param_id)
2853-
};
28542849
let param_index = tcx
2855-
.generics_of(parent)
2850+
.generics_of(item_def_id)
28562851
.param_def_id_to_index(tcx, param_id)
28572852
.unwrap();
2858-
let predicates = tcx.predicates_of(parent).instantiate_identity(tcx);
2853+
let predicates = tcx.predicates_of(item_def_id).instantiate_identity(tcx);
28592854
predicates.into_iter().filter_map(move |(clause, _)| {
28602855
clause
28612856
.as_trait_clause()

crates/flux-fhir-analysis/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,14 @@ fn adt_def(genv: GlobalEnv, def_id: MaybeExternId) -> QueryResult<rty::AdtDef> {
202202

203203
fn constant_info(genv: GlobalEnv, def_id: MaybeExternId) -> QueryResult<rty::ConstantInfo> {
204204
let node = genv.fhir_node(def_id.local_id())?;
205-
let owner = rustc_hir::OwnerId { def_id: def_id.local_id() };
206205
let Some(sort) = genv.sort_of_def_id(def_id.resolved_id()).emit(&genv)? else {
207206
return Ok(rty::ConstantInfo::Uninterpreted);
208207
};
209208
let tcx = genv.tcx();
210209
match node {
211210
fhir::Node::Item(fhir::Item { kind: fhir::ItemKind::Const(Some(expr)), .. }) => {
212211
// If the constant has a `#[consant(expr)]` annotation we use that
212+
let owner = def_id.map(|def_id| rustc_hir::OwnerId { def_id });
213213
let wfckresults = wf::check_constant_expr(genv, owner, expr, &sort)?;
214214
let expr = AfterSortck::new(genv, &wfckresults)
215215
.into_conv_ctxt()
@@ -377,7 +377,7 @@ fn sort_of_assoc_reft(
377377
match &genv.fhir_expect_item(container_id.local_id())?.kind {
378378
fhir::ItemKind::Trait(trait_) => {
379379
let assoc_reft = trait_.find_assoc_reft(assoc_id.name()).unwrap();
380-
let wfckresults = WfckResults::new(OwnerId { def_id: container_id.local_id() });
380+
let wfckresults = WfckResults::new(container_id.map(|def_id| OwnerId { def_id }));
381381
let mut cx = AfterSortck::new(genv, &wfckresults).into_conv_ctxt();
382382
let inputs = assoc_reft
383383
.params
@@ -389,7 +389,7 @@ fn sort_of_assoc_reft(
389389
}
390390
fhir::ItemKind::Impl(impl_) => {
391391
let assoc_reft = impl_.find_assoc_reft(assoc_id.name()).unwrap();
392-
let wfckresults = WfckResults::new(OwnerId { def_id: container_id.local_id() });
392+
let wfckresults = WfckResults::new(container_id.map(|def_id| OwnerId { def_id }));
393393
let mut cx = AfterSortck::new(genv, &wfckresults).into_conv_ctxt();
394394
let inputs = assoc_reft
395395
.params

crates/flux-fhir-analysis/src/wf/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub(crate) fn check_flux_item<'genv>(
4848

4949
pub(crate) fn check_constant_expr<'genv>(
5050
genv: GlobalEnv<'genv, '_>,
51-
owner: OwnerId,
51+
owner: MaybeExternId<OwnerId>,
5252
expr: &fhir::Expr<'genv>,
5353
sort: &rty::Sort,
5454
) -> Result<WfckResults> {
@@ -70,7 +70,7 @@ pub(crate) fn check_invariants<'genv>(
7070
params: &[fhir::RefineParam<'genv>],
7171
invariants: &[fhir::Expr<'genv>],
7272
) -> Result<WfckResults> {
73-
let owner = FluxOwnerId::Rust(adt_def_id.local_id());
73+
let owner = FluxOwnerId::Rust(adt_def_id);
7474
let mut infcx = InferCtxt::new(genv, owner);
7575
Wf::with(&mut infcx, |wf| {
7676
wf.declare_params_for_invariants(params, invariants)?;
@@ -94,7 +94,7 @@ pub(crate) fn check_node<'genv>(
9494
genv: GlobalEnv<'genv, '_>,
9595
node: &fhir::OwnerNode<'genv>,
9696
) -> Result<WfckResults> {
97-
let mut infcx = InferCtxt::new(genv, node.owner_id().local_id().into());
97+
let mut infcx = InferCtxt::new(genv, node.owner_id().into());
9898
Wf::with(&mut infcx, |wf| {
9999
wf.init_infcx_for_node(node)
100100
.map_err(|err| err.at(genv.tcx().def_span(node.owner_id().local_id())))

crates/flux-fhir-analysis/src/wf/sortck.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -581,8 +581,7 @@ impl<'genv, 'tcx> InferCtxt<'genv, 'tcx> {
581581
.infer_ctxt()
582582
.with_next_trait_solver(true)
583583
.build(TypingMode::non_body_analysis());
584-
if let Some(def_id) = owner.def_id() {
585-
let def_id = genv.maybe_extern_id(def_id).resolved_id();
584+
if let Some(def_id) = owner.resolved_id() {
586585
t.deeply_normalize_sorts(def_id, genv, &infcx)
587586
} else {
588587
Ok(t.clone())
@@ -801,12 +800,14 @@ impl<'genv> InferCtxt<'genv, '_> {
801800
.insert(node.fhir_id, sort);
802801
}
803802

804-
let allow_uninterpreted_cast = self
805-
.owner
806-
.def_id()
807-
.map_or_else(flux_config::allow_uninterpreted_cast, |def_id| {
808-
self.genv.infer_opts(def_id).allow_uninterpreted_cast
809-
});
803+
let allow_uninterpreted_cast =
804+
self.owner
805+
.as_rust()
806+
.map_or_else(flux_config::allow_uninterpreted_cast, |def_id| {
807+
self.genv
808+
.infer_opts(def_id.local_id().def_id)
809+
.allow_uninterpreted_cast
810+
});
810811

811812
// Make sure that function applications are fully resolved
812813
for (node, sort_args) in std::mem::take(&mut self.sort_args_of_app) {

crates/flux-middle/src/def_id.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl rustc_middle::query::IntoQueryParam<FluxDefId> for FluxLocalDefId {
110110
/// [_local id_]: MaybeExternId::local_id
111111
/// [_resolved id_]: MaybeExternId::resolved_id
112112
/// [`GlobalEnv::maybe_extern_id`]: crate::global_env::GlobalEnv::maybe_extern_id
113-
#[derive(Clone, Copy, Debug)]
113+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, Encodable, Decodable)]
114114
pub enum MaybeExternId<Id = LocalDefId> {
115115
/// An id for a local spec.
116116
Local(Id),

crates/flux-middle/src/fhir.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -591,16 +591,20 @@ pub enum Lifetime {
591591
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, Encodable, Decodable)]
592592
pub enum FluxOwnerId {
593593
Flux(FluxLocalDefId),
594-
Rust(OwnerId),
594+
Rust(MaybeExternId<OwnerId>),
595595
}
596596

597597
impl FluxOwnerId {
598-
pub fn def_id(self) -> Option<LocalDefId> {
598+
pub fn as_rust(self) -> Option<MaybeExternId<OwnerId>> {
599599
match self {
600600
FluxOwnerId::Flux(_) => None,
601-
FluxOwnerId::Rust(owner_id) => Some(owner_id.def_id),
601+
FluxOwnerId::Rust(owner_id) => Some(owner_id),
602602
}
603603
}
604+
605+
pub fn resolved_id(self) -> Option<DefId> {
606+
self.as_rust().map(MaybeExternId::resolved_id)
607+
}
604608
}
605609

606610
/// A unique identifier for a node in the AST. Like [`HirId`] it is composed of an `owner` and a
@@ -1107,8 +1111,8 @@ impl PolyTraitRef<'_> {
11071111
}
11081112
}
11091113

1110-
impl From<OwnerId> for FluxOwnerId {
1111-
fn from(owner_id: OwnerId) -> Self {
1114+
impl From<MaybeExternId<OwnerId>> for FluxOwnerId {
1115+
fn from(owner_id: MaybeExternId<OwnerId>) -> Self {
11121116
FluxOwnerId::Rust(owner_id)
11131117
}
11141118
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Test that we can resolve sasociated type in methods
2+
3+
struct S<T> {
4+
x: T,
5+
}
6+
7+
trait MyTrait {
8+
type Assoc;
9+
}
10+
11+
impl<T> S<T> {
12+
fn test(&self) -> &T::Assoc
13+
where
14+
T: MyTrait,
15+
{
16+
todo!()
17+
}
18+
}

0 commit comments

Comments
 (0)