Skip to content

type_complexity: ignore opaque impl Trait bounds - #17463

Open
jesco-absolut wants to merge 3 commits into
rust-lang:masterfrom
jesco-absolut:fix-17195-type-complexity-impl-trait
Open

type_complexity: ignore opaque impl Trait bounds#17463
jesco-absolut wants to merge 3 commits into
rust-lang:masterfrom
jesco-absolut:fix-17195-type-complexity-impl-trait

Conversation

@jesco-absolut

@jesco-absolut jesco-absolut commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

changelog: [type_complexity]: avoid false positives for types containing opaque impl Trait bounds

fixes #17195

Opaque impl Trait bounds cannot be extracted into a stable type alias, so counting their internal structure can produce a warning with no usable refactoring. This change stops complexity scoring inside an opaque type while continuing to score surrounding containers and independently nameable sibling types.

The UI coverage includes the nested impl FnMut reproducer, a complex bound wholly inside an opaque return type, and an opaque-first tuple whose separate complex sibling must still trigger the lint.

Tested with:

  • TESTNAME=type_complexity cargo uitest — both matching UI suites passed
  • cargo fmt --all --check

@rustbot rustbot added S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jul 26, 2026
@rustbot

rustbot commented Jul 26, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request. A reviewer will take a look after it receives 2 community reviews.

In the meantime, we would highly appreciate if you could try to review any of PRs waiting on community reviews.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@jesco-absolut
jesco-absolut force-pushed the fix-17195-type-complexity-impl-trait branch from 998635d to e29598b Compare July 26, 2026 21:31
@github-actions

github-actions Bot commented Jul 26, 2026

Copy link
Copy Markdown

Lintcheck changes for f2830c4

Lint Added Removed Changed
clippy::type_complexity 0 3 0

This comment will be updated if you push new changes

@CommanderStorm CommanderStorm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Community review:

Looks like a good start, but I think this likely needs a bit better integration, or?
Maybe I am missing something why it cant, that is possible. I am new here.

View changes since this review

Comment thread tests/ui/type_complexity.rs Outdated
@@ -87,3 +87,27 @@ struct D {
}

fn main() {}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the "usual" approach that I have seen across the codebase is that there is fn main with the initial impl and then fn issue_xxx with the changes after this.

Can we also make this file follow this pattern or is there a reason why we are not?

Comment thread tests/ui/type_complexity.rs Outdated

// The presence of an opaque type must not hide complexity in a sibling type that can be factored
// out.
#[expect(clippy::type_complexity)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of expecting, please use the existing infra with //~^ type_complexity and the error messages

Comment on lines +45 to +49
// Opaque types cannot be extracted into type aliases on stable. Ignore their
// bounds, but keep scoring surrounding type components that can be extracted.
if matches!(ty.kind, TyKind::OpaqueDef(..)) {
return;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems a bit blunt of a hammer doesnt it?

A pattern that I have seen is this. Coud you try and make this work this way?

msrv.is_stable(cx, item.def_id)

Not sure if this a bad suggestion or if there is better infra around.

@CommanderStorm CommanderStorm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Community review:

Good progress. I still have some comments left, but looks much better than before

View changes since this review

Comment on lines +51 to +53
// Opaque `impl Trait` types cannot be extracted into type aliases on stable. Skip
// their subtree only when `type_alias_impl_trait` is not enabled for this crate.
let skip_nested_type = matches!(ty.kind, TyKind::OpaqueDef(..)) && !self.type_alias_impl_trait_enabled;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you just inline it below?
I think the code would be simpler to read for future readers given the negations of negations going on here.

Comment thread tests/ui/type_complexity.rs Outdated
Comment on lines +101 to +102
// Complexity inside an opaque type cannot be factored into a type alias either.
fn complex_opaque_bound() -> impl Fn(Vec<Vec<Box<(u32, u32, u32, u32)>>>) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 Why can't it?
as far as I tried this, it defitively can..

Is there maybe a typo in the comment or what do you mean here?

type Foo = Vec<Vec<Box<(u32, u32, u32, u32)>>>;

fn complex_opaque_bound() -> impl Fn(Foo) {
   |_|{}
}

Comment thread tests/ui/type_complexity.rs Outdated
// The presence of an opaque type must not hide complexity in a sibling type that can be factored
// out.
fn complex_after_opaque() -> (impl Iterator<Item = u32>, Vec<Vec<Box<(u32, u32, u32, u32)>>>) {
//~^ ERROR: very complex type used. Consider factoring parts into `type` definitions

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see the rest of the code

Suggested change
//~^ ERROR: very complex type used. Consider factoring parts into `type` definitions
//~^ type_complexity

#![warn(clippy::type_complexity)]

fn complex_opaque_bound() -> impl Fn(Vec<Vec<Box<(u32, u32, u32, u32)>>>) {
//~^ ERROR: very complex type used. Consider factoring parts into `type` definitions

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//~^ ERROR: very complex type used. Consider factoring parts into `type` definitions
//~^ type_complexity

Inline the stable opaque-type skip at the traversal site, remove a misleading stable test case that can be factored through a normal alias, and use the existing UI shorthand lint annotations requested in review.

@CommanderStorm CommanderStorm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please actually answer my question in #17463 (comment) by commenting!

I did not ask you to remove the testcase, I asked you why this is the case.
A sneaky false-nagative is also not great.

View changes since this review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties

Projects

None yet

Development

Successfully merging this pull request may close these issues.

type_complexity should not trigger on types using impl trait

3 participants