Skip to content

Commit f22a0c2

Browse files
committed
Auto merge of #123594 - Urgau:fix-non_local_def-lint-overflow, r=lcnr
Fix trait solver overflow with `non_local_definitions` lint This PR fixes the trait solver overflow with the `non_local_definitions` lint reported in #123573 using the suggestion from `@lcnr:` #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 #123573 r? `@lcnr`
2 parents aa31bad + c2e2245 commit f22a0c2

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

compiler/rustc_lint/src/non_local_def.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,19 @@ 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+
// Which is unacceptable for a lint.
272+
//
273+
// Thanksfully the part we use here are very similar to the
274+
// new-trait-solver-as-coherence, which is in stabilization.
275+
//
276+
// https://github.com/rust-lang/rust/issues/123573
277+
.with_next_trait_solver(true)
278+
.build();
279+
268280
let trait_ref = binder.instantiate(tcx, infcx.fresh_args_for_item(infer_span, trait_def_id));
269281

270282
let trait_ref = trait_ref.fold_with(&mut ReplaceLocalTypesWithInfer {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//@ check-pass
2+
//@ edition:2021
3+
4+
// https://github.com/rust-lang/rust/issues/123573#issue-2229428739
5+
6+
pub trait Test {}
7+
8+
impl<'a, T: 'a> Test for &[T] where &'a T: Test {}
9+
10+
fn main() {
11+
struct Local {}
12+
impl Test for &Local {}
13+
//~^ WARN non-local `impl` definition
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
warning: non-local `impl` definition, they should be avoided as they go against expectation
2+
--> $DIR/trait-solver-overflow-123573.rs:12:5
3+
|
4+
LL | impl Test for &Local {}
5+
| ^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= help: move this `impl` block outside the of the current function `main`
8+
= 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`
9+
= 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
10+
= 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>
11+
= note: `#[warn(non_local_definitions)]` on by default
12+
13+
warning: 1 warning emitted
14+

0 commit comments

Comments
 (0)