From 274674819c3c37e09f5fe0e3276d3921068dbb95 Mon Sep 17 00:00:00 2001 From: Lukas Markeffsky <@> Date: Fri, 5 Jan 2024 21:50:52 +0100 Subject: [PATCH 1/2] fix OOM when `ty::Instance` is used in query description --- compiler/rustc_middle/src/ty/instance.rs | 12 ++++++++---- .../auxiliary/suggest-constructor-cycle-error.rs | 12 ++++++++++++ .../ui/resolve/suggest-constructor-cycle-error.rs | 10 ++++++++++ .../suggest-constructor-cycle-error.stderr | 15 +++++++++++++++ 4 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 tests/ui/resolve/auxiliary/suggest-constructor-cycle-error.rs create mode 100644 tests/ui/resolve/suggest-constructor-cycle-error.rs create mode 100644 tests/ui/resolve/suggest-constructor-cycle-error.stderr diff --git a/compiler/rustc_middle/src/ty/instance.rs b/compiler/rustc_middle/src/ty/instance.rs index 2ac3cddfa15ad..dd41cb5a61f44 100644 --- a/compiler/rustc_middle/src/ty/instance.rs +++ b/compiler/rustc_middle/src/ty/instance.rs @@ -293,12 +293,16 @@ impl<'tcx> InstanceDef<'tcx> { fn fmt_instance( f: &mut fmt::Formatter<'_>, instance: &Instance<'_>, - type_length: rustc_session::Limit, + type_length: Option, ) -> fmt::Result { ty::tls::with(|tcx| { let args = tcx.lift(instance.args).expect("could not lift for printing"); - let mut cx = FmtPrinter::new_with_limit(tcx, Namespace::ValueNS, type_length); + let mut cx = if let Some(type_length) = type_length { + FmtPrinter::new_with_limit(tcx, Namespace::ValueNS, type_length) + } else { + FmtPrinter::new(tcx, Namespace::ValueNS) + }; cx.print_def_path(instance.def_id(), args)?; let s = cx.into_buffer(); f.write_str(&s) @@ -324,13 +328,13 @@ pub struct ShortInstance<'a, 'tcx>(pub &'a Instance<'tcx>, pub usize); impl<'a, 'tcx> fmt::Display for ShortInstance<'a, 'tcx> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt_instance(f, self.0, rustc_session::Limit(self.1)) + fmt_instance(f, self.0, Some(rustc_session::Limit(self.1))) } } impl<'tcx> fmt::Display for Instance<'tcx> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - ty::tls::with(|tcx| fmt_instance(f, self, tcx.type_length_limit())) + fmt_instance(f, self, None) } } diff --git a/tests/ui/resolve/auxiliary/suggest-constructor-cycle-error.rs b/tests/ui/resolve/auxiliary/suggest-constructor-cycle-error.rs new file mode 100644 index 0000000000000..8de68c38bc34d --- /dev/null +++ b/tests/ui/resolve/auxiliary/suggest-constructor-cycle-error.rs @@ -0,0 +1,12 @@ +mod m { + pub struct Uuid(()); + + impl Uuid { + pub fn encode_buffer() -> [u8; LENGTH] { + [] + } + } + const LENGTH: usize = 0; +} + +pub use m::Uuid; diff --git a/tests/ui/resolve/suggest-constructor-cycle-error.rs b/tests/ui/resolve/suggest-constructor-cycle-error.rs new file mode 100644 index 0000000000000..2336cd92f4d33 --- /dev/null +++ b/tests/ui/resolve/suggest-constructor-cycle-error.rs @@ -0,0 +1,10 @@ +// aux-build:suggest-constructor-cycle-error.rs +//~^ cycle detected when getting the resolver for lowering [E0391] + +// Regression test for https://github.com/rust-lang/rust/issues/119625 + +extern crate suggest_constructor_cycle_error as a; + +const CONST_NAME: a::Uuid = a::Uuid(()); + +fn main() {} diff --git a/tests/ui/resolve/suggest-constructor-cycle-error.stderr b/tests/ui/resolve/suggest-constructor-cycle-error.stderr new file mode 100644 index 0000000000000..ae139619c695c --- /dev/null +++ b/tests/ui/resolve/suggest-constructor-cycle-error.stderr @@ -0,0 +1,15 @@ +error[E0391]: cycle detected when getting the resolver for lowering + | + = note: ...which requires normalizing `[u8; suggest_constructor_cycle_error::::m::{impl#0}::encode_buffer::{constant#0}]`... +note: ...which requires resolving instance `suggest_constructor_cycle_error::m::Uuid::encode_buffer::{constant#0}`... + --> $DIR/auxiliary/suggest-constructor-cycle-error.rs:5:40 + | +LL | pub fn encode_buffer() -> [u8; LENGTH] { + | ^^^^^^ + = note: ...which requires calculating the lang items map... + = note: ...which again requires getting the resolver for lowering, completing the cycle + = note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0391`. From 339fa311ad8140cfb98a1c6080ea7e002b9ce3fa Mon Sep 17 00:00:00 2001 From: Lukas Markeffsky <@> Date: Fri, 5 Jan 2024 21:55:09 +0100 Subject: [PATCH 2/2] fix cycle error for "use constructor" suggestion --- .../rustc_resolve/src/late/diagnostics.rs | 7 ++----- .../suggest-constructor-cycle-error.rs | 2 +- .../suggest-constructor-cycle-error.stderr | 20 +++++++++---------- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 6c38ed6227038..1ccb4820cdd9f 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -1755,11 +1755,8 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { .filter_map(|item| { // Only assoc fns that return `Self` let fn_sig = self.r.tcx.fn_sig(item.def_id).skip_binder(); - let ret_ty = fn_sig.output(); - let ret_ty = self - .r - .tcx - .normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), ret_ty); + // Don't normalize the return type, because that can cause cycle errors. + let ret_ty = fn_sig.output().skip_binder(); let ty::Adt(def, _args) = ret_ty.kind() else { return None; }; diff --git a/tests/ui/resolve/suggest-constructor-cycle-error.rs b/tests/ui/resolve/suggest-constructor-cycle-error.rs index 2336cd92f4d33..e36fffd97d1b4 100644 --- a/tests/ui/resolve/suggest-constructor-cycle-error.rs +++ b/tests/ui/resolve/suggest-constructor-cycle-error.rs @@ -1,10 +1,10 @@ // aux-build:suggest-constructor-cycle-error.rs -//~^ cycle detected when getting the resolver for lowering [E0391] // Regression test for https://github.com/rust-lang/rust/issues/119625 extern crate suggest_constructor_cycle_error as a; const CONST_NAME: a::Uuid = a::Uuid(()); +//~^ ERROR: cannot initialize a tuple struct which contains private fields [E0423] fn main() {} diff --git a/tests/ui/resolve/suggest-constructor-cycle-error.stderr b/tests/ui/resolve/suggest-constructor-cycle-error.stderr index ae139619c695c..c6ec2465a432c 100644 --- a/tests/ui/resolve/suggest-constructor-cycle-error.stderr +++ b/tests/ui/resolve/suggest-constructor-cycle-error.stderr @@ -1,15 +1,15 @@ -error[E0391]: cycle detected when getting the resolver for lowering +error[E0423]: cannot initialize a tuple struct which contains private fields + --> $DIR/suggest-constructor-cycle-error.rs:7:29 | - = note: ...which requires normalizing `[u8; suggest_constructor_cycle_error::::m::{impl#0}::encode_buffer::{constant#0}]`... -note: ...which requires resolving instance `suggest_constructor_cycle_error::m::Uuid::encode_buffer::{constant#0}`... - --> $DIR/auxiliary/suggest-constructor-cycle-error.rs:5:40 +LL | const CONST_NAME: a::Uuid = a::Uuid(()); + | ^^^^^^^ | -LL | pub fn encode_buffer() -> [u8; LENGTH] { - | ^^^^^^ - = note: ...which requires calculating the lang items map... - = note: ...which again requires getting the resolver for lowering, completing the cycle - = note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information +note: constructor is not visible here due to private fields + --> $DIR/auxiliary/suggest-constructor-cycle-error.rs:2:21 + | +LL | pub struct Uuid(()); + | ^^ private field error: aborting due to 1 previous error -For more information about this error, try `rustc --explain E0391`. +For more information about this error, try `rustc --explain E0423`.