Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: New lint trait_no_longer_object_safe #659

Merged
merged 11 commits into from
Feb 13, 2024

Conversation

devanbenz
Copy link
Contributor

  • Adds a new lint for traits to see if they are still object safe

@devanbenz
Copy link
Contributor Author

@obi1kenobi Having a few issues with this PR. I'm noticing that the test is failing with the following error:

thread 'query::tests_lints::trait_no_longer_object_safe' panicked at src/query.rs:342:13:
The query produced a non-empty output when it compared two crates with the same rustdoc.
Query trait_no_longer_object_safe produced incorrect output (./src/lints/trait_no_longer_object_safe.ron).

Differences (-expected (empty)|+actual (trait_now_doc_hidden/new)):
-{}
+{
+    "./test_crates/trait_now_doc_hidden/": [
+        {
+            "name": String("AliasedAsDocHidden"),
+            "path": List([
+                String("trait_now_doc_hidden"),
+                String("AliasedAsDocHidden"),
+            ]),
+            "span_begin_line": Uint64(44),
+            "span_filename": String("src/lib.rs"),
+            "visibility_limit": String("public"),
+        },
+        {
+            "name": String("PublicTraitHiddenVariant"),
+            "path": List([
+                String("trait_now_doc_hidden"),
+                String("PublicTraitHiddenVariant"),
+            ]),
+            "span_begin_line": Uint64(49),
+            "span_filename": String("src/lib.rs"),
+            "visibility_limit": String("public"),
+        },
+        {
+            "name": String("PublicTraitDocumentedWithStringHidden"),
+            "path": List([
+                String("trait_now_doc_hidden"),
+                String("PublicTraitDocumentedWithStringHidden"),
+            ]),
+            "span_begin_line": Uint64(62),
+            "span_filename": String("src/lib.rs"),
+            "visibility_limit": String("public"),
+        },
+    ],
+}

It looks like its picking up a query from a different lint. When I go through and try to debug what exactly is happening I see that https://github.com/devanbenz/cargo-semver-checks/blob/ea9a307765ea5d80b12bf8ada784019ad3f23cab/src/query.rs#L140 is outputting trait_no_longer_object_safe in the test crates returned array which looks okay. Looking here https://github.com/devanbenz/cargo-semver-checks/blob/ea9a307765ea5d80b12bf8ada784019ad3f23cab/src/query.rs#L325 getting the output of crate_pair_path I'm seeing

"./test_crates/function_changed_abi/"
"./test_crates/function_changed_abi/"
"./test_crates/trait_now_doc_hidden/"

Which is the incorrect crate path 🤔

// Non-object safe traits
pub trait RefTrait {
fn by_ref(self) -> Self;
}
Copy link
Owner

Choose a reason for hiding this comment

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

Would you mind configuring your editor to always end files with a newline, then fixing up all the new files?

IIRC you use one of the IntelliJ editors, so something like Settings → Editor → General → Ensure line feed at file end on Save should do the trick.

human_readable_name: "trait no longer object safe",
description: "A trait is no longer object safe",
required_update: Major,
reference_link: Some("https://doc.rust-lang.org/stable/reference/items/traits.html?highlight=object#object-safety"),
Copy link
Owner

Choose a reason for hiding this comment

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

Let's avoid the ?highlight=object bit, we can just reference the heading and let the user read without extraneous visual highlights.

Suggested change
reference_link: Some("https://doc.rust-lang.org/stable/reference/items/traits.html?highlight=object#object-safety"),
reference_link: Some("https://doc.rust-lang.org/stable/reference/items/traits.html#object-safety"),

SemverQuery(
id: "trait_no_longer_object_safe",
human_readable_name: "trait no longer object safe",
description: "A trait is no longer object safe",
Copy link
Owner

Choose a reason for hiding this comment

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

"Object safe" is likely to be an unfamiliar concept to many Rustaceans, so this would be a great spot to explain it a bit more.

Suggested change
description: "A trait is no longer object safe",
description: "A trait is no longer object safe, meaning it can no longer be used as `dyn Trait`",

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep! I plan on going through and cleaning up the messages :)

@obi1kenobi
Copy link
Owner

obi1kenobi commented Feb 12, 2024

This is the key line in that test output:

The query produced a non-empty output when it compared two crates with the same rustdoc.

This means that comparing a crate to itself (i.e. no changes whatsoever) triggered a lint. This is always a false-positive, since there was no source code change at all.

There's a minor logic error bug in your query. Make sure the object-safety property is different between the baseline and current :)

Two ideas for follow-up PRs, if you're open to them:

  • Add a note to the CONTRIBUTING doc that the test cases run all lints on all test crates, so it's perfectly normal to see lint output from other crates if those crates have code that triggers your new lint. Also add a note describing the linting of crates with an identical copy of themselves, to catch false-positives.
  • Update the test output in that test, to do a better job explaining why a lint reporting issues when comparing some rustdoc to itself is a problem. In retrospect, there's no reasonable way you could have understood what that test was trying to do based on that message, so it's a clear and unnecessary onboarding papercut.

@devanbenz
Copy link
Contributor Author

Two ideas for follow-up PRs, if you're open to them:

  • Add a note to the CONTRIBUTING doc that the test cases run all lints on all test crates, so it's perfectly normal to see lint output from other crates if those crates have code that triggers your new lint. Also add a note describing the linting of crates with an identical copy of themselves, to catch false-positives.
  • Update the test output in that test, to do a better job explaining why a lint reporting issues when comparing some rustdoc to itself is a problem. In retrospect, there's no reasonable way you could have understood what that test was trying to do based on that message, so it's a clear and unnecessary onboarding papercut.

Amazing - was able to resolve. Going to take note of those pr ideas you indicated :D

@devanbenz devanbenz marked this pull request as ready for review February 12, 2024 18:21
@devanbenz
Copy link
Contributor Author

Assuming that this is failing due to nightly v stable 🤔

build.rs Outdated Show resolved Hide resolved
@obi1kenobi
Copy link
Owner

obi1kenobi commented Feb 12, 2024

Let's do this:

  • Split the test crate and expected nightly test output out of this PR and into a separate PR. Having seen that it would pass right now, we'll leave that one as a draft and merge it in the future.
  • That will leave the new lint unconditionally included and available on all Rust versions here in this PR. That we can merge today and ship to our users who run nightly or beta Rust right now.

I think this is the least complex solution that gets us most of the benefits without big drawbacks.

Copy link
Owner

@obi1kenobi obi1kenobi left a comment

Choose a reason for hiding this comment

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

Will merge after applying a couple of minor nit fixes + after you've had a chance to open the draft PR with the test crate and test outputs.

src/lints/trait_no_longer_object_safe.ron Outdated Show resolved Hide resolved
src/lints/trait_no_longer_object_safe.ron Outdated Show resolved Hide resolved
@devanbenz
Copy link
Contributor Author

@obi1kenobi #661 opened a draft PR with the tests

@obi1kenobi obi1kenobi merged commit fc486f2 into obi1kenobi:main Feb 13, 2024
35 checks passed
@devanbenz devanbenz deleted the object_safe_trait branch February 13, 2024 01:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants