1616pub ( crate ) mod cast;
1717pub ( crate ) mod closure;
1818mod coerce;
19- mod diagnostics;
19+ pub ( crate ) mod diagnostics;
2020mod expr;
2121mod mutability;
2222mod pat;
@@ -1480,21 +1480,22 @@ impl<'a> InferenceContext<'a> {
14801480 & self . diagnostics ,
14811481 InferenceTyDiagnosticSource :: Body ,
14821482 ) ;
1483+ let mut path_ctx = ctx. at_path ( path, node) ;
14831484 let ( resolution, unresolved) = if value_ns {
1484- let Some ( res) = ctx . resolve_path_in_value_ns ( path , node , HygieneId :: ROOT ) else {
1485+ let Some ( res) = path_ctx . resolve_path_in_value_ns ( HygieneId :: ROOT ) else {
14851486 return ( self . err_ty ( ) , None ) ;
14861487 } ;
14871488 match res {
14881489 ResolveValueResult :: ValueNs ( value, _) => match value {
14891490 ValueNs :: EnumVariantId ( var) => {
1490- let substs = ctx . substs_from_path ( path , var. into ( ) , true ) ;
1491+ let substs = path_ctx . substs_from_path ( var. into ( ) , true ) ;
14911492 drop ( ctx) ;
14921493 let ty = self . db . ty ( var. lookup ( self . db . upcast ( ) ) . parent . into ( ) ) ;
14931494 let ty = self . insert_type_vars ( ty. substitute ( Interner , & substs) ) ;
14941495 return ( ty, Some ( var. into ( ) ) ) ;
14951496 }
14961497 ValueNs :: StructId ( strukt) => {
1497- let substs = ctx . substs_from_path ( path , strukt. into ( ) , true ) ;
1498+ let substs = path_ctx . substs_from_path ( strukt. into ( ) , true ) ;
14981499 drop ( ctx) ;
14991500 let ty = self . db . ty ( strukt. into ( ) ) ;
15001501 let ty = self . insert_type_vars ( ty. substitute ( Interner , & substs) ) ;
@@ -1509,7 +1510,7 @@ impl<'a> InferenceContext<'a> {
15091510 ResolveValueResult :: Partial ( typens, unresolved, _) => ( typens, Some ( unresolved) ) ,
15101511 }
15111512 } else {
1512- match ctx . resolve_path_in_type_ns ( path , node ) {
1513+ match path_ctx . resolve_path_in_type_ns ( ) {
15131514 Some ( ( it, idx) ) => ( it, idx) ,
15141515 None => return ( self . err_ty ( ) , None ) ,
15151516 }
@@ -1520,21 +1521,21 @@ impl<'a> InferenceContext<'a> {
15201521 } ;
15211522 return match resolution {
15221523 TypeNs :: AdtId ( AdtId :: StructId ( strukt) ) => {
1523- let substs = ctx . substs_from_path ( path , strukt. into ( ) , true ) ;
1524+ let substs = path_ctx . substs_from_path ( strukt. into ( ) , true ) ;
15241525 drop ( ctx) ;
15251526 let ty = self . db . ty ( strukt. into ( ) ) ;
15261527 let ty = self . insert_type_vars ( ty. substitute ( Interner , & substs) ) ;
15271528 forbid_unresolved_segments ( ( ty, Some ( strukt. into ( ) ) ) , unresolved)
15281529 }
15291530 TypeNs :: AdtId ( AdtId :: UnionId ( u) ) => {
1530- let substs = ctx . substs_from_path ( path , u. into ( ) , true ) ;
1531+ let substs = path_ctx . substs_from_path ( u. into ( ) , true ) ;
15311532 drop ( ctx) ;
15321533 let ty = self . db . ty ( u. into ( ) ) ;
15331534 let ty = self . insert_type_vars ( ty. substitute ( Interner , & substs) ) ;
15341535 forbid_unresolved_segments ( ( ty, Some ( u. into ( ) ) ) , unresolved)
15351536 }
15361537 TypeNs :: EnumVariantId ( var) => {
1537- let substs = ctx . substs_from_path ( path , var. into ( ) , true ) ;
1538+ let substs = path_ctx . substs_from_path ( var. into ( ) , true ) ;
15381539 drop ( ctx) ;
15391540 let ty = self . db . ty ( var. lookup ( self . db . upcast ( ) ) . parent . into ( ) ) ;
15401541 let ty = self . insert_type_vars ( ty. substitute ( Interner , & substs) ) ;
@@ -1545,31 +1546,32 @@ impl<'a> InferenceContext<'a> {
15451546 let substs = generics. placeholder_subst ( self . db ) ;
15461547 let mut ty = self . db . impl_self_ty ( impl_id) . substitute ( Interner , & substs) ;
15471548
1548- let Some ( mut remaining_idx) = unresolved else {
1549+ let Some ( remaining_idx) = unresolved else {
15491550 drop ( ctx) ;
15501551 return self . resolve_variant_on_alias ( ty, None , mod_path) ;
15511552 } ;
15521553
15531554 let mut remaining_segments = path. segments ( ) . skip ( remaining_idx) ;
15541555
1556+ if remaining_segments. len ( ) >= 2 {
1557+ path_ctx. ignore_last_segment ( ) ;
1558+ }
1559+
15551560 // We need to try resolving unresolved segments one by one because each may resolve
15561561 // to a projection, which `TyLoweringContext` cannot handle on its own.
15571562 let mut tried_resolving_once = false ;
1558- while !remaining_segments. is_empty ( ) {
1559- let resolved_segment = path. segments ( ) . get ( remaining_idx - 1 ) . unwrap ( ) ;
1560- let current_segment = remaining_segments. take ( 1 ) ;
1561-
1563+ while let Some ( current_segment) = remaining_segments. first ( ) {
15621564 // If we can resolve to an enum variant, it takes priority over associated type
15631565 // of the same name.
15641566 if let Some ( ( AdtId :: EnumId ( id) , _) ) = ty. as_adt ( ) {
15651567 let enum_data = self . db . enum_data ( id) ;
1566- let name = current_segment. first ( ) . unwrap ( ) . name ;
1567- if let Some ( variant) = enum_data. variant ( name) {
1568+ if let Some ( variant) = enum_data. variant ( current_segment. name ) {
15681569 return if remaining_segments. len ( ) == 1 {
15691570 ( ty, Some ( variant. into ( ) ) )
15701571 } else {
15711572 // We still have unresolved paths, but enum variants never have
15721573 // associated types!
1574+ // FIXME: Report an error.
15731575 ( self . err_ty ( ) , None )
15741576 } ;
15751577 }
@@ -1578,23 +1580,13 @@ impl<'a> InferenceContext<'a> {
15781580 if tried_resolving_once {
15791581 // FIXME: with `inherent_associated_types` this is allowed, but our `lower_partly_resolved_path()`
15801582 // will need to be updated to err at the correct segment.
1581- //
1582- // We need to stop here because otherwise the segment index passed to `lower_partly_resolved_path()`
1583- // will be incorrect, and that can mess up error reporting.
15841583 break ;
15851584 }
15861585
15871586 // `lower_partly_resolved_path()` returns `None` as type namespace unless
15881587 // `remaining_segments` is empty, which is never the case here. We don't know
15891588 // which namespace the new `ty` is in until normalized anyway.
1590- ( ty, _) = ctx. lower_partly_resolved_path (
1591- node,
1592- resolution,
1593- resolved_segment,
1594- current_segment,
1595- ( remaining_idx - 1 ) as u32 ,
1596- false ,
1597- ) ;
1589+ ( ty, _) = path_ctx. lower_partly_resolved_path ( resolution, false ) ;
15981590 tried_resolving_once = true ;
15991591
16001592 ty = self . table . insert_type_vars ( ty) ;
@@ -1604,8 +1596,6 @@ impl<'a> InferenceContext<'a> {
16041596 return ( self . err_ty ( ) , None ) ;
16051597 }
16061598
1607- // FIXME(inherent_associated_types): update `resolution` based on `ty` here.
1608- remaining_idx += 1 ;
16091599 remaining_segments = remaining_segments. skip ( 1 ) ;
16101600 }
16111601 drop ( ctx) ;
@@ -1621,11 +1611,7 @@ impl<'a> InferenceContext<'a> {
16211611 ( ty, variant)
16221612 }
16231613 TypeNs :: TypeAliasId ( it) => {
1624- let resolved_seg = match unresolved {
1625- None => path. segments ( ) . last ( ) . unwrap ( ) ,
1626- Some ( n) => path. segments ( ) . get ( path. segments ( ) . len ( ) - n - 1 ) . unwrap ( ) ,
1627- } ;
1628- let substs = ctx. substs_from_path_segment ( resolved_seg, it. into ( ) , true , None ) ;
1614+ let substs = path_ctx. substs_from_path_segment ( it. into ( ) , true , None ) ;
16291615 drop ( ctx) ;
16301616 let ty = self . db . ty ( it. into ( ) ) ;
16311617 let ty = self . insert_type_vars ( ty. substitute ( Interner , & substs) ) ;
0 commit comments