1616pub ( crate ) mod cast;
1717pub ( crate ) mod closure;
1818mod coerce;
19- mod diagnostics;
19+ pub ( crate ) mod diagnostics;
2020mod expr;
2121mod mutability;
2222mod pat;
@@ -1502,21 +1502,22 @@ impl<'a> InferenceContext<'a> {
15021502 & self . diagnostics ,
15031503 InferenceTyDiagnosticSource :: Body ,
15041504 ) ;
1505+ let mut path_ctx = ctx. at_path ( path, node) ;
15051506 let ( resolution, unresolved) = if value_ns {
1506- let Some ( res) = ctx . resolve_path_in_value_ns ( path , node , HygieneId :: ROOT ) else {
1507+ let Some ( res) = path_ctx . resolve_path_in_value_ns ( HygieneId :: ROOT ) else {
15071508 return ( self . err_ty ( ) , None ) ;
15081509 } ;
15091510 match res {
15101511 ResolveValueResult :: ValueNs ( value, _) => match value {
15111512 ValueNs :: EnumVariantId ( var) => {
1512- let substs = ctx . substs_from_path ( path , var. into ( ) , true ) ;
1513+ let substs = path_ctx . substs_from_path ( var. into ( ) , true ) ;
15131514 drop ( ctx) ;
15141515 let ty = self . db . ty ( var. lookup ( self . db . upcast ( ) ) . parent . into ( ) ) ;
15151516 let ty = self . insert_type_vars ( ty. substitute ( Interner , & substs) ) ;
15161517 return ( ty, Some ( var. into ( ) ) ) ;
15171518 }
15181519 ValueNs :: StructId ( strukt) => {
1519- let substs = ctx . substs_from_path ( path , strukt. into ( ) , true ) ;
1520+ let substs = path_ctx . substs_from_path ( strukt. into ( ) , true ) ;
15201521 drop ( ctx) ;
15211522 let ty = self . db . ty ( strukt. into ( ) ) ;
15221523 let ty = self . insert_type_vars ( ty. substitute ( Interner , & substs) ) ;
@@ -1531,7 +1532,7 @@ impl<'a> InferenceContext<'a> {
15311532 ResolveValueResult :: Partial ( typens, unresolved, _) => ( typens, Some ( unresolved) ) ,
15321533 }
15331534 } else {
1534- match ctx . resolve_path_in_type_ns ( path , node ) {
1535+ match path_ctx . resolve_path_in_type_ns ( ) {
15351536 Some ( ( it, idx) ) => ( it, idx) ,
15361537 None => return ( self . err_ty ( ) , None ) ,
15371538 }
@@ -1542,21 +1543,21 @@ impl<'a> InferenceContext<'a> {
15421543 } ;
15431544 return match resolution {
15441545 TypeNs :: AdtId ( AdtId :: StructId ( strukt) ) => {
1545- let substs = ctx . substs_from_path ( path , strukt. into ( ) , true ) ;
1546+ let substs = path_ctx . substs_from_path ( strukt. into ( ) , true ) ;
15461547 drop ( ctx) ;
15471548 let ty = self . db . ty ( strukt. into ( ) ) ;
15481549 let ty = self . insert_type_vars ( ty. substitute ( Interner , & substs) ) ;
15491550 forbid_unresolved_segments ( ( ty, Some ( strukt. into ( ) ) ) , unresolved)
15501551 }
15511552 TypeNs :: AdtId ( AdtId :: UnionId ( u) ) => {
1552- let substs = ctx . substs_from_path ( path , u. into ( ) , true ) ;
1553+ let substs = path_ctx . substs_from_path ( u. into ( ) , true ) ;
15531554 drop ( ctx) ;
15541555 let ty = self . db . ty ( u. into ( ) ) ;
15551556 let ty = self . insert_type_vars ( ty. substitute ( Interner , & substs) ) ;
15561557 forbid_unresolved_segments ( ( ty, Some ( u. into ( ) ) ) , unresolved)
15571558 }
15581559 TypeNs :: EnumVariantId ( var) => {
1559- let substs = ctx . substs_from_path ( path , var. into ( ) , true ) ;
1560+ let substs = path_ctx . substs_from_path ( var. into ( ) , true ) ;
15601561 drop ( ctx) ;
15611562 let ty = self . db . ty ( var. lookup ( self . db . upcast ( ) ) . parent . into ( ) ) ;
15621563 let ty = self . insert_type_vars ( ty. substitute ( Interner , & substs) ) ;
@@ -1567,31 +1568,32 @@ impl<'a> InferenceContext<'a> {
15671568 let substs = generics. placeholder_subst ( self . db ) ;
15681569 let mut ty = self . db . impl_self_ty ( impl_id) . substitute ( Interner , & substs) ;
15691570
1570- let Some ( mut remaining_idx) = unresolved else {
1571+ let Some ( remaining_idx) = unresolved else {
15711572 drop ( ctx) ;
15721573 return self . resolve_variant_on_alias ( ty, None , mod_path) ;
15731574 } ;
15741575
15751576 let mut remaining_segments = path. segments ( ) . skip ( remaining_idx) ;
15761577
1578+ if remaining_segments. len ( ) >= 2 {
1579+ path_ctx. ignore_last_segment ( ) ;
1580+ }
1581+
15771582 // We need to try resolving unresolved segments one by one because each may resolve
15781583 // to a projection, which `TyLoweringContext` cannot handle on its own.
15791584 let mut tried_resolving_once = false ;
1580- while !remaining_segments. is_empty ( ) {
1581- let resolved_segment = path. segments ( ) . get ( remaining_idx - 1 ) . unwrap ( ) ;
1582- let current_segment = remaining_segments. take ( 1 ) ;
1583-
1585+ while let Some ( current_segment) = remaining_segments. first ( ) {
15841586 // If we can resolve to an enum variant, it takes priority over associated type
15851587 // of the same name.
15861588 if let Some ( ( AdtId :: EnumId ( id) , _) ) = ty. as_adt ( ) {
15871589 let enum_data = self . db . enum_data ( id) ;
1588- let name = current_segment. first ( ) . unwrap ( ) . name ;
1589- if let Some ( variant) = enum_data. variant ( name) {
1590+ if let Some ( variant) = enum_data. variant ( current_segment. name ) {
15901591 return if remaining_segments. len ( ) == 1 {
15911592 ( ty, Some ( variant. into ( ) ) )
15921593 } else {
15931594 // We still have unresolved paths, but enum variants never have
15941595 // associated types!
1596+ // FIXME: Report an error.
15951597 ( self . err_ty ( ) , None )
15961598 } ;
15971599 }
@@ -1600,23 +1602,13 @@ impl<'a> InferenceContext<'a> {
16001602 if tried_resolving_once {
16011603 // FIXME: with `inherent_associated_types` this is allowed, but our `lower_partly_resolved_path()`
16021604 // will need to be updated to err at the correct segment.
1603- //
1604- // We need to stop here because otherwise the segment index passed to `lower_partly_resolved_path()`
1605- // will be incorrect, and that can mess up error reporting.
16061605 break ;
16071606 }
16081607
16091608 // `lower_partly_resolved_path()` returns `None` as type namespace unless
16101609 // `remaining_segments` is empty, which is never the case here. We don't know
16111610 // which namespace the new `ty` is in until normalized anyway.
1612- ( ty, _) = ctx. lower_partly_resolved_path (
1613- node,
1614- resolution,
1615- resolved_segment,
1616- current_segment,
1617- ( remaining_idx - 1 ) as u32 ,
1618- false ,
1619- ) ;
1611+ ( ty, _) = path_ctx. lower_partly_resolved_path ( resolution, false ) ;
16201612 tried_resolving_once = true ;
16211613
16221614 ty = self . table . insert_type_vars ( ty) ;
@@ -1626,8 +1618,6 @@ impl<'a> InferenceContext<'a> {
16261618 return ( self . err_ty ( ) , None ) ;
16271619 }
16281620
1629- // FIXME(inherent_associated_types): update `resolution` based on `ty` here.
1630- remaining_idx += 1 ;
16311621 remaining_segments = remaining_segments. skip ( 1 ) ;
16321622 }
16331623 drop ( ctx) ;
@@ -1643,12 +1633,7 @@ impl<'a> InferenceContext<'a> {
16431633 ( ty, variant)
16441634 }
16451635 TypeNs :: TypeAliasId ( it) => {
1646- let resolved_seg = match unresolved {
1647- None => path. segments ( ) . last ( ) . unwrap ( ) ,
1648- Some ( n) => path. segments ( ) . get ( path. segments ( ) . len ( ) - n - 1 ) . unwrap ( ) ,
1649- } ;
1650- let substs =
1651- ctx. substs_from_path_segment ( resolved_seg, Some ( it. into ( ) ) , true , None ) ;
1636+ let substs = path_ctx. substs_from_path_segment ( it. into ( ) , true , None ) ;
16521637 drop ( ctx) ;
16531638 let ty = self . db . ty ( it. into ( ) ) ;
16541639 let ty = self . insert_type_vars ( ty. substitute ( Interner , & substs) ) ;
0 commit comments