Skip to content

Extra name mangling #2289

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions src/ir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -813,26 +813,33 @@ If you encounter an error missing from this list, please file an issue or a PR!"
// TODO: Move all this syntax crap to other part of the code.

/// Mangles a name so it doesn't conflict with any keyword.
#[rustfmt::skip]
pub fn rust_mangle<'a>(&self, name: &'a str) -> Cow<'a, str> {
#[rustfmt::skip]
const KEYWORDS: &[&'static str] = &[
"abstract", "alignof", "as", "async", "await", "become", "box",
"break", "const", "continue", "crate", "do", "dyn", "else", "enum",
"extern", "false", "final", "fn", "for", "if", "impl", "in", "let",
"loop", "macro", "match", "mod", "move", "mut", "offsetof",
"override", "priv", "proc", "pub", "pure", "ref", "return", "Self",
"self", "sizeof", "static", "struct", "super", "trait", "true",
"try", "type", "typeof", "unsafe", "unsized", "use", "virtual",
"where", "while", "yield", "str", "bool", "f32", "f64", "usize",
"isize", "u128", "i128", "u64", "i64", "u32", "i32", "u16", "i16",
"u8", "i8", "_",
];
let mut is_keyword_or_mangled_keyword = false;
for keyword in KEYWORDS {
if let Some(remaining) = name.strip_prefix(keyword) {
if remaining.is_empty() || remaining.chars().all(|c| c == '_') {
is_keyword_or_mangled_keyword = true;
break;
}
}
}
if name.contains('@') ||
name.contains('?') ||
name.contains('$') ||
matches!(
name,
"abstract" | "alignof" | "as" | "async" | "await" | "become" |
"box" | "break" | "const" | "continue" | "crate" | "do" |
"dyn" | "else" | "enum" | "extern" | "false" | "final" |
"fn" | "for" | "if" | "impl" | "in" | "let" | "loop" |
"macro" | "match" | "mod" | "move" | "mut" | "offsetof" |
"override" | "priv" | "proc" | "pub" | "pure" | "ref" |
"return" | "Self" | "self" | "sizeof" | "static" |
"struct" | "super" | "trait" | "true" | "try" | "type" | "typeof" |
"unsafe" | "unsized" | "use" | "virtual" | "where" |
"while" | "yield" | "str" | "bool" | "f32" | "f64" |
"usize" | "isize" | "u128" | "i128" | "u64" | "i64" |
"u32" | "i32" | "u16" | "i16" | "u8" | "i8" | "_"
)
is_keyword_or_mangled_keyword
{
let mut s = name.to_owned();
s = s.replace('@', "_");
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/tests/arg_keyword.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions tests/expectations/tests/bitfield-method-same-name.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/expectations/tests/call-conv-typedef.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 6 additions & 8 deletions tests/expectations/tests/derive-bitfield-method-same-name.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/expectations/tests/enum_and_vtable_mangling.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions tests/expectations/tests/issue-1382-rust-primitive-types.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions tests/expectations/tests/keyword_mangling.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading