Skip to content

Commit 9c25823

Browse files
Use extension trait derive
1 parent 3250e95 commit 9c25823

File tree

29 files changed

+119
-972
lines changed

29 files changed

+119
-972
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
5757
use rustc_hir::def_id::{LocalDefId, LocalDefIdMap, CRATE_DEF_ID, LOCAL_CRATE};
5858
use rustc_hir::{ConstArg, GenericArg, ItemLocalMap, ParamName, TraitCandidate};
5959
use rustc_index::{Idx, IndexSlice, IndexVec};
60+
use rustc_macros::extension;
6061
use rustc_middle::span_bug;
6162
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
6263
use rustc_session::parse::{add_feature_diagnostics, feature_err};
@@ -190,15 +191,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
190191
}
191192
}
192193

193-
trait ResolverAstLoweringExt {
194-
fn legacy_const_generic_args(&self, expr: &Expr) -> Option<Vec<usize>>;
195-
fn get_partial_res(&self, id: NodeId) -> Option<PartialRes>;
196-
fn get_import_res(&self, id: NodeId) -> PerNS<Option<Res<NodeId>>>;
197-
fn get_label_res(&self, id: NodeId) -> Option<NodeId>;
198-
fn get_lifetime_res(&self, id: NodeId) -> Option<LifetimeRes>;
199-
fn take_extra_lifetime_params(&mut self, id: NodeId) -> Vec<(Ident, NodeId, LifetimeRes)>;
200-
}
201-
194+
#[extension]
202195
impl ResolverAstLoweringExt for ResolverAstLowering {
203196
fn legacy_const_generic_args(&self, expr: &Expr) -> Option<Vec<usize>> {
204197
if let ExprKind::Path(None, path) = &expr.kind {

compiler/rustc_borrowck/src/facts.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::location::{LocationIndex, LocationTable};
22
use crate::BorrowIndex;
33
use polonius_engine::AllFacts as PoloniusFacts;
44
use polonius_engine::Atom;
5+
use rustc_macros::extension;
56
use rustc_middle::mir::Local;
67
use rustc_middle::ty::{RegionVid, TyCtxt};
78
use rustc_mir_dataflow::move_paths::MovePathIndex;
@@ -24,20 +25,10 @@ impl polonius_engine::FactTypes for RustcFacts {
2425

2526
pub type AllFacts = PoloniusFacts<RustcFacts>;
2627

27-
pub(crate) trait AllFactsExt {
28+
#[extension]
29+
pub(crate) impl AllFactsExt for AllFacts {
2830
/// Returns `true` if there is a need to gather `AllFacts` given the
2931
/// current `-Z` flags.
30-
fn enabled(tcx: TyCtxt<'_>) -> bool;
31-
32-
fn write_to_dir(
33-
&self,
34-
dir: impl AsRef<Path>,
35-
location_table: &LocationTable,
36-
) -> Result<(), Box<dyn Error>>;
37-
}
38-
39-
impl AllFactsExt for AllFacts {
40-
/// Return
4132
fn enabled(tcx: TyCtxt<'_>) -> bool {
4233
tcx.sess.opts.unstable_opts.nll_facts
4334
|| tcx.sess.opts.unstable_opts.polonius.is_legacy_enabled()

compiler/rustc_borrowck/src/place_ext.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
11
use crate::borrow_set::LocalsStateAtExit;
22
use rustc_hir as hir;
3+
use rustc_macros::extension;
34
use rustc_middle::mir::ProjectionElem;
45
use rustc_middle::mir::{Body, Mutability, Place};
56
use rustc_middle::ty::{self, TyCtxt};
67

7-
/// Extension methods for the `Place` type.
8-
pub trait PlaceExt<'tcx> {
8+
#[extension]
9+
pub impl<'tcx> PlaceExt<'tcx> for Place<'tcx> {
910
/// Returns `true` if we can safely ignore borrows of this place.
1011
/// This is true whenever there is no action that the user can do
1112
/// to the place `self` that would invalidate the borrow. This is true
1213
/// for borrows of raw pointer dereferents as well as shared references.
13-
fn ignore_borrow(
14-
&self,
15-
tcx: TyCtxt<'tcx>,
16-
body: &Body<'tcx>,
17-
locals_state_at_exit: &LocalsStateAtExit,
18-
) -> bool;
19-
}
20-
21-
impl<'tcx> PlaceExt<'tcx> for Place<'tcx> {
2214
fn ignore_borrow(
2315
&self,
2416
tcx: TyCtxt<'tcx>,

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_hir::OpaqueTyOrigin;
66
use rustc_infer::infer::InferCtxt;
77
use rustc_infer::infer::TyCtxtInferExt as _;
88
use rustc_infer::traits::{Obligation, ObligationCause};
9+
use rustc_macros::extension;
910
use rustc_middle::traits::DefiningAnchor;
1011
use rustc_middle::ty::visit::TypeVisitableExt;
1112
use rustc_middle::ty::{self, OpaqueHiddenType, OpaqueTypeKey, Ty, TyCtxt, TypeFoldable};
@@ -225,15 +226,8 @@ impl<'tcx> RegionInferenceContext<'tcx> {
225226
}
226227
}
227228

228-
pub trait InferCtxtExt<'tcx> {
229-
fn infer_opaque_definition_from_instantiation(
230-
&self,
231-
opaque_type_key: OpaqueTypeKey<'tcx>,
232-
instantiated_ty: OpaqueHiddenType<'tcx>,
233-
) -> Ty<'tcx>;
234-
}
235-
236-
impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
229+
#[extension]
230+
pub impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
237231
/// Given the fully resolved, instantiated type for an opaque
238232
/// type, i.e., the value of an inference variable like C1 or C2
239233
/// (*), computes the "definition type" for an opaque type

compiler/rustc_borrowck/src/universal_regions.rs

+2-20
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use rustc_hir::lang_items::LangItem;
2222
use rustc_hir::BodyOwnerKind;
2323
use rustc_index::IndexVec;
2424
use rustc_infer::infer::NllRegionVariableOrigin;
25+
use rustc_macros::extension;
2526
use rustc_middle::ty::fold::TypeFoldable;
2627
use rustc_middle::ty::print::with_no_trimmed_paths;
2728
use rustc_middle::ty::{self, InlineConstArgs, InlineConstArgsParts, RegionVid, Ty, TyCtxt};
@@ -793,26 +794,7 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
793794
}
794795
}
795796

796-
trait InferCtxtExt<'tcx> {
797-
fn replace_free_regions_with_nll_infer_vars<T>(
798-
&self,
799-
origin: NllRegionVariableOrigin,
800-
value: T,
801-
) -> T
802-
where
803-
T: TypeFoldable<TyCtxt<'tcx>>;
804-
805-
fn replace_bound_regions_with_nll_infer_vars<T>(
806-
&self,
807-
origin: NllRegionVariableOrigin,
808-
all_outlive_scope: LocalDefId,
809-
value: ty::Binder<'tcx, T>,
810-
indices: &mut UniversalRegionIndices<'tcx>,
811-
) -> T
812-
where
813-
T: TypeFoldable<TyCtxt<'tcx>>;
814-
}
815-
797+
#[extension]
816798
impl<'cx, 'tcx> InferCtxtExt<'tcx> for BorrowckInferCtxt<'cx, 'tcx> {
817799
#[instrument(skip(self), level = "debug")]
818800
fn replace_free_regions_with_nll_infer_vars<T>(

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_hir::def::{DefKind, Res};
1414
use rustc_hir::def_id::LocalDefId;
1515
use rustc_hir::intravisit::{self, Visitor};
1616
use rustc_hir::{GenericArg, GenericParam, GenericParamKind, HirIdMap, LifetimeName, Node};
17+
use rustc_macros::extension;
1718
use rustc_middle::bug;
1819
use rustc_middle::hir::nested_filter;
1920
use rustc_middle::middle::resolve_bound_vars::*;
@@ -27,16 +28,7 @@ use std::fmt;
2728

2829
use crate::errors;
2930

30-
trait RegionExt {
31-
fn early(param: &GenericParam<'_>) -> (LocalDefId, ResolvedArg);
32-
33-
fn late(index: u32, param: &GenericParam<'_>) -> (LocalDefId, ResolvedArg);
34-
35-
fn id(&self) -> Option<DefId>;
36-
37-
fn shifted(self, amount: u32) -> ResolvedArg;
38-
}
39-
31+
#[extension]
4032
impl RegionExt for ResolvedArg {
4133
fn early(param: &GenericParam<'_>) -> (LocalDefId, ResolvedArg) {
4234
debug!("ResolvedArg::early: def_id={:?}", param.def_id);

compiler/rustc_infer/src/infer/canonical/instantiate.rs

+6-20
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,23 @@ use rustc_middle::ty::{self, TyCtxt};
1313

1414
/// FIXME(-Znext-solver): This or public because it is shared with the
1515
/// new trait solver implementation. We should deduplicate canonicalization.
16-
pub trait CanonicalExt<'tcx, V> {
16+
#[extension]
17+
pub impl<'tcx, V> CanonicalExt<'tcx, V> for Canonical<'tcx, V> {
1718
/// Instantiate the wrapped value, replacing each canonical value
1819
/// with the value given in `var_values`.
1920
fn instantiate(&self, tcx: TyCtxt<'tcx>, var_values: &CanonicalVarValues<'tcx>) -> V
2021
where
21-
V: TypeFoldable<TyCtxt<'tcx>>;
22+
V: TypeFoldable<TyCtxt<'tcx>>,
23+
{
24+
self.instantiate_projected(tcx, var_values, |value| value.clone())
25+
}
2226

2327
/// Allows one to apply a instantiation to some subset of
2428
/// `self.value`. Invoke `projection_fn` with `self.value` to get
2529
/// a value V that is expressed in terms of the same canonical
2630
/// variables bound in `self` (usually this extracts from subset
2731
/// of `self`). Apply the instantiation `var_values` to this value
2832
/// V, replacing each of the canonical variables.
29-
fn instantiate_projected<T>(
30-
&self,
31-
tcx: TyCtxt<'tcx>,
32-
var_values: &CanonicalVarValues<'tcx>,
33-
projection_fn: impl FnOnce(&V) -> T,
34-
) -> T
35-
where
36-
T: TypeFoldable<TyCtxt<'tcx>>;
37-
}
38-
39-
impl<'tcx, V> CanonicalExt<'tcx, V> for Canonical<'tcx, V> {
40-
fn instantiate(&self, tcx: TyCtxt<'tcx>, var_values: &CanonicalVarValues<'tcx>) -> V
41-
where
42-
V: TypeFoldable<TyCtxt<'tcx>>,
43-
{
44-
self.instantiate_projected(tcx, var_values, |value| value.clone())
45-
}
46-
4733
fn instantiate_projected<T>(
4834
&self,
4935
tcx: TyCtxt<'tcx>,

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+2-13
Original file line numberDiff line numberDiff line change
@@ -2786,19 +2786,8 @@ pub enum FailureCode {
27862786
Error0644,
27872787
}
27882788

2789-
pub trait ObligationCauseExt<'tcx> {
2790-
fn as_failure_code(&self, terr: TypeError<'tcx>) -> FailureCode;
2791-
2792-
fn as_failure_code_diag(
2793-
&self,
2794-
terr: TypeError<'tcx>,
2795-
span: Span,
2796-
subdiags: Vec<TypeErrorAdditionalDiags>,
2797-
) -> ObligationCauseFailureCode;
2798-
fn as_requirement_str(&self) -> &'static str;
2799-
}
2800-
2801-
impl<'tcx> ObligationCauseExt<'tcx> for ObligationCause<'tcx> {
2789+
#[extension]
2790+
pub impl<'tcx> ObligationCauseExt<'tcx> for ObligationCause<'tcx> {
28022791
fn as_failure_code(&self, terr: TypeError<'tcx>) -> FailureCode {
28032792
use self::FailureCode::*;
28042793
use crate::traits::ObligationCauseCode::*;

compiler/rustc_infer/src/infer/mod.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -626,11 +626,8 @@ pub struct InferCtxtBuilder<'tcx> {
626626
next_trait_solver: bool,
627627
}
628628

629-
pub trait TyCtxtInferExt<'tcx> {
630-
fn infer_ctxt(self) -> InferCtxtBuilder<'tcx>;
631-
}
632-
633-
impl<'tcx> TyCtxtInferExt<'tcx> for TyCtxt<'tcx> {
629+
#[extension]
630+
pub impl<'tcx> TyCtxtInferExt<'tcx> for TyCtxt<'tcx> {
634631
fn infer_ctxt(self) -> InferCtxtBuilder<'tcx> {
635632
InferCtxtBuilder {
636633
tcx: self,

compiler/rustc_infer/src/traits/engine.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,8 @@ pub trait TraitEngine<'tcx>: 'tcx {
5252
) -> Vec<PredicateObligation<'tcx>>;
5353
}
5454

55-
pub trait TraitEngineExt<'tcx> {
56-
fn register_predicate_obligations(
57-
&mut self,
58-
infcx: &InferCtxt<'tcx>,
59-
obligations: impl IntoIterator<Item = PredicateObligation<'tcx>>,
60-
);
61-
62-
#[must_use]
63-
fn select_all_or_error(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<FulfillmentError<'tcx>>;
64-
}
65-
66-
impl<'tcx, T: ?Sized + TraitEngine<'tcx>> TraitEngineExt<'tcx> for T {
55+
#[extension]
56+
pub impl<'tcx, T: ?Sized + TraitEngine<'tcx>> TraitEngineExt<'tcx> for T {
6757
fn register_predicate_obligations(
6858
&mut self,
6959
infcx: &InferCtxt<'tcx>,

compiler/rustc_middle/src/ty/layout.rs

+4-20
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,8 @@ use std::fmt;
2323
use std::num::NonZero;
2424
use std::ops::Bound;
2525

26-
pub trait IntegerExt {
27-
fn to_ty<'tcx>(&self, tcx: TyCtxt<'tcx>, signed: bool) -> Ty<'tcx>;
28-
fn from_int_ty<C: HasDataLayout>(cx: &C, ity: ty::IntTy) -> Integer;
29-
fn from_uint_ty<C: HasDataLayout>(cx: &C, uty: ty::UintTy) -> Integer;
30-
fn repr_discr<'tcx>(
31-
tcx: TyCtxt<'tcx>,
32-
ty: Ty<'tcx>,
33-
repr: &ReprOptions,
34-
min: i128,
35-
max: i128,
36-
) -> (Integer, bool);
37-
}
38-
39-
impl IntegerExt for Integer {
26+
#[extension]
27+
pub impl IntegerExt for Integer {
4028
#[inline]
4129
fn to_ty<'tcx>(&self, tcx: TyCtxt<'tcx>, signed: bool) -> Ty<'tcx> {
4230
match (*self, signed) {
@@ -123,12 +111,8 @@ impl IntegerExt for Integer {
123111
}
124112
}
125113

126-
pub trait PrimitiveExt {
127-
fn to_ty<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx>;
128-
fn to_int_ty<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx>;
129-
}
130-
131-
impl PrimitiveExt for Primitive {
114+
#[extension]
115+
pub impl PrimitiveExt for Primitive {
132116
#[inline]
133117
fn to_ty<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
134118
match *self {

compiler/rustc_middle/src/ty/util.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,8 @@ impl<'tcx> Discr<'tcx> {
9696
}
9797
}
9898

99-
pub trait IntTypeExt {
100-
fn to_ty<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx>;
101-
fn disr_incr<'tcx>(&self, tcx: TyCtxt<'tcx>, val: Option<Discr<'tcx>>) -> Option<Discr<'tcx>>;
102-
fn initial_discriminant<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Discr<'tcx>;
103-
}
104-
105-
impl IntTypeExt for IntegerType {
99+
#[extension]
100+
pub impl IntTypeExt for IntegerType {
106101
fn to_ty<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
107102
match self {
108103
IntegerType::Pointer(true) => tcx.types.isize,

0 commit comments

Comments
 (0)