Skip to content

Commit 21b5752

Browse files
queryify things
1 parent 02fb9ea commit 21b5752

File tree

4 files changed

+78
-89
lines changed

4 files changed

+78
-89
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

+9-19
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::check::intrinsicck::InlineAsmCtxt;
22
use crate::errors::LinkageType;
33

44
use super::compare_impl_item::check_type_bounds;
5-
use super::compare_impl_item::{compare_impl_method, compare_impl_ty};
65
use super::*;
76
use rustc_attr as attr;
87
use rustc_errors::{Applicability, ErrorGuaranteed, MultiSpan};
@@ -780,25 +779,16 @@ fn check_impl_items_against_trait<'tcx>(
780779
));
781780
}
782781
hir::ImplItemKind::Fn(..) => {
783-
let opt_trait_span = tcx.hir().span_if_local(ty_trait_item.def_id);
784-
compare_impl_method(
785-
tcx,
786-
&ty_impl_item,
787-
&ty_trait_item,
788-
impl_trait_ref,
789-
opt_trait_span,
790-
);
782+
let _ = tcx.compare_impl_method((
783+
impl_item.id.owner_id.def_id,
784+
ty_impl_item.trait_item_def_id.unwrap(),
785+
));
791786
}
792-
hir::ImplItemKind::Type(impl_ty) => {
793-
let opt_trait_span = tcx.hir().span_if_local(ty_trait_item.def_id);
794-
compare_impl_ty(
795-
tcx,
796-
&ty_impl_item,
797-
impl_ty.span,
798-
&ty_trait_item,
799-
impl_trait_ref,
800-
opt_trait_span,
801-
);
787+
hir::ImplItemKind::Type(..) => {
788+
let _ = tcx.compare_impl_ty((
789+
impl_item.id.owner_id.def_id,
790+
ty_impl_item.trait_item_def_id.unwrap(),
791+
));
802792
}
803793
}
804794

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+53-60
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKi
1212
use rustc_infer::infer::{self, InferCtxt, TyCtxtInferExt};
1313
use rustc_infer::traits::util;
1414
use rustc_middle::ty::error::{ExpectedFound, TypeError};
15+
use rustc_middle::ty::query::Providers;
1516
use rustc_middle::ty::util::ExplicitSelf;
1617
use rustc_middle::ty::{
1718
self, DefIdTree, InternalSubsts, Ty, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitable,
@@ -25,62 +26,52 @@ use rustc_trait_selection::traits::{
2526
};
2627
use std::iter;
2728

29+
pub(super) fn provide(providers: &mut Providers) {
30+
*providers = Providers {
31+
compare_impl_const: compare_impl_const_raw,
32+
compare_impl_method: compare_impl_method_raw,
33+
compare_impl_ty: compare_impl_ty_raw,
34+
collect_trait_impl_trait_tys,
35+
..*providers
36+
};
37+
}
38+
2839
/// Checks that a method from an impl conforms to the signature of
2940
/// the same method as declared in the trait.
30-
///
31-
/// # Parameters
32-
///
33-
/// - `impl_m`: type of the method we are checking
34-
/// - `impl_m_span`: span to use for reporting errors
35-
/// - `trait_m`: the method in the trait
36-
/// - `impl_trait_ref`: the TraitRef corresponding to the trait implementation
37-
pub(super) fn compare_impl_method<'tcx>(
38-
tcx: TyCtxt<'tcx>,
39-
impl_m: &ty::AssocItem,
40-
trait_m: &ty::AssocItem,
41-
impl_trait_ref: ty::TraitRef<'tcx>,
42-
trait_item_span: Option<Span>,
43-
) {
44-
debug!("compare_impl_method(impl_trait_ref={:?})", impl_trait_ref);
41+
fn compare_impl_method_raw(
42+
tcx: TyCtxt<'_>,
43+
(impl_m_def_id, trait_m_def_id): (LocalDefId, DefId),
44+
) -> Result<(), ErrorGuaranteed> {
45+
let impl_m = tcx.associated_item(impl_m_def_id);
46+
let impl_m_span = tcx.def_span(impl_m_def_id);
47+
let trait_m = tcx.associated_item(trait_m_def_id);
48+
let impl_trait_ref = tcx.impl_trait_ref(impl_m.container_id(tcx)).unwrap();
49+
let trait_item_span = tcx.hir().span_if_local(trait_m_def_id);
4550

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

48-
if let Err(_) = compare_self_type(tcx, impl_m, impl_m_span, trait_m, impl_trait_ref) {
49-
return;
50-
}
53+
compare_self_type(tcx, impl_m, impl_m_span, trait_m, impl_trait_ref)?;
5154

52-
if let Err(_) = compare_number_of_generics(tcx, impl_m, trait_m, trait_item_span, false) {
53-
return;
54-
}
55+
compare_number_of_generics(tcx, impl_m, trait_m, trait_item_span, false)?;
5556

56-
if let Err(_) = compare_generic_param_kinds(tcx, impl_m, trait_m, false) {
57-
return;
58-
}
57+
compare_generic_param_kinds(tcx, impl_m, trait_m, false)?;
5958

60-
if let Err(_) =
61-
compare_number_of_method_arguments(tcx, impl_m, impl_m_span, trait_m, trait_item_span)
62-
{
63-
return;
64-
}
59+
compare_number_of_method_arguments(tcx, impl_m, impl_m_span, trait_m, trait_item_span)?;
6560

66-
if let Err(_) = compare_synthetic_generics(tcx, impl_m, trait_m) {
67-
return;
68-
}
61+
compare_synthetic_generics(tcx, impl_m, trait_m)?;
6962

70-
if let Err(_) = compare_asyncness(tcx, impl_m, impl_m_span, trait_m, trait_item_span) {
71-
return;
72-
}
63+
compare_asyncness(tcx, impl_m, impl_m_span, trait_m, trait_item_span)?;
7364

74-
if let Err(_) = compare_method_predicate_entailment(
65+
compare_method_predicate_entailment(
7566
tcx,
7667
impl_m,
7768
impl_m_span,
7869
trait_m,
7970
impl_trait_ref,
8071
CheckImpliedWfMode::Check,
81-
) {
82-
return;
83-
}
72+
)?;
73+
74+
Ok(())
8475
}
8576

8677
/// This function is best explained by example. Consider a trait:
@@ -442,13 +433,13 @@ fn compare_asyncness<'tcx>(
442433
}
443434

444435
#[instrument(skip(tcx), level = "debug", ret)]
445-
pub(super) fn collect_trait_impl_trait_tys<'tcx>(
436+
fn collect_trait_impl_trait_tys<'tcx>(
446437
tcx: TyCtxt<'tcx>,
447438
def_id: DefId,
448439
) -> Result<&'tcx FxHashMap<DefId, Ty<'tcx>>, ErrorGuaranteed> {
449-
let impl_m = tcx.opt_associated_item(def_id).unwrap();
450-
let trait_m = tcx.opt_associated_item(impl_m.trait_item_def_id.unwrap()).unwrap();
451-
let impl_trait_ref = tcx.impl_trait_ref(impl_m.impl_container(tcx).unwrap()).unwrap();
440+
let impl_m = tcx.associated_item(def_id);
441+
let trait_m = tcx.associated_item(impl_m.trait_item_def_id.unwrap());
442+
let impl_trait_ref = tcx.impl_trait_ref(impl_m.container_id(tcx)).unwrap();
452443
let param_env = tcx.param_env(def_id);
453444

454445
// First, check a few of the same thing as `compare_impl_method`, just so we don't ICE during substitutions later.
@@ -1517,7 +1508,7 @@ fn compare_generic_param_kinds<'tcx>(
15171508
}
15181509

15191510
/// Use `tcx.compare_impl_const` instead
1520-
pub(super) fn compare_impl_const_raw(
1511+
fn compare_impl_const_raw(
15211512
tcx: TyCtxt<'_>,
15221513
(impl_const_item_def, trait_const_item_def): (LocalDefId, DefId),
15231514
) -> Result<(), ErrorGuaranteed> {
@@ -1623,26 +1614,28 @@ pub(super) fn compare_impl_const_raw(
16231614
Ok(())
16241615
}
16251616

1626-
pub(super) fn compare_impl_ty<'tcx>(
1617+
fn compare_impl_ty_raw<'tcx>(
16271618
tcx: TyCtxt<'tcx>,
1628-
impl_ty: &ty::AssocItem,
1629-
impl_ty_span: Span,
1630-
trait_ty: &ty::AssocItem,
1631-
impl_trait_ref: ty::TraitRef<'tcx>,
1632-
trait_item_span: Option<Span>,
1633-
) {
1634-
debug!("compare_impl_type(impl_trait_ref={:?})", impl_trait_ref);
1619+
(impl_ty_def_id, trait_ty_def_id): (LocalDefId, DefId),
1620+
) -> Result<(), ErrorGuaranteed> {
1621+
let impl_ty = tcx.associated_item(impl_ty_def_id);
1622+
let impl_ty_span = tcx.def_span(impl_ty_def_id);
1623+
let trait_ty = tcx.associated_item(trait_ty_def_id);
1624+
let impl_trait_ref = tcx.impl_trait_ref(impl_ty.container_id(tcx)).unwrap();
1625+
let trait_item_span = tcx.hir().span_if_local(trait_ty_def_id);
1626+
1627+
debug!("compare_impl_ty(impl_trait_ref={:?})", impl_trait_ref);
16351628

1636-
let _: Result<(), ErrorGuaranteed> = (|| {
1637-
compare_number_of_generics(tcx, impl_ty, trait_ty, trait_item_span, false)?;
1629+
compare_number_of_generics(tcx, impl_ty, trait_ty, trait_item_span, false)?;
16381630

1639-
compare_generic_param_kinds(tcx, impl_ty, trait_ty, false)?;
1631+
compare_generic_param_kinds(tcx, impl_ty, trait_ty, false)?;
16401632

1641-
let sp = tcx.def_span(impl_ty.def_id);
1642-
compare_type_predicate_entailment(tcx, impl_ty, sp, trait_ty, impl_trait_ref)?;
1633+
let sp = tcx.def_span(impl_ty.def_id);
1634+
compare_type_predicate_entailment(tcx, impl_ty, sp, trait_ty, impl_trait_ref)?;
16431635

1644-
check_type_bounds(tcx, trait_ty, impl_ty, impl_ty_span, impl_trait_ref)
1645-
})();
1636+
check_type_bounds(tcx, trait_ty, impl_ty, impl_ty_span, impl_trait_ref)?;
1637+
1638+
Ok(())
16461639
}
16471640

16481641
/// The equivalent of [compare_method_predicate_entailment], but for associated types

compiler/rustc_hir_analysis/src/check/mod.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,13 @@ use std::num::NonZeroU32;
9494
use crate::require_c_abi_if_c_variadic;
9595
use crate::util::common::indenter;
9696

97-
use self::compare_impl_item::collect_trait_impl_trait_tys;
9897
use self::region::region_scope_tree;
9998

10099
pub fn provide(providers: &mut Providers) {
101100
wfcheck::provide(providers);
102-
*providers = Providers {
103-
adt_destructor,
104-
check_mod_item_types,
105-
region_scope_tree,
106-
collect_trait_impl_trait_tys,
107-
compare_impl_const: compare_impl_item::compare_impl_const_raw,
108-
..*providers
109-
};
101+
compare_impl_item::provide(providers);
102+
*providers =
103+
Providers { adt_destructor, check_mod_item_types, region_scope_tree, ..*providers };
110104
}
111105

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

compiler/rustc_middle/src/query/mod.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -2120,7 +2120,19 @@ rustc_queries! {
21202120
query compare_impl_const(
21212121
key: (LocalDefId, DefId)
21222122
) -> Result<(), ErrorGuaranteed> {
2123-
desc { |tcx| "checking assoc const `{}` has the same type as trait item", tcx.def_path_str(key.0.to_def_id()) }
2123+
desc { |tcx| "checking associated const `{}` is compatible with trait item", tcx.def_path_str(key.0.to_def_id()) }
2124+
}
2125+
2126+
query compare_impl_method(
2127+
key: (LocalDefId, DefId)
2128+
) -> Result<(), ErrorGuaranteed> {
2129+
desc { |tcx| "checking associated method `{}` is compatible with trait item", tcx.def_path_str(key.0.to_def_id()) }
2130+
}
2131+
2132+
query compare_impl_ty(
2133+
key: (LocalDefId, DefId)
2134+
) -> Result<(), ErrorGuaranteed> {
2135+
desc { |tcx| "checking associated type `{}` is compatible with trait item", tcx.def_path_str(key.0.to_def_id()) }
21242136
}
21252137

21262138
query deduced_param_attrs(def_id: DefId) -> &'tcx [ty::DeducedParamAttrs] {

0 commit comments

Comments
 (0)