Skip to content

Commit 87cbe77

Browse files
committed
Implement the same safe_ident strategy that codegen uses for parameter names that might collide with keywords
Document duplicated definition of `safe_ident` Add test for `safe_ident` parameter identifier generation for `#[godot-api]` virtual call generation
1 parent bf67856 commit 87cbe77

File tree

6 files changed

+57
-2
lines changed

6 files changed

+57
-2
lines changed

godot-codegen/src/special_cases/codegen_special_cases.rs

+1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ const SELECTED_CLASSES: &[&str] = &[
137137
"CollisionShape2D",
138138
"Control",
139139
"EditorPlugin",
140+
"EditorExportPlugin",
140141
"Engine",
141142
"FileAccess",
142143
"GDScript",

godot-codegen/src/util.rs

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ pub fn cstr_u8_slice(string: &str) -> Literal {
8383
Literal::byte_string(format!("{string}\0").as_bytes())
8484
}
8585

86+
// This function is duplicated in godot-macros\src\util\mod.rs
8687
#[rustfmt::skip]
8788
pub fn safe_ident(s: &str) -> Ident {
8889
// See also: https://doc.rust-lang.org/reference/keywords.html

godot-macros/src/class/data_models/func.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
66
*/
77

8-
use crate::util::{bail_fn, ident};
8+
use crate::util::{bail_fn, ident, safe_ident};
99
use crate::{util, ParseResult};
1010
use proc_macro2::{Group, Ident, TokenStream, TokenTree};
1111
use quote::{format_ident, quote};
@@ -368,7 +368,7 @@ pub(crate) fn maybe_rename_parameter(param_ident: Ident, next_unnamed_index: &mu
368368
// This could technically collide with another parameter of the same name (without "_"), but that's very unlikely and not
369369
// something we really need to support.
370370
// Note that the case of a single "_" is handled above.
371-
ident(remain)
371+
safe_ident(remain)
372372
} else {
373373
param_ident
374374
}

godot-macros/src/util/mod.rs

+24
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,27 @@ pub fn make_virtual_tool_check() -> TokenStream {
273273
pub fn make_virtual_tool_check() -> TokenStream {
274274
TokenStream::new()
275275
}
276+
277+
// This function is duplicated in godot-codegen\src\util.rs
278+
#[rustfmt::skip]
279+
pub fn safe_ident(s: &str) -> Ident {
280+
// See also: https://doc.rust-lang.org/reference/keywords.html
281+
match s {
282+
// Lexer
283+
| "as" | "break" | "const" | "continue" | "crate" | "else" | "enum" | "extern" | "false" | "fn" | "for" | "if"
284+
| "impl" | "in" | "let" | "loop" | "match" | "mod" | "move" | "mut" | "pub" | "ref" | "return" | "self" | "Self"
285+
| "static" | "struct" | "super" | "trait" | "true" | "type" | "unsafe" | "use" | "where" | "while"
286+
287+
// Lexer 2018+
288+
| "async" | "await" | "dyn"
289+
290+
// Reserved
291+
| "abstract" | "become" | "box" | "do" | "final" | "macro" | "override" | "priv" | "typeof" | "unsized" | "virtual" | "yield"
292+
293+
// Reserved 2018+
294+
| "try"
295+
=> format_ident!("{}_", s),
296+
297+
_ => ident(s)
298+
}
299+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) godot-rust; Bromeon and contributors.
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
6+
*/
7+
8+
// Needed for Clippy to accept #[cfg(all())]
9+
#![allow(clippy::non_minimal_cfg)]
10+
11+
use godot::classes::{EditorExportPlugin, IEditorExportPlugin};
12+
use godot::prelude::*;
13+
14+
#[derive(GodotClass)]
15+
#[class(base=EditorExportPlugin, init)]
16+
struct KeywordParameterEditorExportPlugin {
17+
_base: Base<EditorExportPlugin>,
18+
}
19+
20+
#[godot_api]
21+
impl IEditorExportPlugin for KeywordParameterEditorExportPlugin {
22+
// This test requires that the second non-self parameter on `export_file`
23+
// remain named `_type`. Additionally tell rustfmt to skip this code and
24+
// allow all clippy lints.
25+
#[allow(clippy::all)]
26+
#[rustfmt::skip]
27+
fn export_file(&mut self, _path: GString, _type: GString, _features: PackedStringArray) { }
28+
}

itest/rust/src/register_tests/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ mod conversion_test;
1010
mod derive_variant_test;
1111
mod func_test;
1212
mod gdscript_ffi_test;
13+
mod keyword_parameters_test;
1314
mod option_ffi_test;
1415
mod var_test;
1516

0 commit comments

Comments
 (0)