@@ -245,47 +245,49 @@ pub(super) fn check_item<'tcx>(
245245 // won't be allowed unless there's an *explicit* implementation of `Send`
246246 // for `T`
247247 hir:: ItemKind :: Impl ( impl_) => {
248- let header = tcx. impl_trait_header ( def_id) ;
249- let is_auto = header
250- . is_some_and ( |header| tcx. trait_is_auto ( header. trait_ref . skip_binder ( ) . def_id ) ) ;
251-
252- crate :: impl_wf_check:: check_impl_wf ( tcx, def_id) ?;
248+ crate :: impl_wf_check:: check_impl_wf ( tcx, def_id, impl_. of_trait . is_some ( ) ) ?;
253249 let mut res = Ok ( ( ) ) ;
254- if let ( hir:: Defaultness :: Default { .. } , true ) = ( impl_. defaultness , is_auto) {
255- let sp = impl_. of_trait . as_ref ( ) . map_or ( item. span , |t| t. path . span ) ;
256- res = Err ( tcx
257- . dcx ( )
258- . struct_span_err ( sp, "impls of auto traits cannot be default" )
259- . with_span_labels ( impl_. defaultness_span , "default because of this" )
260- . with_span_label ( sp, "auto trait" )
261- . emit ( ) ) ;
262- }
263- // We match on both `ty::ImplPolarity` and `ast::ImplPolarity` just to get the `!` span.
264- match header. map ( |h| h. polarity ) {
265- // `None` means this is an inherent impl
266- Some ( ty:: ImplPolarity :: Positive ) | None => {
267- res = res. and ( check_impl ( tcx, item, impl_. self_ty , & impl_. of_trait ) ) ;
268- }
269- Some ( ty:: ImplPolarity :: Negative ) => {
270- let ast:: ImplPolarity :: Negative ( span) = impl_. polarity else {
271- bug ! ( "impl_polarity query disagrees with impl's polarity in HIR" ) ;
272- } ;
273- // FIXME(#27579): what amount of WF checking do we need for neg impls?
274- if let hir:: Defaultness :: Default { .. } = impl_. defaultness {
275- let mut spans = vec ! [ span] ;
276- spans. extend ( impl_. defaultness_span ) ;
277- res = Err ( struct_span_code_err ! (
278- tcx. dcx( ) ,
279- spans,
280- E0750 ,
281- "negative impls cannot be default impls"
282- )
250+ if impl_. of_trait . is_some ( ) {
251+ let header = tcx. impl_trait_header ( def_id) ;
252+ let is_auto = tcx. trait_is_auto ( header. trait_ref . skip_binder ( ) . def_id ) ;
253+ if let ( hir:: Defaultness :: Default { .. } , true ) = ( impl_. defaultness , is_auto) {
254+ let sp = impl_. of_trait . as_ref ( ) . map_or ( item. span , |t| t. path . span ) ;
255+ res = Err ( tcx
256+ . dcx ( )
257+ . struct_span_err ( sp, "impls of auto traits cannot be default" )
258+ . with_span_labels ( impl_. defaultness_span , "default because of this" )
259+ . with_span_label ( sp, "auto trait" )
283260 . emit ( ) ) ;
284- }
285261 }
286- Some ( ty:: ImplPolarity :: Reservation ) => {
287- // FIXME: what amount of WF checking do we need for reservation impls?
262+ // We match on both `ty::ImplPolarity` and `ast::ImplPolarity` just to get the `!` span.
263+ match header. polarity {
264+ // `None` means this is an inherent impl
265+ ty:: ImplPolarity :: Positive => {
266+ res = res. and ( check_impl ( tcx, item, impl_. self_ty , & impl_. of_trait ) ) ;
267+ }
268+ ty:: ImplPolarity :: Negative => {
269+ let ast:: ImplPolarity :: Negative ( span) = impl_. polarity else {
270+ bug ! ( "impl_polarity query disagrees with impl's polarity in HIR" ) ;
271+ } ;
272+ // FIXME(#27579): what amount of WF checking do we need for neg impls?
273+ if let hir:: Defaultness :: Default { .. } = impl_. defaultness {
274+ let mut spans = vec ! [ span] ;
275+ spans. extend ( impl_. defaultness_span ) ;
276+ res = Err ( struct_span_code_err ! (
277+ tcx. dcx( ) ,
278+ spans,
279+ E0750 ,
280+ "negative impls cannot be default impls"
281+ )
282+ . emit ( ) ) ;
283+ }
284+ }
285+ ty:: ImplPolarity :: Reservation => {
286+ // FIXME: what amount of WF checking do we need for reservation impls?
287+ }
288288 }
289+ } else {
290+ res = res. and ( check_impl ( tcx, item, impl_. self_ty , & impl_. of_trait ) ) ;
289291 }
290292 res
291293 }
@@ -1271,7 +1273,7 @@ fn check_impl<'tcx>(
12711273 // `#[rustc_reservation_impl]` impls are not real impls and
12721274 // therefore don't need to be WF (the trait's `Self: Trait` predicate
12731275 // won't hold).
1274- let trait_ref = tcx. impl_trait_ref ( item. owner_id ) . unwrap ( ) . instantiate_identity ( ) ;
1276+ let trait_ref = tcx. impl_trait_ref ( item. owner_id ) . instantiate_identity ( ) ;
12751277 // Avoid bogus "type annotations needed `Foo: Bar`" errors on `impl Bar for Foo` in case
12761278 // other `Foo` impls are incoherent.
12771279 tcx. ensure_ok ( ) . coherent_trait ( trait_ref. def_id ) ?;
0 commit comments