Skip to content

Commit 0b6e2e2

Browse files
committed
Refactor hir_analysis check_item impl of trait
1 parent d10e2f4 commit 0b6e2e2

File tree

1 file changed

+38
-36
lines changed

1 file changed

+38
-36
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
252248
crate::impl_wf_check::check_impl_wf(tcx, def_id)?;
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).unwrap();
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
}

0 commit comments

Comments
 (0)