@@ -1556,19 +1556,77 @@ where
15561556 }
15571557 }
15581558
1559+ /// Like `try_merge_candidates`, but returns `None` if there are multiple user-written
1560+ /// impls, or a user-written impl and a builtin impl, in which case we bail with ambiguity.
1561+ ///
1562+ /// Candidates with equal responses can still come from conflicting impls, ex - two
1563+ /// overlapping impls which both apply due to impossible where-clauses. We don't want
1564+ /// to merge these. Multiple builtin impls are still merged as usual. This is not
1565+ /// done for marker traits, as their impls are allowed to overlap.
1566+ #[ instrument( level = "trace" , skip( self ) , ret) ]
1567+ fn try_merge_impl_candidates (
1568+ & mut self ,
1569+ trait_def_id : I :: TraitId ,
1570+ candidates : & [ Candidate < I > ] ,
1571+ ) -> Option < CanonicalResponse < I > > {
1572+ let is_marker = self . cx ( ) . trait_is_marker ( trait_def_id) ;
1573+ let all_builtin = candidates
1574+ . iter ( )
1575+ . all ( |candidate| matches ! ( candidate. source, CandidateSource :: BuiltinImpl ( _) ) ) ;
1576+
1577+ if is_marker || all_builtin {
1578+ self . try_merge_candidates ( candidates) . map ( |( response, _) | response)
1579+ } else if candidates. len ( ) > 1 {
1580+ None
1581+ } else {
1582+ candidates. first ( ) . map ( |candidate| candidate. result )
1583+ }
1584+ }
1585+
1586+ fn merge_candidates_or_bail_with_ambiguity (
1587+ & mut self ,
1588+ candidates : & [ Candidate < I > ] ,
1589+ proven_via : TraitGoalProvenVia ,
1590+ ) -> ( CanonicalResponse < I > , Option < TraitGoalProvenVia > ) {
1591+ if let Some ( ( response, _) ) = self . try_merge_candidates ( candidates) {
1592+ ( response, Some ( proven_via) )
1593+ } else {
1594+ ( self . bail_with_ambiguity ( candidates) , None )
1595+ }
1596+ }
1597+
1598+ fn merge_impl_candidates_or_bail_with_ambiguity (
1599+ & mut self ,
1600+ trait_def_id : I :: TraitId ,
1601+ candidates : & [ Candidate < I > ] ,
1602+ ) -> ( CanonicalResponse < I > , Option < TraitGoalProvenVia > ) {
1603+ debug_assert ! ( candidates. iter( ) . all( |c| matches!(
1604+ c. source,
1605+ CandidateSource :: Impl ( _) | CandidateSource :: BuiltinImpl ( _)
1606+ ) ) ) ;
1607+ match self . try_merge_impl_candidates ( trait_def_id, candidates) {
1608+ Some ( response) => ( response, Some ( TraitGoalProvenVia :: Misc ) ) ,
1609+ None => ( self . bail_with_ambiguity ( candidates) , None ) ,
1610+ }
1611+ }
1612+
15591613 #[ instrument( level = "debug" , skip( self ) , ret) ]
15601614 pub ( super ) fn merge_trait_candidates (
15611615 & mut self ,
1562- candidate_preference_mode : CandidatePreferenceMode ,
1616+ trait_def_id : I :: TraitId ,
15631617 mut candidates : Vec < Candidate < I > > ,
15641618 failed_candidate_info : FailedCandidateInfo ,
15651619 ) -> Result < ( CanonicalResponse < I > , Option < TraitGoalProvenVia > ) , NoSolution > {
1620+ if candidates. is_empty ( ) {
1621+ return Err ( NoSolution ) ;
1622+ }
1623+
1624+ let candidate_preference_mode = CandidatePreferenceMode :: compute ( self . cx ( ) , trait_def_id) ;
15661625 if self . typing_mode ( ) . is_coherence ( ) {
1567- return if let Some ( ( response, _) ) = self . try_merge_candidates ( & candidates) {
1568- Ok ( ( response, Some ( TraitGoalProvenVia :: Misc ) ) )
1569- } else {
1570- self . flounder ( & candidates) . map ( |r| ( r, None ) )
1571- } ;
1626+ return Ok ( match self . try_merge_impl_candidates ( trait_def_id, & candidates) {
1627+ Some ( response) => ( response, Some ( TraitGoalProvenVia :: Misc ) ) ,
1628+ None => ( self . bail_with_ambiguity ( & candidates) , None ) ,
1629+ } ) ;
15721630 }
15731631
15741632 // We prefer trivial builtin candidates, i.e. builtin impls without any
@@ -1595,11 +1653,10 @@ where
15951653 let alias_bounds: Vec < _ > = candidates
15961654 . extract_if ( .., |c| matches ! ( c. source, CandidateSource :: AliasBound ( ..) ) )
15971655 . collect ( ) ;
1598- return if let Some ( ( response, _) ) = self . try_merge_candidates ( & alias_bounds) {
1599- Ok ( ( response, Some ( TraitGoalProvenVia :: AliasBound ) ) )
1600- } else {
1601- Ok ( ( self . bail_with_ambiguity ( & alias_bounds) , None ) )
1602- } ;
1656+ return Ok ( self . merge_candidates_or_bail_with_ambiguity (
1657+ & alias_bounds,
1658+ TraitGoalProvenVia :: AliasBound ,
1659+ ) ) ;
16031660 }
16041661
16051662 // If there are non-global where-bounds, prefer where-bounds
@@ -1651,11 +1708,10 @@ where
16511708 let alias_bounds: Vec < _ > = candidates
16521709 . extract_if ( .., |c| matches ! ( c. source, CandidateSource :: AliasBound ( _) ) )
16531710 . collect ( ) ;
1654- return if let Some ( ( response, _) ) = self . try_merge_candidates ( & alias_bounds) {
1655- Ok ( ( response, Some ( TraitGoalProvenVia :: AliasBound ) ) )
1656- } else {
1657- Ok ( ( self . bail_with_ambiguity ( & alias_bounds) , None ) )
1658- } ;
1711+ return Ok ( self . merge_candidates_or_bail_with_ambiguity (
1712+ & alias_bounds,
1713+ TraitGoalProvenVia :: AliasBound ,
1714+ ) ) ;
16591715 }
16601716
16611717 self . filter_specialized_impls ( AllowInferenceConstraints :: No , & mut candidates) ;
@@ -1665,21 +1721,16 @@ where
16651721 // is still reported as being proven-via the param-env so that rigid projections
16661722 // operate correctly. Otherwise, drop all global where-bounds before merging the
16671723 // remaining candidates.
1668- let proven_via = if candidates
1724+ let only_global_where_bounds = candidates
16691725 . iter ( )
1670- . all ( |c| matches ! ( c. source, CandidateSource :: ParamEnv ( ParamEnvSource :: Global ) ) )
1671- {
1672- TraitGoalProvenVia :: ParamEnv
1726+ . all ( |c| matches ! ( c. source, CandidateSource :: ParamEnv ( ParamEnvSource :: Global ) ) ) ;
1727+ if only_global_where_bounds {
1728+ Ok ( self
1729+ . merge_candidates_or_bail_with_ambiguity ( & candidates, TraitGoalProvenVia :: ParamEnv ) )
16731730 } else {
16741731 candidates
16751732 . retain ( |c| !matches ! ( c. source, CandidateSource :: ParamEnv ( ParamEnvSource :: Global ) ) ) ;
1676- TraitGoalProvenVia :: Misc
1677- } ;
1678-
1679- if let Some ( ( response, _) ) = self . try_merge_candidates ( & candidates) {
1680- Ok ( ( response, Some ( proven_via) ) )
1681- } else {
1682- self . flounder ( & candidates) . map ( |r| ( r, None ) )
1733+ Ok ( self . merge_impl_candidates_or_bail_with_ambiguity ( trait_def_id, & candidates) )
16831734 }
16841735 }
16851736
@@ -1691,9 +1742,7 @@ where
16911742 {
16921743 let ( candidates, failed_candidate_info) =
16931744 self . assemble_and_evaluate_candidates ( goal, AssembleCandidatesFrom :: All ) ?;
1694- let candidate_preference_mode =
1695- CandidatePreferenceMode :: compute ( self . cx ( ) , goal. predicate . def_id ( ) ) ;
1696- self . merge_trait_candidates ( candidate_preference_mode, candidates, failed_candidate_info)
1745+ self . merge_trait_candidates ( goal. predicate . def_id ( ) , candidates, failed_candidate_info)
16971746 . map_err ( Into :: into)
16981747 }
16991748
0 commit comments