Skip to content

Commit 4cd4701

Browse files
committed
Auto merge of #153013 - jhpratt:rollup-7LbyTa3, r=jhpratt
Rollup of 6 pull requests Successful merges: - #153007 (`rust-analyzer` subtree update) - #152768 (Enable autodiff in ci for all major os) - #152003 (Reflection TypeId::trait_info_of) - #152988 (Port `#[register_tool]` to the new attribute system) - #152989 (Port `#[rustc_inherit_overflow_checks]` to the new attribute parsers) - #152991 (fix interpreter tracing output)
2 parents eeb94be + 8472621 commit 4cd4701

93 files changed

Lines changed: 2557 additions & 984 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

compiler/rustc_attr_parsing/src/attributes/crate_level.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,50 @@ impl<S: Stage> NoArgsAttributeParser<S> for DefaultLibAllocatorParser {
301301
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
302302
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::DefaultLibAllocator;
303303
}
304+
305+
pub(crate) struct RegisterToolParser;
306+
307+
impl<S: Stage> CombineAttributeParser<S> for RegisterToolParser {
308+
const PATH: &[Symbol] = &[sym::register_tool];
309+
type Item = Ident;
310+
const CONVERT: ConvertFn<Self::Item> = AttributeKind::RegisterTool;
311+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
312+
const TEMPLATE: AttributeTemplate = template!(List: &["tool1, tool2, ..."]);
313+
314+
fn extend(
315+
cx: &mut AcceptContext<'_, '_, S>,
316+
args: &ArgParser,
317+
) -> impl IntoIterator<Item = Self::Item> {
318+
let ArgParser::List(list) = args else {
319+
cx.expected_list(cx.attr_span, args);
320+
return Vec::new();
321+
};
322+
323+
if list.is_empty() {
324+
cx.warn_empty_attribute(cx.attr_span);
325+
}
326+
327+
let mut res = Vec::new();
328+
329+
for elem in list.mixed() {
330+
let Some(elem) = elem.meta_item() else {
331+
cx.expected_identifier(elem.span());
332+
continue;
333+
};
334+
if let Err(arg_span) = elem.args().no_args() {
335+
cx.expected_no_args(arg_span);
336+
continue;
337+
}
338+
339+
let path = elem.path();
340+
let Some(ident) = path.word() else {
341+
cx.expected_identifier(path.span());
342+
continue;
343+
};
344+
345+
res.push(ident);
346+
}
347+
348+
res
349+
}
350+
}

compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,20 @@ impl<S: Stage> SingleAttributeParser<S> for RustcLegacyConstGenericsParser {
175175
}
176176
}
177177

178+
pub(crate) struct RustcInheritOverflowChecksParser;
179+
180+
impl<S: Stage> NoArgsAttributeParser<S> for RustcInheritOverflowChecksParser {
181+
const PATH: &[Symbol] = &[sym::rustc_inherit_overflow_checks];
182+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
183+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
184+
Allow(Target::Fn),
185+
Allow(Target::Method(MethodKind::Inherent)),
186+
Allow(Target::Method(MethodKind::TraitImpl)),
187+
Allow(Target::Closure),
188+
]);
189+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcInheritOverflowChecks;
190+
}
191+
178192
pub(crate) struct RustcLintOptDenyFieldAccessParser;
179193

180194
impl<S: Stage> SingleAttributeParser<S> for RustcLintOptDenyFieldAccessParser {

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ attribute_parsers!(
159159
Combine<DebuggerViualizerParser>,
160160
Combine<ForceTargetFeatureParser>,
161161
Combine<LinkParser>,
162+
Combine<RegisterToolParser>,
162163
Combine<ReprParser>,
163164
Combine<RustcCleanParser>,
164165
Combine<RustcLayoutParser>,
@@ -286,6 +287,7 @@ attribute_parsers!(
286287
Single<WithoutArgs<RustcEvaluateWhereClausesParser>>,
287288
Single<WithoutArgs<RustcHasIncoherentInherentImplsParser>>,
288289
Single<WithoutArgs<RustcHiddenTypeOfOpaquesParser>>,
290+
Single<WithoutArgs<RustcInheritOverflowChecksParser>>,
289291
Single<WithoutArgs<RustcInsignificantDtorParser>>,
290292
Single<WithoutArgs<RustcIntrinsicConstStableIndirectParser>>,
291293
Single<WithoutArgs<RustcIntrinsicParser>>,

compiler/rustc_const_eval/src/const_eval/machine.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::borrow::{Borrow, Cow};
22
use std::fmt;
33
use std::hash::Hash;
44

5-
use rustc_abi::{Align, Size};
5+
use rustc_abi::{Align, FIRST_VARIANT, Size};
66
use rustc_ast::Mutability;
77
use rustc_data_structures::fx::{FxHashMap, FxIndexMap, IndexEntry};
88
use rustc_errors::msg;
@@ -24,7 +24,7 @@ use crate::interpret::{
2424
self, AllocId, AllocInit, AllocRange, ConstAllocation, CtfeProvenance, FnArg, Frame,
2525
GlobalAlloc, ImmTy, InterpCx, InterpResult, OpTy, PlaceTy, Pointer, RangeSet, Scalar,
2626
compile_time_machine, err_inval, interp_ok, throw_exhaust, throw_inval, throw_ub,
27-
throw_ub_custom, throw_unsup, throw_unsup_format,
27+
throw_ub_custom, throw_unsup, throw_unsup_format, type_implements_dyn_trait,
2828
};
2929

3030
/// When hitting this many interpreted terminators we emit a deny by default lint
@@ -598,6 +598,22 @@ impl<'tcx> interpret::Machine<'tcx> for CompileTimeMachine<'tcx> {
598598
}
599599
}
600600

601+
sym::type_id_vtable => {
602+
let tp_ty = ecx.read_type_id(&args[0])?;
603+
let result_ty = ecx.read_type_id(&args[1])?;
604+
605+
let (implements_trait, preds) = type_implements_dyn_trait(ecx, tp_ty, result_ty)?;
606+
607+
if implements_trait {
608+
let vtable_ptr = ecx.get_vtable_ptr(tp_ty, preds)?;
609+
// Writing a non-null pointer into an `Option<NonNull>` will automatically make it `Some`.
610+
ecx.write_pointer(vtable_ptr, dest)?;
611+
} else {
612+
// Write `None`
613+
ecx.write_discriminant(FIRST_VARIANT, dest)?;
614+
}
615+
}
616+
601617
sym::type_of => {
602618
let ty = ecx.read_type_id(&args[0])?;
603619
ecx.write_type_info(ty, dest)?;

compiler/rustc_const_eval/src/interpret/intrinsics.rs

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,16 @@
44
55
mod simd;
66

7-
use rustc_abi::{FIRST_VARIANT, FieldIdx, HasDataLayout, Size, VariantIdx};
7+
use rustc_abi::{FieldIdx, HasDataLayout, Size, VariantIdx};
88
use rustc_apfloat::ieee::{Double, Half, Quad, Single};
99
use rustc_data_structures::assert_matches;
1010
use rustc_errors::msg;
11-
use rustc_hir::def_id::CRATE_DEF_ID;
12-
use rustc_infer::infer::TyCtxtInferExt;
1311
use rustc_middle::mir::interpret::{CTFE_ALLOC_SALT, read_target_uint, write_target_uint};
1412
use rustc_middle::mir::{self, BinOp, ConstValue, NonDivergingIntrinsic};
1513
use rustc_middle::ty::layout::TyAndLayout;
16-
use rustc_middle::ty::{FloatTy, PolyExistentialPredicate, Ty, TyCtxt, TypeVisitableExt};
14+
use rustc_middle::ty::{FloatTy, Ty, TyCtxt, TypeVisitableExt};
1715
use rustc_middle::{bug, span_bug, ty};
1816
use rustc_span::{Symbol, sym};
19-
use rustc_trait_selection::traits::{Obligation, ObligationCause, ObligationCtxt};
2017
use tracing::trace;
2118

2219
use super::memory::MemoryKind;
@@ -227,44 +224,6 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
227224

228225
self.write_scalar(Scalar::from_target_usize(offset, self), dest)?;
229226
}
230-
sym::vtable_for => {
231-
let tp_ty = instance.args.type_at(0);
232-
let result_ty = instance.args.type_at(1);
233-
234-
ensure_monomorphic_enough(tcx, tp_ty)?;
235-
ensure_monomorphic_enough(tcx, result_ty)?;
236-
let ty::Dynamic(preds, _) = result_ty.kind() else {
237-
span_bug!(
238-
self.find_closest_untracked_caller_location(),
239-
"Invalid type provided to vtable_for::<T, U>. U must be dyn Trait, got {result_ty}."
240-
);
241-
};
242-
243-
let (infcx, param_env) =
244-
self.tcx.infer_ctxt().build_with_typing_env(self.typing_env);
245-
246-
let ocx = ObligationCtxt::new(&infcx);
247-
ocx.register_obligations(preds.iter().map(|pred: PolyExistentialPredicate<'_>| {
248-
let pred = pred.with_self_ty(tcx, tp_ty);
249-
// Lifetimes can only be 'static because of the bound on T
250-
let pred = ty::fold_regions(tcx, pred, |r, _| {
251-
if r == tcx.lifetimes.re_erased { tcx.lifetimes.re_static } else { r }
252-
});
253-
Obligation::new(tcx, ObligationCause::dummy(), param_env, pred)
254-
}));
255-
let type_impls_trait = ocx.evaluate_obligations_error_on_ambiguity().is_empty();
256-
// Since `assumed_wf_tys=[]` the choice of LocalDefId is irrelevant, so using the "default"
257-
let regions_are_valid = ocx.resolve_regions(CRATE_DEF_ID, param_env, []).is_empty();
258-
259-
if regions_are_valid && type_impls_trait {
260-
let vtable_ptr = self.get_vtable_ptr(tp_ty, preds)?;
261-
// Writing a non-null pointer into an `Option<NonNull>` will automatically make it `Some`.
262-
self.write_pointer(vtable_ptr, dest)?;
263-
} else {
264-
// Write `None`
265-
self.write_discriminant(FIRST_VARIANT, dest)?;
266-
}
267-
}
268227
sym::variant_count => {
269228
let tp_ty = instance.args.type_at(0);
270229
let ty = match tp_ty.kind() {

compiler/rustc_const_eval/src/interpret/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ use self::place::{MemPlace, Place};
3838
pub use self::projection::{OffsetMode, Projectable};
3939
pub use self::stack::{Frame, FrameInfo, LocalState, ReturnContinuation};
4040
pub use self::util::EnteredTraceSpan;
41-
pub(crate) use self::util::create_static_alloc;
41+
pub(crate) use self::util::{create_static_alloc, type_implements_dyn_trait};
4242
pub use self::validity::{CtfeValidationMode, RangeSet, RefTracking};
4343
pub use self::visitor::ValueVisitor;

compiler/rustc_const_eval/src/interpret/step.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
8686
span = ?stmt.source_info.span,
8787
tracing_separate_thread = Empty,
8888
)
89-
.or_if_tracing_disabled(|| info!(stmt = ?stmt.kind));
89+
.or_if_tracing_disabled(|| info!("{:?}", stmt.kind));
9090

9191
use rustc_middle::mir::StatementKind::*;
9292

@@ -490,7 +490,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
490490
span = ?terminator.source_info.span,
491491
tracing_separate_thread = Empty,
492492
)
493-
.or_if_tracing_disabled(|| info!(terminator = ?terminator.kind));
493+
.or_if_tracing_disabled(|| info!("{:?}", terminator.kind));
494494

495495
use rustc_middle::mir::TerminatorKind::*;
496496
match terminator.kind {

compiler/rustc_const_eval/src/interpret/util.rs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,51 @@
1-
use rustc_hir::def_id::LocalDefId;
2-
use rustc_middle::mir;
1+
use rustc_hir::def_id::{CRATE_DEF_ID, LocalDefId};
2+
use rustc_infer::infer::TyCtxtInferExt;
3+
use rustc_infer::traits::{Obligation, ObligationCause};
34
use rustc_middle::mir::interpret::{AllocInit, Allocation, GlobalAlloc, InterpResult, Pointer};
45
use rustc_middle::ty::layout::TyAndLayout;
5-
use rustc_middle::ty::{TyCtxt, TypeVisitable, TypeVisitableExt};
6+
use rustc_middle::ty::{PolyExistentialPredicate, Ty, TyCtxt, TypeVisitable, TypeVisitableExt};
7+
use rustc_middle::{mir, span_bug, ty};
8+
use rustc_trait_selection::traits::ObligationCtxt;
69
use tracing::debug;
710

811
use super::{InterpCx, MPlaceTy, MemoryKind, interp_ok, throw_inval};
912
use crate::const_eval::{CompileTimeInterpCx, CompileTimeMachine, InterpretationResult};
13+
use crate::interpret::Machine;
14+
15+
/// Checks if a type implements predicates.
16+
/// Calls `ensure_monomorphic_enough` on `ty` and `trait_ty` for you.
17+
pub(crate) fn type_implements_dyn_trait<'tcx, M: Machine<'tcx>>(
18+
ecx: &mut InterpCx<'tcx, M>,
19+
ty: Ty<'tcx>,
20+
trait_ty: Ty<'tcx>,
21+
) -> InterpResult<'tcx, (bool, &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>)> {
22+
ensure_monomorphic_enough(ecx.tcx.tcx, ty)?;
23+
ensure_monomorphic_enough(ecx.tcx.tcx, trait_ty)?;
24+
25+
let ty::Dynamic(preds, _) = trait_ty.kind() else {
26+
span_bug!(
27+
ecx.find_closest_untracked_caller_location(),
28+
"Invalid type provided to type_implements_predicates. U must be dyn Trait, got {trait_ty}."
29+
);
30+
};
31+
32+
let (infcx, param_env) = ecx.tcx.infer_ctxt().build_with_typing_env(ecx.typing_env);
33+
34+
let ocx = ObligationCtxt::new(&infcx);
35+
ocx.register_obligations(preds.iter().map(|pred: PolyExistentialPredicate<'_>| {
36+
let pred = pred.with_self_ty(ecx.tcx.tcx, ty);
37+
// Lifetimes can only be 'static because of the bound on T
38+
let pred = rustc_middle::ty::fold_regions(ecx.tcx.tcx, pred, |r, _| {
39+
if r == ecx.tcx.tcx.lifetimes.re_erased { ecx.tcx.tcx.lifetimes.re_static } else { r }
40+
});
41+
Obligation::new(ecx.tcx.tcx, ObligationCause::dummy(), param_env, pred)
42+
}));
43+
let type_impls_trait = ocx.evaluate_obligations_error_on_ambiguity().is_empty();
44+
// Since `assumed_wf_tys=[]` the choice of LocalDefId is irrelevant, so using the "default"
45+
let regions_are_valid = ocx.resolve_regions(CRATE_DEF_ID, param_env, []).is_empty();
46+
47+
interp_ok((regions_are_valid && type_impls_trait, preds))
48+
}
1049

1150
/// Checks whether a type contains generic parameters which must be instantiated.
1251
///

compiler/rustc_driver_impl/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,8 +707,9 @@ fn print_crate_info(
707707
};
708708
let crate_name = passes::get_crate_name(sess, attrs);
709709
let lint_store = crate::unerased_lint_store(sess);
710-
let registered_tools = rustc_resolve::registered_tools_ast(sess.dcx(), attrs);
711710
let features = rustc_expand::config::features(sess, attrs, crate_name);
711+
let registered_tools =
712+
rustc_resolve::registered_tools_ast(sess.dcx(), attrs, sess, &features);
712713
let lint_levels = rustc_lint::LintLevelsBuilder::crate_root(
713714
sess,
714715
&features,

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,9 @@ pub enum AttributeKind {
11361136
/// Represents `#[reexport_test_harness_main]`
11371137
ReexportTestHarnessMain(Symbol),
11381138

1139+
/// Represents `#[register_tool]`
1140+
RegisterTool(ThinVec<Ident>, Span),
1141+
11391142
/// Represents [`#[repr]`](https://doc.rust-lang.org/stable/reference/type-layout.html#representations).
11401143
Repr {
11411144
reprs: ThinVec<(ReprAttr, Span)>,
@@ -1274,6 +1277,9 @@ pub enum AttributeKind {
12741277
/// Represents `#[rustc_if_this_changed]`
12751278
RustcIfThisChanged(Span, Option<Symbol>),
12761279

1280+
/// Represents `#[rustc_inherit_overflow_checks]`
1281+
RustcInheritOverflowChecks,
1282+
12771283
/// Represents `#[rustc_insignificant_dtor]`
12781284
RustcInsignificantDtor,
12791285

0 commit comments

Comments
 (0)