Skip to content

Commit

Permalink
Remove an impl_polarity call where the information is already avail…
Browse files Browse the repository at this point in the history
…able in the header
  • Loading branch information
oli-obk committed Feb 13, 2024
1 parent 75de6cf commit 251c33c
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions compiler/rustc_hir_analysis/src/coherence/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ pub fn check_trait<'tcx>(
) -> Result<(), ErrorGuaranteed> {
let lang_items = tcx.lang_items();
let checker = Checker { tcx, trait_def_id, impl_def_id };
let mut res = checker.check(lang_items.drop_trait(), visit_implementation_of_drop);
res = res.and(checker.check(lang_items.copy_trait(), visit_implementation_of_copy));
res = res.and(
checker.check(lang_items.const_param_ty_trait(), visit_implementation_of_const_param_ty),
);
let mut res = checker.check(lang_items.drop_trait(), |tcx, id| {
visit_implementation_of_drop(tcx, id, impl_header)
});
res = res.and(checker.check(lang_items.copy_trait(), |tcx, id| {
visit_implementation_of_copy(tcx, id, impl_header)
}));
res = res.and(checker.check(lang_items.const_param_ty_trait(), |tcx, id| {
visit_implementation_of_const_param_ty(tcx, id, impl_header)
}));
res = res.and(
checker.check(lang_items.coerce_unsized_trait(), visit_implementation_of_coerce_unsized),
);
Expand All @@ -62,12 +66,13 @@ impl<'tcx> Checker<'tcx> {
}
}

fn visit_implementation_of_drop(
tcx: TyCtxt<'_>,
fn visit_implementation_of_drop<'tcx>(
tcx: TyCtxt<'tcx>,
impl_did: LocalDefId,
header: ty::ImplTraitHeader<'tcx>,
) -> Result<(), ErrorGuaranteed> {
// Destructors only work on local ADT types.
match tcx.type_of(impl_did).instantiate_identity().kind() {
match header.trait_ref.self_ty().kind() {
ty::Adt(def, _) if def.did().is_local() => return Ok(()),
ty::Error(_) => return Ok(()),
_ => {}
Expand All @@ -78,21 +83,22 @@ fn visit_implementation_of_drop(
Err(tcx.dcx().emit_err(errors::DropImplOnWrongItem { span: impl_.self_ty.span }))
}

fn visit_implementation_of_copy(
tcx: TyCtxt<'_>,
fn visit_implementation_of_copy<'tcx>(
tcx: TyCtxt<'tcx>,
impl_did: LocalDefId,
impl_header: ty::ImplTraitHeader<'tcx>,
) -> Result<(), ErrorGuaranteed> {
debug!("visit_implementation_of_copy: impl_did={:?}", impl_did);

let self_type = tcx.type_of(impl_did).instantiate_identity();
let self_type = impl_header.trait_ref.self_ty();
debug!("visit_implementation_of_copy: self_type={:?} (bound)", self_type);

let param_env = tcx.param_env(impl_did);
assert!(!self_type.has_escaping_bound_vars());

debug!("visit_implementation_of_copy: self_type={:?} (free)", self_type);

if let ty::ImplPolarity::Negative = tcx.impl_polarity(impl_did) {
if let ty::ImplPolarity::Negative = impl_header.polarity {
return Ok(());
}

Expand All @@ -114,16 +120,17 @@ fn visit_implementation_of_copy(
}
}

fn visit_implementation_of_const_param_ty(
tcx: TyCtxt<'_>,
fn visit_implementation_of_const_param_ty<'tcx>(
tcx: TyCtxt<'tcx>,
impl_did: LocalDefId,
header: ty::ImplTraitHeader<'tcx>,
) -> Result<(), ErrorGuaranteed> {
let self_type = tcx.type_of(impl_did).instantiate_identity();
let self_type = header.trait_ref.self_ty();
assert!(!self_type.has_escaping_bound_vars());

let param_env = tcx.param_env(impl_did);

if let ty::ImplPolarity::Negative = tcx.impl_polarity(impl_did) {
if let ty::ImplPolarity::Negative = header.polarity {
return Ok(());
}

Expand Down

0 comments on commit 251c33c

Please sign in to comment.