-
Notifications
You must be signed in to change notification settings - Fork 12
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
201/pass cfg flags #214
Merged
Merged
201/pass cfg flags #214
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6de93a8
Marker, Adapter: Change the MARKER_LINT_CRATES env variable to pass a…
NathanFrasier a74ab7c
rustc: Pass crate names in a `marker` cfg flag
NathanFrasier 702d58a
CI: add cfg flag test
NathanFrasier 1fb3b1e
Doc: add documentation covering usage of the new --cfg flags
NathanFrasier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Setting Lint Levels | ||
Once your crate is configured to run some lints, it quickly becomes important to control the lint levels within your | ||
code. Marker provides the ability to use normal lint control attributes like `#[allow(...)]` `#[deny(...)]` and others | ||
to control the behavior of marker lints. | ||
|
||
## On nightly | ||
Marker uses the `marker::` tool prefix for lints. In order for rustc to recognize this prefix, it requires marker to be | ||
registered via the [`#[register_tool()]` feature](https://github.com/rust-lang/rust/issues/66079). | ||
|
||
If your crate is compiled using nightly, then controlling lints is as simple as placing `#![feature(register_tool)]` | ||
and `#![register_tool(marker)]` at the top of your crate `lib.rs` or `mod.rs` file. You can then use all of the normal | ||
lint attributes you might usually use, on marker provided lints, like `#[allow(marker::my_lint)]`. | ||
|
||
|
||
#### Nightly Required Attributes in `lib.rs` / `main.rs` | ||
```rust | ||
#![feature(register_tool)] | ||
#![register_tool(marker)] | ||
``` | ||
|
||
#### Nightly Lint Attribute | ||
``` | ||
// Marker lints can be controlled like this | ||
#[allow(marker::my_lint)] | ||
fn foo() {} | ||
``` | ||
|
||
## On Stable | ||
If your project isn't on nightly, you can still use the `register_tool` attribute, you'll just need to add some extra | ||
guards around it. Marker provides config arguments to rust during the lint passes, so that your linted code can | ||
conditionally apply attributes only during the marker run. | ||
|
||
Specifically marker passes `--cfg=marker` and a `--cfg=marker="my_crate"` flag for each lint crate. This means that you | ||
can use `#[cfg_attr(marker, foo)]` to conditionally apply the `foo` attribute only during lint runs. | ||
|
||
This conditional compilation can be used to leverage the fact that marker uses nightly for linting, without requiring | ||
the project to use a nightly toolchain. To do this, add `#![cfg_attr(marker, feature(register_tool))]` and | ||
`#![cfg_attr(marker, register_tool(marker))]` attributes to the top of your crate file, to register marker as a tool. | ||
Then you can apply lint level attributes like `#[cfg_attr(marker, allow(marker::foo))]` to control your marker lints. | ||
|
||
Additionally, you can check if a specific lint crate is in the set of loaded lint crates. This is useful, when you | ||
only want to enable some attributes if specific lints are loaded. For this you can use a `marker = "<lint-crate-name>"` | ||
check, like this: `#[cfg_attr(marker = "my_crate", allow(marker::foo))]`. | ||
|
||
#### Stable Required Attributes in `lib.rs` / `main.rs` | ||
```rust | ||
#![cfg_attr(marker, feature(register_tool))] | ||
#![cfg_attr(marker, register_tool(marker))] | ||
``` | ||
|
||
#### Stable Conditional Lint Attribute | ||
``` | ||
// Marker lints can be controlled like this | ||
#[cfg_attr(marker, allow(marker::my_lint))] | ||
fn foo() {} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#![cfg_attr(marker, feature(register_tool))] | ||
#![cfg_attr(marker, register_tool(marker))] | ||
|
||
mod allow_with_simple_attr { | ||
#[cfg_attr(marker, allow(marker::item_with_test_name))] | ||
fn find_me_fn() {} | ||
xFrednet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
mod allow_with_crate_check_attr { | ||
#[cfg_attr(marker = "marker_uilints", allow(marker::item_with_test_name))] | ||
fn find_me_fn() {} | ||
} | ||
|
||
mod lint_with_unloaded_crate_attr { | ||
#[cfg_attr(marker = "some_unknown_crate_that_isnt_loaded", allow(marker::item_with_test_name))] | ||
fn find_me_fn() {} | ||
} | ||
|
||
mod unknown_lint_allow { | ||
#[cfg_attr(marker, allow(marker::some_unknown_lint_that_doesnt_exist))] | ||
fn foo() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
warning: unknown lint: `marker::some_unknown_lint_that_doesnt_exist` | ||
--> $DIR/cfg_attr_check.rs:20:30 | ||
| | ||
20 | #[cfg_attr(marker, allow(marker::some_unknown_lint_that_doesnt_exist))] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `#[warn(unknown_lints)]` on by default | ||
|
||
warning: found a `fn` item with a test name | ||
--> $DIR/cfg_attr_check.rs:16:5 | ||
| | ||
16 | fn find_me_fn() {} | ||
| ^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `#[warn(marker::item_with_test_name)]` on by default | ||
|
||
warning: 2 warnings emitted | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you noted above, it feels inefficient to scan the directory after every compilation.
cargo metadata
sadly doesn't provide any information how the build file will be called.I see a few solutions:
Leaving it like this, it seems to work
Your suggestion of changing the target directory per crate
Guess the output file name and error if the name can't be found. The dynamic library names are constructed by adding a
lib
prefix on unix systems and the OS-specific file ending. Here is a draft of the code:Guess the file name with more confidence. Option 3 would fail, if the user specified a
name
field in the[lib]
section, as that changes the name. This is where we can do some more hackery:Cargo's build command supports the
--config
KEY=VALUE argument, to override parts of the manifest (At least from my understanding). We should be able to use this during lint compilation to prevent any weird renames, by setting the name ourself:--config "lib.name = '<lint_crate_name>'"
. Then we can just apply option 3 to confidently guess that the file actually exists and uses the name we expect.Would you mind looking at option 4? It's totally fine if not, this is already a really nice addition to Marker. Option 2, 3 and 4 can also be added later :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can absolutely look into number 4, But I'd like to focus on getting this in and the user documentation ready first. I'm inclined to handle making this path more efficient as a separate issue, but If you think it should be part of this, I can work on it here.
I was trying to think if we'd get any real sandboxing type benefits from option 2, but I don't really see a point in making sure that some lints can't use build artifacts of others. it would just be more file system usage with little benefit. Besides, if there's one thing I learned writing C#, it's that windows doesn't like deeply nested files, so don't make folders unless you need them :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For me it's fine if we continue this implementation and do the path cleanup in a later PR. This system should have the same robustness level :)