-
-
Notifications
You must be signed in to change notification settings - Fork 12
Distinguish unsafe and extern #55
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
Merged
davidlattimore
merged 5 commits into
cackle-rs:main
from
XSpielinbox:distinguish-unsafe
May 18, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
528f9d5
Create separate permission for extern usage
XSpielinbox a77e400
Update cackle.toml according to new permission
XSpielinbox 9fc67ae
Introduce new config file format version
XSpielinbox 2026edb
Revert changes to main cackle.toml
XSpielinbox 6897b9b
Add version 2 permission fallback
XSpielinbox 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,136 @@ | ||
| //! This module tokenises Rust code and looks for the extern blocks. This is done as an additional | ||
| //! layer of defence in addition to use of the -Fmissing-unsafe-on-extern flag when compiling crates, | ||
| //! since that flag only works on code before Rust edition 2024. | ||
|
|
||
| use crate::location::SourceLocation; | ||
| use anyhow::Context; | ||
| use anyhow::Result; | ||
| use ra_ap_rustc_lexer::Token; | ||
| use ra_ap_rustc_lexer::TokenKind; | ||
| use std::path::Path; | ||
|
|
||
| /// Returns the locations of all extern block usages found in `path` | ||
| pub(crate) fn scan_path(path: &Path) -> Result<Vec<SourceLocation>> { | ||
| let bytes = | ||
| std::fs::read(path).with_context(|| format!("Failed to read `{}`", path.display()))?; | ||
| let Ok(source) = std::str::from_utf8(&bytes) else { | ||
| // If the file isn't valid UTF-8 then we don't need to check it for the extern blocks, | ||
| // since it can't be a source file that the rust compiler would accept. | ||
| return Ok(Vec::new()); | ||
| }; | ||
| Ok(scan_string(source, path)) | ||
| } | ||
|
|
||
| fn scan_string(source: &str, path: &Path) -> Vec<SourceLocation> { | ||
| let mut token_start_offset = 0; | ||
| let mut locations = Vec::new(); | ||
|
|
||
| let skip_condition = |x| { | ||
| matches!( | ||
| x, | ||
| TokenKind::BlockComment { | ||
| doc_style: _, | ||
| terminated: _ | ||
| } | TokenKind::LineComment { doc_style: _ } | ||
| | TokenKind::Whitespace | ||
| ) | ||
| }; | ||
|
|
||
| let mut iter = ra_ap_rustc_lexer::tokenize(source, ra_ap_rustc_lexer::FrontmatterAllowed::No); | ||
| let mut previous_token_text = ""; | ||
| let mut previous_token_end_offset = 0; | ||
| let mut previous_token_length = 0; | ||
|
|
||
| while let (Some(token), skipped_offset) = next_token(&mut iter, skip_condition) { | ||
| let token_length = usize::try_from(token.len).unwrap(); | ||
|
|
||
| token_start_offset += skipped_offset; | ||
|
|
||
| let token_end_offset = token_start_offset + token_length; | ||
| let token_text = &source[token_start_offset..token_end_offset]; | ||
| if !previous_token_text.is_empty() { | ||
| // check against previous token | ||
| if check_tokens(previous_token_text, token_text) { | ||
| add_location( | ||
| source, | ||
| path, | ||
| &mut locations, | ||
| previous_token_end_offset, | ||
| previous_token_length, | ||
| ); | ||
| } | ||
| } | ||
| previous_token_text = token_text; | ||
| previous_token_end_offset = token_end_offset; | ||
| previous_token_length = token_length; | ||
| // always check against potential future token | ||
| if let (Some(next_token), skipped_offset) = next_token(&mut iter, skip_condition) { | ||
| // the next token starts after the end of the current token | ||
| token_start_offset += token_length; | ||
| token_start_offset += skipped_offset; | ||
|
|
||
| let next_token_length = usize::try_from(next_token.len).unwrap(); | ||
|
|
||
| let next_token_end_offset = token_start_offset + next_token_length; | ||
| let next_token_text = &source[token_start_offset..next_token_end_offset]; | ||
| if check_tokens(token_text, next_token_text) { | ||
| add_location(source, path, &mut locations, token_end_offset, token_length); | ||
| } | ||
| token_start_offset = next_token_end_offset; | ||
| // as we consumed the next token already, this will be our previous token in the next loop iteration | ||
| previous_token_text = next_token_text; | ||
| previous_token_end_offset = next_token_end_offset; | ||
| previous_token_length = next_token_length; | ||
| } else { | ||
| // current token is last token in file | ||
| // this should never be valid code, but we flag it nevertheless to be safe | ||
| if token_text == "extern" { | ||
| add_location(source, path, &mut locations, token_end_offset, token_length); | ||
| } | ||
| // as there should not be another loop iteration this should be useless | ||
| // we do it just for completeness | ||
| token_start_offset = token_end_offset; | ||
| } | ||
| } | ||
| locations | ||
| } | ||
|
|
||
| fn check_tokens(first_token_text: &str, second_token_text: &str) -> bool { | ||
| first_token_text == "extern" && !matches!(second_token_text, "crate" | "]" | ")") | ||
| } | ||
|
|
||
| /// Returns the next relevant token according to the condition given and the offset to it | ||
| fn next_token<F>( | ||
| iter: &mut impl Iterator<Item = Token>, | ||
| skip_condition: F, | ||
| ) -> (Option<Token>, usize) | ||
| where | ||
| F: Fn(TokenKind) -> bool, | ||
| { | ||
| let mut skipped_offset = 0; | ||
|
|
||
| for next in iter.by_ref() { | ||
| if skip_condition(next.kind) { | ||
| skipped_offset += usize::try_from(next.len).unwrap(); | ||
| } else { | ||
| return (Some(next), skipped_offset); | ||
| } | ||
| } | ||
| (None, skipped_offset) | ||
| } | ||
|
|
||
| fn add_location( | ||
| source: &str, | ||
| path: &Path, | ||
| locations: &mut Vec<SourceLocation>, | ||
| token_end_offset: usize, | ||
| token_length: usize, | ||
| ) { | ||
| let column = source[..token_end_offset] | ||
| .lines() | ||
| .last() | ||
| .map(|line| (line.len() - token_length + 1) as u32) | ||
| .unwrap_or(1); | ||
| let line = 1.max(source[..token_end_offset].lines().count() as u32); | ||
| locations.push(SourceLocation::new(path, line, Some(column))); | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
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.
Are we now reading and tokenising every file twice now? Once for checking for unsafe and once for checking for extern?
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.
Yeah, that is indeed correct and not ideal. I split it to keep it as separate and easy to understand as possible for now. But it could also be merged, once it has been confirmed that this code in general is correct and useful.
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.
Oh, right, sorry. I meant completely revert the changes to
cackle.toml, so the new fields wouldn't be setThere 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.
Ok, I have reverted both now, but that does not help either as then the tests are failing. I don't really understand where you want to go with this.
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.
From the failure, it looks like the tests are failing because they're saying that the "extern" permission is needed. When I was suggesting reverting the changes to cackle.toml, I meant just the
cackle.tomlfor cackle itself, not the one for the test.I suspect we also need to make it so that if a config has version=2, then we copy the value from
allow_unsafetoallow_extern. That way a version 2 config will still work with a newer version of cackle, which I think is important.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.
Ok, I changed it to only revert the main
cackle.toml.I will try to add this fallback for version 2 tomorrow.
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.
Is the change I made now what you had in mind?