Skip to content

Commit c872a14

Browse files
committed
Auto merge of #125507 - compiler-errors:type-length-limit, r=lcnr
Re-implement a type-size based limit r? lcnr This PR reintroduces the type length limit added in #37789, which was accidentally made practically useless by the caching changes to `Ty::walk` in #72412, which caused the `walk` function to no longer walk over identical elements. Hitting this length limit is not fatal unless we are in codegen -- so it shouldn't affect passes like the mir inliner which creates potentially very large types (which we observed, for example, when the new trait solver compiles `itertools` in `--release` mode). This also increases the type length limit from `1048576 == 2 ** 20` to `2 ** 24`, which covers all of the code that can be reached with craterbot-check. Individual crates can increase the length limit further if desired. Perf regression is mild and I think we should accept it -- reinstating this limit is important for the new trait solver and to make sure we don't accidentally hit more type-size related regressions in the future. Fixes #125460
2 parents 2db4ff4 + b1059cc commit c872a14

File tree

54 files changed

+358
-266
lines changed

Some content is hidden

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

54 files changed

+358
-266
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3733,7 +3733,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
37333733
if tcx.is_diagnostic_item(sym::deref_method, method_did) {
37343734
let deref_target =
37353735
tcx.get_diagnostic_item(sym::deref_target).and_then(|deref_target| {
3736-
Instance::resolve(tcx, self.param_env, deref_target, method_args)
3736+
Instance::try_resolve(tcx, self.param_env, deref_target, method_args)
37373737
.transpose()
37383738
});
37393739
if let Some(Ok(instance)) = deref_target {

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
949949
return;
950950
}
951951

952-
if let Ok(Some(instance)) = ty::Instance::resolve(
952+
if let Ok(Some(instance)) = ty::Instance::try_resolve(
953953
tcx,
954954
self.param_env,
955955
*fn_did,

compiler/rustc_codegen_cranelift/src/abi/mod.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,14 @@ pub(crate) fn codegen_terminator_call<'tcx>(
371371

372372
// Handle special calls like intrinsics and empty drop glue.
373373
let instance = if let ty::FnDef(def_id, fn_args) = *func.layout().ty.kind() {
374-
let instance =
375-
ty::Instance::expect_resolve(fx.tcx, ty::ParamEnv::reveal_all(), def_id, fn_args)
376-
.polymorphize(fx.tcx);
374+
let instance = ty::Instance::expect_resolve(
375+
fx.tcx,
376+
ty::ParamEnv::reveal_all(),
377+
def_id,
378+
fn_args,
379+
source_info.span,
380+
)
381+
.polymorphize(fx.tcx);
377382

378383
if is_call_from_compiler_builtins_to_upstream_monomorphization(fx.tcx, instance) {
379384
if target.is_some() {

compiler/rustc_codegen_cranelift/src/main_shim.rs

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rustc_middle::ty::AssocKind;
44
use rustc_middle::ty::GenericArg;
55
use rustc_session::config::{sigpipe, EntryFnType};
66
use rustc_span::symbol::Ident;
7+
use rustc_span::DUMMY_SP;
78

89
use crate::prelude::*;
910

@@ -119,6 +120,7 @@ pub(crate) fn maybe_create_entry_wrapper(
119120
ParamEnv::reveal_all(),
120121
report.def_id,
121122
tcx.mk_args(&[GenericArg::from(main_ret_ty)]),
123+
DUMMY_SP,
122124
)
123125
.polymorphize(tcx);
124126

@@ -144,6 +146,7 @@ pub(crate) fn maybe_create_entry_wrapper(
144146
ParamEnv::reveal_all(),
145147
start_def_id,
146148
tcx.mk_args(&[main_ret_ty.into()]),
149+
DUMMY_SP,
147150
)
148151
.polymorphize(tcx);
149152
let start_func_id = import_function(tcx, m, start_instance);

compiler/rustc_codegen_gcc/src/context.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_middle::ty::layout::{
1717
};
1818
use rustc_middle::ty::{self, Instance, ParamEnv, PolyExistentialTraitRef, Ty, TyCtxt};
1919
use rustc_session::Session;
20-
use rustc_span::{source_map::respan, Span};
20+
use rustc_span::{source_map::respan, Span, DUMMY_SP};
2121
use rustc_target::abi::{
2222
call::FnAbi, HasDataLayout, PointeeInfo, Size, TargetDataLayout, VariantIdx,
2323
};
@@ -479,6 +479,7 @@ impl<'gcc, 'tcx> MiscMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
479479
ty::ParamEnv::reveal_all(),
480480
def_id,
481481
ty::List::empty(),
482+
DUMMY_SP,
482483
);
483484

484485
let symbol_name = tcx.symbol_name(instance).name;

compiler/rustc_codegen_llvm/src/context.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use rustc_session::config::{BranchProtection, CFGuard, CFProtection};
2828
use rustc_session::config::{CrateType, DebugInfo, PAuthKey, PacRet};
2929
use rustc_session::Session;
3030
use rustc_span::source_map::Spanned;
31-
use rustc_span::Span;
31+
use rustc_span::{Span, DUMMY_SP};
3232
use rustc_target::abi::{call::FnAbi, HasDataLayout, TargetDataLayout, VariantIdx};
3333
use rustc_target::spec::{HasTargetSpec, RelocModel, Target, TlsModel};
3434
use smallvec::SmallVec;
@@ -580,6 +580,7 @@ impl<'ll, 'tcx> MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
580580
ty::ParamEnv::reveal_all(),
581581
def_id,
582582
ty::List::empty(),
583+
DUMMY_SP,
583584
)),
584585
_ => {
585586
let name = name.unwrap_or("rust_eh_personality");

compiler/rustc_codegen_ssa/src/base.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
3737
use rustc_session::config::{self, CrateType, EntryFnType, OptLevel, OutputType};
3838
use rustc_session::Session;
3939
use rustc_span::symbol::sym;
40-
use rustc_span::Symbol;
40+
use rustc_span::{Symbol, DUMMY_SP};
4141
use rustc_target::abi::FIRST_VARIANT;
4242

4343
use std::cmp;
@@ -467,6 +467,7 @@ pub fn maybe_create_entry_wrapper<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
467467
ty::ParamEnv::reveal_all(),
468468
start_def_id,
469469
cx.tcx().mk_args(&[main_ret_ty.into()]),
470+
DUMMY_SP,
470471
);
471472
let start_fn = cx.get_fn_addr(start_instance);
472473

compiler/rustc_codegen_ssa/src/mir/block.rs

+1
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
842842
ty::ParamEnv::reveal_all(),
843843
def_id,
844844
args,
845+
fn_span,
845846
)
846847
.polymorphize(bx.tcx()),
847848
),

compiler/rustc_const_eval/src/check_consts/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
768768
is_trait = true;
769769

770770
if let Ok(Some(instance)) =
771-
Instance::resolve(tcx, param_env, callee, fn_args)
771+
Instance::try_resolve(tcx, param_env, callee, fn_args)
772772
&& let InstanceKind::Item(def) = instance.def
773773
{
774774
// Resolve a trait method call to its concrete implementation, which may be in a

compiler/rustc_const_eval/src/const_eval/machine.rs

+1
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ impl<'tcx> CompileTimeInterpCx<'tcx> {
253253
ty::ParamEnv::reveal_all(),
254254
const_def_id,
255255
instance.args,
256+
self.cur_span(),
256257
);
257258

258259
return Ok(Some(new_instance));

compiler/rustc_const_eval/src/interpret/eval_context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
618618
trace!("resolve: {:?}, {:#?}", def, args);
619619
trace!("param_env: {:#?}", self.param_env);
620620
trace!("args: {:#?}", args);
621-
match ty::Instance::resolve(*self.tcx, self.param_env, def, args) {
621+
match ty::Instance::try_resolve(*self.tcx, self.param_env, def, args) {
622622
Ok(Some(instance)) => Ok(instance),
623623
Ok(None) => throw_inval!(TooGeneric),
624624

compiler/rustc_const_eval/src/interpret/terminator.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -883,13 +883,13 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
883883
ty::ExistentialTraitRef::erase_self_ty(tcx, virtual_trait_ref);
884884
let concrete_trait_ref = existential_trait_ref.with_self_ty(tcx, dyn_ty);
885885

886-
let concrete_method = Instance::resolve_for_vtable(
886+
let concrete_method = Instance::expect_resolve_for_vtable(
887887
tcx,
888888
self.param_env,
889889
def_id,
890890
instance.args.rebase_onto(tcx, trait_def_id, concrete_trait_ref.args),
891-
)
892-
.unwrap();
891+
self.cur_span(),
892+
);
893893
assert_eq!(fn_inst, concrete_method);
894894
}
895895

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
534534
let tcx = self.tcx();
535535

536536
// Find the method being called.
537-
let Ok(Some(instance)) = ty::Instance::resolve(
537+
let Ok(Some(instance)) = ty::Instance::try_resolve(
538538
tcx,
539539
ctxt.param_env,
540540
ctxt.assoc_item.def_id,

compiler/rustc_lint/src/internal.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ declare_lint_pass!(QueryStability => [POTENTIAL_QUERY_INSTABILITY]);
8888
impl LateLintPass<'_> for QueryStability {
8989
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
9090
let Some((span, def_id, args)) = typeck_results_of_method_fn(cx, expr) else { return };
91-
if let Ok(Some(instance)) = ty::Instance::resolve(cx.tcx, cx.param_env, def_id, args) {
91+
if let Ok(Some(instance)) = ty::Instance::try_resolve(cx.tcx, cx.param_env, def_id, args) {
9292
let def_id = instance.def_id();
9393
if cx.tcx.has_attr(def_id, sym::rustc_lint_query_instability) {
9494
cx.emit_span_lint(
@@ -393,7 +393,7 @@ impl LateLintPass<'_> for Diagnostics {
393393
};
394394

395395
// Is the callee marked with `#[rustc_lint_diagnostics]`?
396-
let has_attr = ty::Instance::resolve(cx.tcx, cx.param_env, def_id, fn_gen_args)
396+
let has_attr = ty::Instance::try_resolve(cx.tcx, cx.param_env, def_id, fn_gen_args)
397397
.ok()
398398
.flatten()
399399
.is_some_and(|inst| cx.tcx.has_attr(inst.def_id(), sym::rustc_lint_diagnostics));

compiler/rustc_lint/src/noop_method_call.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
9696
.tcx
9797
.normalize_erasing_regions(cx.param_env, cx.typeck_results().node_args(expr.hir_id));
9898
// Resolve the trait method instance.
99-
let Ok(Some(i)) = ty::Instance::resolve(cx.tcx, cx.param_env, did, args) else { return };
99+
let Ok(Some(i)) = ty::Instance::try_resolve(cx.tcx, cx.param_env, did, args) else {
100+
return;
101+
};
100102
// (Re)check that it implements the noop diagnostic.
101103
let Some(name) = cx.tcx.get_diagnostic_name(i.def_id()) else { return };
102104
if !matches!(

compiler/rustc_middle/messages.ftl

+6
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ middle_cannot_be_normalized =
4141
middle_conflict_types =
4242
this expression supplies two conflicting concrete types for the same opaque type
4343
44+
middle_consider_type_length_limit =
45+
consider adding a `#![type_length_limit="{$type_length}"]` attribute to your crate
46+
4447
middle_const_eval_non_int =
4548
constant evaluation of enum discriminant resulted in non-integer
4649
@@ -94,8 +97,11 @@ middle_strict_coherence_needs_negative_coherence =
9497
to use `strict_coherence` on this trait, the `with_negative_coherence` feature must be enabled
9598
.label = due to this attribute
9699
100+
middle_type_length_limit = reached the type-length limit while instantiating `{$shrunk}`
101+
97102
middle_unknown_layout =
98103
the type `{$ty}` has an unknown layout
99104
100105
middle_values_too_big =
101106
values of the type `{$ty}` are too big for the current architecture
107+
middle_written_to_path = the full type name has been written to '{$path}'

compiler/rustc_middle/src/error.rs

+14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::fmt;
2+
use std::path::PathBuf;
23

34
use rustc_errors::{codes::*, DiagArgName, DiagArgValue, DiagMessage};
45
use rustc_macros::{Diagnostic, Subdiagnostic};
@@ -149,3 +150,16 @@ pub struct ErroneousConstant {
149150

150151
/// Used by `rustc_const_eval`
151152
pub use crate::fluent_generated::middle_adjust_for_foreign_abi_error;
153+
154+
#[derive(Diagnostic)]
155+
#[diag(middle_type_length_limit)]
156+
#[help(middle_consider_type_length_limit)]
157+
pub struct TypeLengthLimit {
158+
#[primary_span]
159+
pub span: Span,
160+
pub shrunk: String,
161+
#[note(middle_written_to_path)]
162+
pub was_written: Option<()>,
163+
pub path: PathBuf,
164+
pub type_length: usize,
165+
}

compiler/rustc_middle/src/middle/limits.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub fn provide(providers: &mut Providers) {
3030
tcx.hir().krate_attrs(),
3131
tcx.sess,
3232
sym::type_length_limit,
33-
1048576,
33+
2usize.pow(24),
3434
),
3535
}
3636
}

compiler/rustc_middle/src/mir/interpret/queries.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl<'tcx> TyCtxt<'tcx> {
7373
bug!("did not expect inference variables here");
7474
}
7575

76-
match ty::Instance::resolve(
76+
match ty::Instance::try_resolve(
7777
self, param_env,
7878
// FIXME: maybe have a separate version for resolving mir::UnevaluatedConst?
7979
ct.def, ct.args,
@@ -106,7 +106,7 @@ impl<'tcx> TyCtxt<'tcx> {
106106
bug!("did not expect inference variables here");
107107
}
108108

109-
match ty::Instance::resolve(self, param_env, ct.def, ct.args) {
109+
match ty::Instance::try_resolve(self, param_env, ct.def, ct.args) {
110110
Ok(Some(instance)) => {
111111
let cid = GlobalId { instance, promoted: None };
112112
self.const_eval_global_id_for_typeck(param_env, cid, span).inspect(|_| {

compiler/rustc_middle/src/query/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2197,8 +2197,8 @@ rustc_queries! {
21972197
/// * `Err(ErrorGuaranteed)` when the `Instance` resolution process
21982198
/// couldn't complete due to errors elsewhere - this is distinct
21992199
/// from `Ok(None)` to avoid misleading diagnostics when an error
2200-
/// has already been/will be emitted, for the original cause
2201-
query resolve_instance(
2200+
/// has already been/will be emitted, for the original cause.
2201+
query resolve_instance_raw(
22022202
key: ty::ParamEnvAnd<'tcx, (DefId, GenericArgsRef<'tcx>)>
22032203
) -> Result<Option<ty::Instance<'tcx>>, ErrorGuaranteed> {
22042204
desc { "resolving instance `{}`", ty::Instance::new(key.value.0, key.value.1) }

0 commit comments

Comments
 (0)