Skip to content

Commit fff3bb1

Browse files
committed
Auto merge of rust-lang#123594 - Urgau:fix-non_local_def-lint-overflow, r=<try>
Fix trait solver overflow with `non_local_definitions` lint This PR fixes the trait solver overflow with the `non_local_definitions` lint reported in rust-lang#123573 using the suggestion from `@lcnr:` rust-lang#123573 (comment) to use the next trait solver. ~~I have not (yet) tried to create a minimized repro~~ ``@compiler-errors`` did the minimization (thanks you) but I have manually tested on the `starlark-rust` project that it fixes the issue. Fixes rust-lang#123573 r? `@lcnr`
2 parents 7a495cc + 5250336 commit fff3bb1

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

compiler/rustc_lint/src/non_local_def.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,18 @@ fn impl_trait_ref_has_enough_non_local_candidates<'tcx>(
264264
binder: EarlyBinder<TraitRef<'tcx>>,
265265
mut did_has_local_parent: impl FnMut(DefId) -> bool,
266266
) -> bool {
267-
let infcx = tcx.infer_ctxt().build();
267+
let infcx = tcx
268+
.infer_ctxt()
269+
// We use the new trait solver since the obligation we are trying to
270+
// prove here may overflow and those are fatal in the old trait solver.
271+
//
272+
// Which is unacceptable. Thanksfully the part we use here are very
273+
// similar to the new-trait-solver-as-coherence, which is in stabilization.
274+
//
275+
// https://github.com/rust-lang/rust/issues/123573
276+
.with_next_trait_solver(true)
277+
.build();
278+
268279
let trait_ref = binder.instantiate(tcx, infcx.fresh_args_for_item(infer_span, trait_def_id));
269280

270281
let trait_ref = trait_ref.fold_with(&mut ReplaceLocalTypesWithInfer {

tests/ui/lint/non_local_definitions.rs

+11
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,17 @@ non_local_macro::non_local_impl!(CargoUpdate);
461461
non_local_macro::non_local_macro_rules!(my_macro);
462462
//~^ WARN non-local `macro_rules!` definition
463463

464+
// https://github.com/rust-lang/rust/issues/123573#issue-2229428739
465+
pub trait StarlarkTypeRepr {}
466+
467+
impl<'a, T: 'a> StarlarkTypeRepr for &[T] where &'a T: StarlarkTypeRepr {}
468+
469+
fn starlark() {
470+
struct Local {}
471+
impl StarlarkTypeRepr for &Local {}
472+
//~^ WARN non-local `impl` definition
473+
}
474+
464475
fn bitflags() {
465476
struct Flags;
466477

tests/ui/lint/non_local_definitions.stderr

+12-1
Original file line numberDiff line numberDiff line change
@@ -701,5 +701,16 @@ LL | non_local_macro::non_local_macro_rules!(my_macro);
701701
= note: the macro `non_local_macro::non_local_macro_rules` may come from an old version of the `non_local_macro` crate, try updating your dependency with `cargo update -p non_local_macro`
702702
= note: this warning originates in the macro `non_local_macro::non_local_macro_rules` (in Nightly builds, run with -Z macro-backtrace for more info)
703703

704-
warning: 55 warnings emitted
704+
warning: non-local `impl` definition, they should be avoided as they go against expectation
705+
--> $DIR/non_local_definitions.rs:471:5
706+
|
707+
LL | impl StarlarkTypeRepr for &Local {}
708+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
709+
|
710+
= help: move this `impl` block outside the of the current function `starlark`
711+
= note: an `impl` definition is non-local if it is nested inside an item and may impact type checking outside of that item. This can be the case if neither the trait or the self type are at the same nesting level as the `impl`
712+
= note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
713+
= note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <https://github.com/rust-lang/rust/issues/120363>
714+
715+
warning: 56 warnings emitted
705716

0 commit comments

Comments
 (0)