Skip to content

Commit ac94dd4

Browse files
authored
Unrolled build for rust-lang#121059
Rollup merge of rust-lang#121059 - compiler-errors:extension, r=davidtwco,Nilstrieb Add and use a simple extension trait derive macro in the compiler Adds `#[extension]` to `rustc_macros` for implementing an extension trait. This expands an impl (with an optional visibility) into two parallel trait + impl definitions. before: ```rust pub trait Extension { fn a(); } impl Extension for () { fn a() {} } ``` to: ```rust #[extension] pub impl Extension for () { fn a() {} } ``` Opted to just implement it by hand because I couldn't figure if there was a "canonical" choice of extension trait macro in the ecosystem. It's really lightweight anyways, and can always be changed. I'm interested in adding this because I'd like to later split up the large `TypeErrCtxtExt` traits into several different files. This should make it one step easier.
2 parents dfdbe30 + f624d55 commit ac94dd4

File tree

31 files changed

+291
-977
lines changed

31 files changed

+291
-977
lines changed

Diff for: compiler/rustc_ast_lowering/src/lib.rs

+3-10
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,16 +191,8 @@ 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-
202-
impl ResolverAstLoweringExt for ResolverAstLowering {
194+
#[extension(trait ResolverAstLoweringExt)]
195+
impl ResolverAstLowering {
203196
fn legacy_const_generic_args(&self, expr: &Expr) -> Option<Vec<usize>> {
204197
if let ExprKind::Path(None, path) = &expr.kind {
205198
// Don't perform legacy const generics rewriting if the path already

Diff for: 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(pub(crate) trait AllFactsExt)]
29+
impl 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()

Diff for: 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(pub trait PlaceExt<'tcx>)]
9+
impl<'tcx> 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>,

Diff for: 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(pub trait InferCtxtExt<'tcx>)]
230+
impl<'tcx> 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

Diff for: compiler/rustc_borrowck/src/universal_regions.rs

+3-21
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,27 +794,8 @@ 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-
816-
impl<'cx, 'tcx> InferCtxtExt<'tcx> for BorrowckInferCtxt<'cx, 'tcx> {
797+
#[extension(trait InferCtxtExt<'tcx>)]
798+
impl<'cx, 'tcx> BorrowckInferCtxt<'cx, 'tcx> {
817799
#[instrument(skip(self), level = "debug")]
818800
fn replace_free_regions_with_nll_infer_vars<T>(
819801
&self,

Diff for: compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

+3-11
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,17 +28,8 @@ 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-
40-
impl RegionExt for ResolvedArg {
31+
#[extension(trait RegionExt)]
32+
impl ResolvedArg {
4133
fn early(param: &GenericParam<'_>) -> (LocalDefId, ResolvedArg) {
4234
debug!("ResolvedArg::early: def_id={:?}", param.def_id);
4335
(param.def_id, ResolvedArg::EarlyBound(param.def_id.to_def_id()))

Diff for: 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(pub trait CanonicalExt<'tcx, V>)]
17+
impl<'tcx, V> 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>,

Diff for: 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(pub trait ObligationCauseExt<'tcx>)]
2790+
impl<'tcx> ObligationCause<'tcx> {
28022791
fn as_failure_code(&self, terr: TypeError<'tcx>) -> FailureCode {
28032792
use self::FailureCode::*;
28042793
use crate::traits::ObligationCauseCode::*;

Diff for: 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(pub trait TyCtxtInferExt<'tcx>)]
630+
impl<'tcx> TyCtxt<'tcx> {
634631
fn infer_ctxt(self) -> InferCtxtBuilder<'tcx> {
635632
InferCtxtBuilder {
636633
tcx: self,

Diff for: compiler/rustc_infer/src/traits/engine.rs

+3-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(pub trait TraitEngineExt<'tcx>)]
56+
impl<'tcx, T: ?Sized + TraitEngine<'tcx>> T {
6757
fn register_predicate_obligations(
6858
&mut self,
6959
infcx: &InferCtxt<'tcx>,
@@ -74,6 +64,7 @@ impl<'tcx, T: ?Sized + TraitEngine<'tcx>> TraitEngineExt<'tcx> for T {
7464
}
7565
}
7666

67+
#[must_use]
7768
fn select_all_or_error(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<FulfillmentError<'tcx>> {
7869
let errors = self.select_where_possible(infcx);
7970
if !errors.is_empty() {

0 commit comments

Comments
 (0)