Skip to content

[perf] Query-ify compare_impl_item #106130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 12 additions & 22 deletions compiler/rustc_hir_analysis/src/check/check.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::check::intrinsicck::InlineAsmCtxt;
use crate::errors::LinkageType;

use super::compare_method::check_type_bounds;
use super::compare_method::{compare_impl_method, compare_ty_impl};
use super::compare_impl_item::check_type_bounds;
use super::*;
use rustc_attr as attr;
use rustc_errors::{Applicability, ErrorGuaranteed, MultiSpan};
Expand Down Expand Up @@ -468,7 +467,7 @@ fn check_opaque_meets_bounds<'tcx>(
// Can have different predicates to their defining use
hir::OpaqueTyOrigin::TyAlias => {
let outlives_environment = OutlivesEnvironment::new(param_env);
infcx.check_region_obligations_and_report_errors(
let _ = infcx.check_region_obligations_and_report_errors(
defining_use_anchor,
&outlives_environment,
);
Expand Down Expand Up @@ -774,31 +773,22 @@ fn check_impl_items_against_trait<'tcx>(
let impl_item_full = tcx.hir().impl_item(impl_item.id);
match impl_item_full.kind {
hir::ImplItemKind::Const(..) => {
let _ = tcx.compare_assoc_const_impl_item_with_trait_item((
let _ = tcx.compare_impl_const((
impl_item.id.owner_id.def_id,
ty_impl_item.trait_item_def_id.unwrap(),
));
}
hir::ImplItemKind::Fn(..) => {
let opt_trait_span = tcx.hir().span_if_local(ty_trait_item.def_id);
compare_impl_method(
tcx,
&ty_impl_item,
&ty_trait_item,
impl_trait_ref,
opt_trait_span,
);
let _ = tcx.compare_impl_method((
impl_item.id.owner_id.def_id,
ty_impl_item.trait_item_def_id.unwrap(),
));
}
hir::ImplItemKind::Type(impl_ty) => {
let opt_trait_span = tcx.hir().span_if_local(ty_trait_item.def_id);
compare_ty_impl(
tcx,
&ty_impl_item,
impl_ty.span,
&ty_trait_item,
impl_trait_ref,
opt_trait_span,
);
hir::ImplItemKind::Type(..) => {
let _ = tcx.compare_impl_ty((
impl_item.id.owner_id.def_id,
ty_impl_item.trait_item_def_id.unwrap(),
));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKi
use rustc_infer::infer::{self, InferCtxt, TyCtxtInferExt};
use rustc_infer::traits::util;
use rustc_middle::ty::error::{ExpectedFound, TypeError};
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::util::ExplicitSelf;
use rustc_middle::ty::{
self, DefIdTree, InternalSubsts, Ty, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitable,
Expand All @@ -25,62 +26,52 @@ use rustc_trait_selection::traits::{
};
use std::iter;

pub(super) fn provide(providers: &mut Providers) {
*providers = Providers {
compare_impl_const: compare_impl_const_raw,
compare_impl_method: compare_impl_method_raw,
compare_impl_ty: compare_impl_ty_raw,
collect_trait_impl_trait_tys,
..*providers
};
}

/// Checks that a method from an impl conforms to the signature of
/// the same method as declared in the trait.
///
/// # Parameters
///
/// - `impl_m`: type of the method we are checking
/// - `impl_m_span`: span to use for reporting errors
/// - `trait_m`: the method in the trait
/// - `impl_trait_ref`: the TraitRef corresponding to the trait implementation
pub(crate) fn compare_impl_method<'tcx>(
tcx: TyCtxt<'tcx>,
impl_m: &ty::AssocItem,
trait_m: &ty::AssocItem,
impl_trait_ref: ty::TraitRef<'tcx>,
trait_item_span: Option<Span>,
) {
debug!("compare_impl_method(impl_trait_ref={:?})", impl_trait_ref);
fn compare_impl_method_raw(
tcx: TyCtxt<'_>,
(impl_m_def_id, trait_m_def_id): (LocalDefId, DefId),
) -> Result<(), ErrorGuaranteed> {
let impl_m = tcx.associated_item(impl_m_def_id);
let impl_m_span = tcx.def_span(impl_m_def_id);
let trait_m = tcx.associated_item(trait_m_def_id);
let impl_trait_ref = tcx.impl_trait_ref(impl_m.container_id(tcx)).unwrap();
let trait_item_span = tcx.hir().span_if_local(trait_m_def_id);

let impl_m_span = tcx.def_span(impl_m.def_id);
debug!("compare_impl_method(impl_trait_ref={:?})", impl_trait_ref);

if let Err(_) = compare_self_type(tcx, impl_m, impl_m_span, trait_m, impl_trait_ref) {
return;
}
compare_self_type(tcx, impl_m, impl_m_span, trait_m, impl_trait_ref)?;

if let Err(_) = compare_number_of_generics(tcx, impl_m, trait_m, trait_item_span, false) {
return;
}
compare_number_of_generics(tcx, impl_m, trait_m, trait_item_span, false)?;

if let Err(_) = compare_generic_param_kinds(tcx, impl_m, trait_m, false) {
return;
}
compare_generic_param_kinds(tcx, impl_m, trait_m, false)?;

if let Err(_) =
compare_number_of_method_arguments(tcx, impl_m, impl_m_span, trait_m, trait_item_span)
{
return;
}
compare_number_of_method_arguments(tcx, impl_m, impl_m_span, trait_m, trait_item_span)?;

if let Err(_) = compare_synthetic_generics(tcx, impl_m, trait_m) {
return;
}
compare_synthetic_generics(tcx, impl_m, trait_m)?;

if let Err(_) = compare_asyncness(tcx, impl_m, impl_m_span, trait_m, trait_item_span) {
return;
}
compare_asyncness(tcx, impl_m, impl_m_span, trait_m, trait_item_span)?;

if let Err(_) = compare_predicate_entailment(
compare_method_predicate_entailment(
tcx,
impl_m,
impl_m_span,
trait_m,
impl_trait_ref,
CheckImpliedWfMode::Check,
) {
return;
}
)?;

Ok(())
}

/// This function is best explained by example. Consider a trait:
Expand Down Expand Up @@ -150,7 +141,7 @@ pub(crate) fn compare_impl_method<'tcx>(
/// Finally we register each of these predicates as an obligation and check that
/// they hold.
#[instrument(level = "debug", skip(tcx, impl_m_span, impl_trait_ref))]
fn compare_predicate_entailment<'tcx>(
fn compare_method_predicate_entailment<'tcx>(
tcx: TyCtxt<'tcx>,
impl_m: &ty::AssocItem,
impl_m_span: Span,
Expand Down Expand Up @@ -337,7 +328,7 @@ fn compare_predicate_entailment<'tcx>(
if !errors.is_empty() {
match check_implied_wf {
CheckImpliedWfMode::Check => {
return compare_predicate_entailment(
return compare_method_predicate_entailment(
tcx,
impl_m,
impl_m_span,
Expand Down Expand Up @@ -374,7 +365,7 @@ fn compare_predicate_entailment<'tcx>(
// becomes a hard error (i.e. ideally we'd just call `resolve_regions_and_report_errors`
match check_implied_wf {
CheckImpliedWfMode::Check => {
return compare_predicate_entailment(
return compare_method_predicate_entailment(
tcx,
impl_m,
impl_m_span,
Expand Down Expand Up @@ -407,7 +398,7 @@ enum CheckImpliedWfMode {
/// re-check with `Skip`, and emit a lint if it succeeds.
Check,
/// Skips checking implied well-formedness of the impl method, but will emit
/// a lint if the `compare_predicate_entailment` succeeded. This means that
/// a lint if the `compare_method_predicate_entailment` succeeded. This means that
/// the reason that we had failed earlier during `Check` was due to the impl
/// having stronger requirements than the trait.
Skip,
Expand All @@ -425,7 +416,7 @@ fn compare_asyncness<'tcx>(
ty::Alias(ty::Opaque, ..) => {
// allow both `async fn foo()` and `fn foo() -> impl Future`
}
ty::Error(rustc_errors::ErrorGuaranteed { .. }) => {
ty::Error(_) => {
// We don't know if it's ok, but at least it's already an error.
}
_ => {
Expand All @@ -442,13 +433,13 @@ fn compare_asyncness<'tcx>(
}

#[instrument(skip(tcx), level = "debug", ret)]
pub fn collect_trait_impl_trait_tys<'tcx>(
fn collect_trait_impl_trait_tys<'tcx>(
tcx: TyCtxt<'tcx>,
def_id: DefId,
) -> Result<&'tcx FxHashMap<DefId, Ty<'tcx>>, ErrorGuaranteed> {
let impl_m = tcx.opt_associated_item(def_id).unwrap();
let trait_m = tcx.opt_associated_item(impl_m.trait_item_def_id.unwrap()).unwrap();
let impl_trait_ref = tcx.impl_trait_ref(impl_m.impl_container(tcx).unwrap()).unwrap();
let impl_m = tcx.associated_item(def_id);
let trait_m = tcx.associated_item(impl_m.trait_item_def_id.unwrap());
let impl_trait_ref = tcx.impl_trait_ref(impl_m.container_id(tcx)).unwrap();
let param_env = tcx.param_env(def_id);

// First, check a few of the same thing as `compare_impl_method`, just so we don't ICE during substitutions later.
Expand Down Expand Up @@ -550,13 +541,13 @@ pub fn collect_trait_impl_trait_tys<'tcx>(
// Unify the whole function signature. We need to do this to fully infer
// the lifetimes of the return type, but do this after unifying just the
// return types, since we want to avoid duplicating errors from
// `compare_predicate_entailment`.
// `compare_method_predicate_entailment`.
match ocx.eq(&cause, param_env, trait_fty, impl_fty) {
Ok(()) => {}
Err(terr) => {
// This function gets called during `compare_predicate_entailment` when normalizing a
// This function gets called during `compare_method_predicate_entailment` when normalizing a
// signature that contains RPITIT. When the method signatures don't match, we have to
// emit an error now because `compare_predicate_entailment` will not report the error
// emit an error now because `compare_method_predicate_entailment` will not report the error
// when normalization fails.
let emitted = report_trait_method_mismatch(
infcx,
Expand Down Expand Up @@ -589,7 +580,7 @@ pub fn collect_trait_impl_trait_tys<'tcx>(
infcx.check_region_obligations_and_report_errors(
impl_m.def_id.expect_local(),
&outlives_environment,
);
)?;

let mut collected_tys = FxHashMap::default();
for (def_id, (ty, substs)) in collector.types {
Expand Down Expand Up @@ -1516,8 +1507,8 @@ fn compare_generic_param_kinds<'tcx>(
Ok(())
}

/// Use `tcx.compare_assoc_const_impl_item_with_trait_item` instead
pub(crate) fn raw_compare_const_impl(
/// Use `tcx.compare_impl_const` instead
fn compare_impl_const_raw(
tcx: TyCtxt<'_>,
(impl_const_item_def, trait_const_item_def): (LocalDefId, DefId),
) -> Result<(), ErrorGuaranteed> {
Expand Down Expand Up @@ -1617,35 +1608,37 @@ pub(crate) fn raw_compare_const_impl(
return Err(infcx.err_ctxt().report_fulfillment_errors(&errors, None));
}

// FIXME return `ErrorReported` if region obligations error?
let outlives_environment = OutlivesEnvironment::new(param_env);
infcx.check_region_obligations_and_report_errors(impl_const_item_def, &outlives_environment);
infcx.check_region_obligations_and_report_errors(impl_const_item_def, &outlives_environment)?;

Ok(())
}

pub(crate) fn compare_ty_impl<'tcx>(
fn compare_impl_ty_raw<'tcx>(
tcx: TyCtxt<'tcx>,
impl_ty: &ty::AssocItem,
impl_ty_span: Span,
trait_ty: &ty::AssocItem,
impl_trait_ref: ty::TraitRef<'tcx>,
trait_item_span: Option<Span>,
) {
debug!("compare_impl_type(impl_trait_ref={:?})", impl_trait_ref);
(impl_ty_def_id, trait_ty_def_id): (LocalDefId, DefId),
) -> Result<(), ErrorGuaranteed> {
let impl_ty = tcx.associated_item(impl_ty_def_id);
let impl_ty_span = tcx.def_span(impl_ty_def_id);
let trait_ty = tcx.associated_item(trait_ty_def_id);
let impl_trait_ref = tcx.impl_trait_ref(impl_ty.container_id(tcx)).unwrap();
let trait_item_span = tcx.hir().span_if_local(trait_ty_def_id);

debug!("compare_impl_ty(impl_trait_ref={:?})", impl_trait_ref);

compare_number_of_generics(tcx, impl_ty, trait_ty, trait_item_span, false)?;

let _: Result<(), ErrorGuaranteed> = (|| {
compare_number_of_generics(tcx, impl_ty, trait_ty, trait_item_span, false)?;
compare_generic_param_kinds(tcx, impl_ty, trait_ty, false)?;

compare_generic_param_kinds(tcx, impl_ty, trait_ty, false)?;
let sp = tcx.def_span(impl_ty.def_id);
compare_type_predicate_entailment(tcx, impl_ty, sp, trait_ty, impl_trait_ref)?;

let sp = tcx.def_span(impl_ty.def_id);
compare_type_predicate_entailment(tcx, impl_ty, sp, trait_ty, impl_trait_ref)?;
check_type_bounds(tcx, trait_ty, impl_ty, impl_ty_span, impl_trait_ref)?;

check_type_bounds(tcx, trait_ty, impl_ty, impl_ty_span, impl_trait_ref)
})();
Ok(())
}

/// The equivalent of [compare_predicate_entailment], but for associated types
/// The equivalent of [compare_method_predicate_entailment], but for associated types
/// instead of associated functions.
fn compare_type_predicate_entailment<'tcx>(
tcx: TyCtxt<'tcx>,
Expand Down Expand Up @@ -1730,7 +1723,7 @@ fn compare_type_predicate_entailment<'tcx>(
infcx.check_region_obligations_and_report_errors(
impl_ty.def_id.expect_local(),
&outlives_environment,
);
)?;

Ok(())
}
Expand All @@ -1749,7 +1742,7 @@ fn compare_type_predicate_entailment<'tcx>(
/// from the impl could be overridden). We also can't normalize generic
/// associated types (yet) because they contain bound parameters.
#[instrument(level = "debug", skip(tcx))]
pub fn check_type_bounds<'tcx>(
pub(super) fn check_type_bounds<'tcx>(
tcx: TyCtxt<'tcx>,
trait_ty: &ty::AssocItem,
impl_ty: &ty::AssocItem,
Expand Down Expand Up @@ -1944,23 +1937,7 @@ pub fn check_type_bounds<'tcx>(
infcx.check_region_obligations_and_report_errors(
impl_ty.def_id.expect_local(),
&outlives_environment,
);

let constraints = infcx.inner.borrow_mut().opaque_type_storage.take_opaque_types();
for (key, value) in constraints {
infcx
.err_ctxt()
.report_mismatched_types(
&ObligationCause::misc(
value.hidden_type.span,
tcx.hir().local_def_id_to_hir_id(impl_ty.def_id.expect_local()),
),
tcx.mk_opaque(key.def_id.to_def_id(), key.substs),
value.hidden_type.ty,
TypeError::Mismatch,
)
.emit();
}
)?;

Ok(())
}
Expand Down
14 changes: 4 additions & 10 deletions compiler/rustc_hir_analysis/src/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ a type parameter).
*/

mod check;
mod compare_method;
mod compare_impl_item;
pub mod dropck;
pub mod intrinsic;
pub mod intrinsicck;
Expand Down Expand Up @@ -94,19 +94,13 @@ use std::num::NonZeroU32;
use crate::require_c_abi_if_c_variadic;
use crate::util::common::indenter;

use self::compare_method::collect_trait_impl_trait_tys;
use self::region::region_scope_tree;

pub fn provide(providers: &mut Providers) {
wfcheck::provide(providers);
*providers = Providers {
adt_destructor,
check_mod_item_types,
region_scope_tree,
collect_trait_impl_trait_tys,
compare_assoc_const_impl_item_with_trait_item: compare_method::raw_compare_const_impl,
..*providers
};
compare_impl_item::provide(providers);
*providers =
Providers { adt_destructor, check_mod_item_types, region_scope_tree, ..*providers };
}

fn adt_destructor(tcx: TyCtxt<'_>, def_id: DefId) -> Option<ty::Destructor> {
Expand Down
Loading