@@ -645,11 +645,20 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
645645 return None ;
646646 }
647647
648+ // preemptively look for ambiguities for panic macros to ensure we don't
649+ // speculatively resolve to a non-prelude item when an ambiguity that we'd
650+ // downgrade is present
651+ let is_issue_147319_hack = || ctxt. edition ( )
652+ <= Edition :: Edition2024
653+ && matches ! ( orig_ident. name, sym:: panic)
654+ && ( this. is_specific_builtin_macro ( binding. res ( ) , sym:: std_panic) || this. is_specific_builtin_macro ( binding. res ( ) , sym:: core_panic) ) ;
655+ let finalizing = !matches ! ( finalize, None | Some ( Finalize { stage: Stage :: Late , .. } ) ) ;
656+
648657 // Below we report various ambiguity errors.
649658 // We do not need to report them if we are either in speculative resolution,
650659 // or in late resolution when everything is already imported and expanded
651660 // and no ambiguities exist.
652- if matches ! ( finalize , None | Some ( Finalize { stage : Stage :: Late , .. } ) ) {
661+ if !finalizing && ! is_issue_147319_hack ( ) {
653662 return Some ( Ok ( binding) ) ;
654663 }
655664
@@ -720,8 +729,20 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
720729 let is_issue_147319_hack = ctxt. edition ( )
721730 <= Edition :: Edition2024
722731 && matches ! ( orig_ident. name, sym:: panic)
723- && this. is_builtin_macro ( binding. res ( ) )
724- && this. is_builtin_macro ( innermost_binding. res ( ) ) ;
732+ && matches ! ( scope, Scope :: StdLibPrelude )
733+ && ( ( this. is_specific_builtin_macro (
734+ binding. res ( ) ,
735+ sym:: std_panic,
736+ ) && this. is_specific_builtin_macro (
737+ innermost_binding. res ( ) ,
738+ sym:: core_panic,
739+ ) ) || ( this. is_specific_builtin_macro (
740+ binding. res ( ) ,
741+ sym:: core_panic,
742+ ) && this. is_specific_builtin_macro (
743+ innermost_binding. res ( ) ,
744+ sym:: std_panic,
745+ ) ) ) ;
725746
726747 let warning = if is_issue_147319_hack {
727748 Some ( AmbiguityWarning :: PanicImport )
@@ -740,16 +761,22 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
740761 AmbiguityErrorMisc :: None
741762 }
742763 } ;
743- this. get_mut ( ) . ambiguity_errors . push ( AmbiguityError {
744- kind,
745- ident : orig_ident,
746- b1 : innermost_binding,
747- b2 : binding,
748- warning,
749- misc1 : misc ( innermost_flags) ,
750- misc2 : misc ( flags) ,
751- } ) ;
752- return Some ( Ok ( innermost_binding) ) ;
764+ if finalizing {
765+ this. get_mut ( ) . ambiguity_errors . push ( AmbiguityError {
766+ kind,
767+ ident : orig_ident,
768+ b1 : innermost_binding,
769+ b2 : binding,
770+ warning,
771+ misc1 : misc ( innermost_flags) ,
772+ misc2 : misc ( flags) ,
773+ } ) ;
774+ }
775+ if is_issue_147319_hack {
776+ return Some ( Ok ( binding) ) ;
777+ } else {
778+ return Some ( Ok ( innermost_binding) ) ;
779+ }
753780 }
754781 }
755782 } else {
0 commit comments