Skip to content

Commit d23ca7e

Browse files
codegen: extract validate_type_params_no_debug helper
The identical `__debug__` type-parameter rejection in compile_function_def and compile_class_def is now a single shared helper, eliminating the duplicated block (addresses a CodeRabbit review note). Behavior-preserving: `def f[__debug__]`/`class C[__debug__]` still raise "cannot assign to __debug__" and valid type params still compile. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
1 parent 89ef4c8 commit d23ca7e

1 file changed

Lines changed: 23 additions & 24 deletions

File tree

‎crates/codegen/src/compile.rs‎

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4698,6 +4698,27 @@ impl Compiler {
46984698
Ok(true)
46994699
}
47004700

4701+
/// Reject `__debug__` used as a type-parameter name, e.g.
4702+
/// `def f[__debug__](): ...` or `class C[__debug__]: ...`.
4703+
fn validate_type_params_no_debug(
4704+
&mut self,
4705+
type_params: Option<&ast::TypeParams>,
4706+
) -> CompileResult<()> {
4707+
if let Some(params) = type_params {
4708+
for tp in &params.type_params {
4709+
let tp_name = match tp {
4710+
ast::TypeParam::TypeVar(t) => &t.name,
4711+
ast::TypeParam::TypeVarTuple(t) => &t.name,
4712+
ast::TypeParam::ParamSpec(t) => &t.name,
4713+
};
4714+
if tp_name.as_str() == "__debug__" {
4715+
return Err(self.error(CodegenErrorType::Assign("__debug__")));
4716+
}
4717+
}
4718+
}
4719+
Ok(())
4720+
}
4721+
47014722
// = compiler_function
47024723
#[expect(clippy::too_many_arguments, reason = "ignore warning for now")]
47034724
fn compile_function_def(
@@ -4715,18 +4736,7 @@ impl Compiler {
47154736
return Err(self.error(CodegenErrorType::Assign("__debug__")));
47164737
}
47174738
// Reject `def f[__debug__](): ...` type parameter (mirrors class defs).
4718-
if let Some(params) = type_params {
4719-
for tp in &params.type_params {
4720-
let tp_name = match tp {
4721-
ast::TypeParam::TypeVar(t) => &t.name,
4722-
ast::TypeParam::TypeVarTuple(t) => &t.name,
4723-
ast::TypeParam::ParamSpec(t) => &t.name,
4724-
};
4725-
if tp_name.as_str() == "__debug__" {
4726-
return Err(self.error(CodegenErrorType::Assign("__debug__")));
4727-
}
4728-
}
4729-
}
4739+
self.validate_type_params_no_debug(type_params)?;
47304740
// CPython's FunctionDef/AsyncFunctionDef LOC(s) starts at the
47314741
// definition line even when decorators are present.
47324742
let stmt_source_range = self.current_source_range;
@@ -5304,18 +5314,7 @@ impl Compiler {
53045314
return Err(self.error(CodegenErrorType::Assign("__debug__")));
53055315
}
53065316
// Reject `class C[__debug__]: ...` type parameter.
5307-
if let Some(params) = type_params {
5308-
for tp in &params.type_params {
5309-
let tp_name = match tp {
5310-
ast::TypeParam::TypeVar(t) => &t.name,
5311-
ast::TypeParam::TypeVarTuple(t) => &t.name,
5312-
ast::TypeParam::ParamSpec(t) => &t.name,
5313-
};
5314-
if tp_name.as_str() == "__debug__" {
5315-
return Err(self.error(CodegenErrorType::Assign("__debug__")));
5316-
}
5317-
}
5318-
}
5317+
self.validate_type_params_no_debug(type_params)?;
53195318
// CPython's ClassDef LOC(s) starts at the class line even when
53205319
// decorators are present.
53215320
let stmt_source_range = self.current_source_range;

0 commit comments

Comments
 (0)