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

Use raw identifier for reserved keywords #295

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

davidMcneil
Copy link

@davidMcneil davidMcneil commented Sep 14, 2020

Resolves #279

Unfortunately, we cannot use a raw identifier for all keywords see the comment here.

Signed-off-by: David McNeil [email protected]

Comment on lines +272 to +280
fn strip_raw_ident(ident: &str) -> &str {
const RAW_IDENT_PREFIX: &str = "r#";

if ident.starts_with(RAW_IDENT_PREFIX) {
&ident[RAW_IDENT_PREFIX.len()..]
} else {
ident
}
}
Copy link
Author

@davidMcneil davidMcneil Sep 14, 2020

Choose a reason for hiding this comment

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

I did not see a convenient way to share this logic between c2rust-bitfields-derive and c2rust-transpiler because they did not share a common crate. Is it worth removing the duplication?

Copy link
Contributor

Choose a reason for hiding this comment

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

It's a small function, probably not worth de-duplicating.

Comment on lines +182 to +188
fn raw_identifier_if_reserved_name(basename: &str) -> Option<String> {
if RESERVED_NAMES.contains(&basename) && !RESERVED_PATH_SEGMENT_NAMES.contains(&basename) {
Some(format!("r#{}", basename))
} else {
None
}
}
Copy link
Author

@davidMcneil davidMcneil Sep 14, 2020

Choose a reason for hiding this comment

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

It is a bummer that RESERVED_NAMES and RESERVED_PATH_SEGMENT_NAMES leak into Renamer. I could make the Renamer take a function allowing the detection of reserved names to be configurable. Would this be preferred?

Copy link
Contributor

Choose a reason for hiding this comment

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

Renamer already takes a reference to RESERVED_NAMES as the argument to its new constructor, so you could take that reference and stick it inside Renamer but I'm not sure that's worth it.

Copy link
Author

Choose a reason for hiding this comment

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

Agreed, that was my initial approach (ie treat reserved_names as names that should be converted to a raw identifier). However, when I had to add RESERVED_PATH_SEGMENT_NAMES there did not seem to be value in keeping the reserved list internally.

@thedataking
Copy link
Contributor

@davidMcneil I've fixed the problem that caused Azure pipelines to fail. Can you rebase your PR on top of master?

@davidMcneil
Copy link
Author

@thedataking I rebased, but it is still failing. It looks like there is a problem reading this token?

@thedataking
Copy link
Contributor

Correct, David that the github actions fails because of the secret token. I turned off actions on forked PRs for that reason so we can ignore that failure. If Azure Pipelines testing succeeds and Andrei is happy, we can can merge this PR.

@XVilka
Copy link

XVilka commented Oct 13, 2022

Should be this PR rebased and updated? @davidMcneil

@VorpalBlade
Copy link

If I'm reading the code correctly (which I might not be, it is late in the evening) this will not handle the case I ran into where a C file had a name starting with a number:

error: expected identifier, found `3DSP`
  --> c2rust-lib.rs:40:9
   |
40 | pub mod 3DSP;
   |         ^^^^ expected identifier

I'm not sure if that would be a separate PR or should be handled here.

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.

Translate Rust keywords into raw identifiers
5 participants