Skip to content

Commit 15892ff

Browse files
Rollup merge of rust-lang#119638 - lukas-code:suggest-constructor-cycle-error, r=cjgillot
fix cyle error when suggesting to use associated function instead of constructor Fixes rust-lang#119625. The first commit fixes the infinite recursion and makes the cycle error actually show up. We do this by making the `Display` for `ty::Instance` impl respect `with_no_queries` so that it can be used in query descriptions. The second commit fixes the cycle error `resolver_for_lowering` -> `normalize` -> `resolve_instance` (for evaluating const) -> `lang_items` (for `drop_in_place`) -> `resolver_for_lowering` (for collecting lang items). We do this by simply skipping the suggestion when encountering an unnormalized type.
2 parents 7ec840e + 339fa31 commit 15892ff

File tree

5 files changed

+47
-9
lines changed

5 files changed

+47
-9
lines changed

compiler/rustc_middle/src/ty/instance.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,16 @@ impl<'tcx> InstanceDef<'tcx> {
293293
fn fmt_instance(
294294
f: &mut fmt::Formatter<'_>,
295295
instance: &Instance<'_>,
296-
type_length: rustc_session::Limit,
296+
type_length: Option<rustc_session::Limit>,
297297
) -> fmt::Result {
298298
ty::tls::with(|tcx| {
299299
let args = tcx.lift(instance.args).expect("could not lift for printing");
300300

301-
let mut cx = FmtPrinter::new_with_limit(tcx, Namespace::ValueNS, type_length);
301+
let mut cx = if let Some(type_length) = type_length {
302+
FmtPrinter::new_with_limit(tcx, Namespace::ValueNS, type_length)
303+
} else {
304+
FmtPrinter::new(tcx, Namespace::ValueNS)
305+
};
302306
cx.print_def_path(instance.def_id(), args)?;
303307
let s = cx.into_buffer();
304308
f.write_str(&s)
@@ -324,13 +328,13 @@ pub struct ShortInstance<'a, 'tcx>(pub &'a Instance<'tcx>, pub usize);
324328

325329
impl<'a, 'tcx> fmt::Display for ShortInstance<'a, 'tcx> {
326330
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
327-
fmt_instance(f, self.0, rustc_session::Limit(self.1))
331+
fmt_instance(f, self.0, Some(rustc_session::Limit(self.1)))
328332
}
329333
}
330334

331335
impl<'tcx> fmt::Display for Instance<'tcx> {
332336
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
333-
ty::tls::with(|tcx| fmt_instance(f, self, tcx.type_length_limit()))
337+
fmt_instance(f, self, None)
334338
}
335339
}
336340

compiler/rustc_resolve/src/late/diagnostics.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1755,11 +1755,8 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
17551755
.filter_map(|item| {
17561756
// Only assoc fns that return `Self`
17571757
let fn_sig = self.r.tcx.fn_sig(item.def_id).skip_binder();
1758-
let ret_ty = fn_sig.output();
1759-
let ret_ty = self
1760-
.r
1761-
.tcx
1762-
.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), ret_ty);
1758+
// Don't normalize the return type, because that can cause cycle errors.
1759+
let ret_ty = fn_sig.output().skip_binder();
17631760
let ty::Adt(def, _args) = ret_ty.kind() else {
17641761
return None;
17651762
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
mod m {
2+
pub struct Uuid(());
3+
4+
impl Uuid {
5+
pub fn encode_buffer() -> [u8; LENGTH] {
6+
[]
7+
}
8+
}
9+
const LENGTH: usize = 0;
10+
}
11+
12+
pub use m::Uuid;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// aux-build:suggest-constructor-cycle-error.rs
2+
3+
// Regression test for https://github.com/rust-lang/rust/issues/119625
4+
5+
extern crate suggest_constructor_cycle_error as a;
6+
7+
const CONST_NAME: a::Uuid = a::Uuid(());
8+
//~^ ERROR: cannot initialize a tuple struct which contains private fields [E0423]
9+
10+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0423]: cannot initialize a tuple struct which contains private fields
2+
--> $DIR/suggest-constructor-cycle-error.rs:7:29
3+
|
4+
LL | const CONST_NAME: a::Uuid = a::Uuid(());
5+
| ^^^^^^^
6+
|
7+
note: constructor is not visible here due to private fields
8+
--> $DIR/auxiliary/suggest-constructor-cycle-error.rs:2:21
9+
|
10+
LL | pub struct Uuid(());
11+
| ^^ private field
12+
13+
error: aborting due to 1 previous error
14+
15+
For more information about this error, try `rustc --explain E0423`.

0 commit comments

Comments
 (0)