Skip to content

Commit

Permalink
Merge pull request #822 from ambeeeeee/ported-escaping
Browse files Browse the repository at this point in the history
Implement the `safe_ident` strategy for virtual call parameter identifier generation
  • Loading branch information
Bromeon authored Jul 29, 2024
2 parents aa7107a + b842921 commit 71b869f
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 2 deletions.
1 change: 1 addition & 0 deletions godot-codegen/src/special_cases/codegen_special_cases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ const SELECTED_CLASSES: &[&str] = &[
"CollisionShape2D",
"Control",
"EditorPlugin",
"EditorExportPlugin",
"Engine",
"FileAccess",
"GDScript",
Expand Down
1 change: 1 addition & 0 deletions godot-codegen/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ pub fn cstr_u8_slice(string: &str) -> Literal {
Literal::byte_string(format!("{string}\0").as_bytes())
}

// This function is duplicated in godot-macros\src\util\mod.rs
#[rustfmt::skip]
pub fn safe_ident(s: &str) -> Ident {
// See also: https://doc.rust-lang.org/reference/keywords.html
Expand Down
4 changes: 2 additions & 2 deletions godot-macros/src/class/data_models/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

use crate::util::{bail_fn, ident};
use crate::util::{bail_fn, ident, safe_ident};
use crate::{util, ParseResult};
use proc_macro2::{Group, Ident, TokenStream, TokenTree};
use quote::{format_ident, quote};
Expand Down Expand Up @@ -368,7 +368,7 @@ pub(crate) fn maybe_rename_parameter(param_ident: Ident, next_unnamed_index: &mu
// This could technically collide with another parameter of the same name (without "_"), but that's very unlikely and not
// something we really need to support.
// Note that the case of a single "_" is handled above.
ident(remain)
safe_ident(remain)
} else {
param_ident
}
Expand Down
24 changes: 24 additions & 0 deletions godot-macros/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,27 @@ pub fn make_virtual_tool_check() -> TokenStream {
pub fn make_virtual_tool_check() -> TokenStream {
TokenStream::new()
}

// This function is duplicated in godot-codegen\src\util.rs
#[rustfmt::skip]
pub fn safe_ident(s: &str) -> Ident {
// See also: https://doc.rust-lang.org/reference/keywords.html
match s {
// Lexer
| "as" | "break" | "const" | "continue" | "crate" | "else" | "enum" | "extern" | "false" | "fn" | "for" | "if"
| "impl" | "in" | "let" | "loop" | "match" | "mod" | "move" | "mut" | "pub" | "ref" | "return" | "self" | "Self"
| "static" | "struct" | "super" | "trait" | "true" | "type" | "unsafe" | "use" | "where" | "while"

// Lexer 2018+
| "async" | "await" | "dyn"

// Reserved
| "abstract" | "become" | "box" | "do" | "final" | "macro" | "override" | "priv" | "typeof" | "unsized" | "virtual" | "yield"

// Reserved 2018+
| "try"
=> format_ident!("{}_", s),

_ => ident(s)
}
}
21 changes: 21 additions & 0 deletions itest/rust/src/register_tests/keyword_parameters_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) godot-rust; Bromeon and contributors.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

use godot::builtin::{GString, PackedStringArray};
use godot::classes::IEditorExportPlugin;
use godot::register::{godot_api, GodotClass};

#[derive(GodotClass)]
#[class(base=EditorExportPlugin, init)]
struct KeywordParameterEditorExportPlugin;

#[godot_api]
impl IEditorExportPlugin for KeywordParameterEditorExportPlugin {
// This test requires that the second non-self parameter on `export_file`
// remain named `_type`.
fn export_file(&mut self, _path: GString, _type: GString, _features: PackedStringArray) {}
}
1 change: 1 addition & 0 deletions itest/rust/src/register_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod conversion_test;
mod derive_variant_test;
mod func_test;
mod gdscript_ffi_test;
mod keyword_parameters_test;
mod option_ffi_test;
mod var_test;

Expand Down

0 comments on commit 71b869f

Please sign in to comment.