(
&mut self,
name: &str,
parameters: &ast::Parameters,
body: &[ast::Stmt],
decorator_list: &[ast::Decorator],
returns: Option<&ast::Expr>, // TODO: use
| 4452 | // = compiler_function |
| 4453 | #[allow(clippy::too_many_arguments)] |
| 4454 | fn compile_function_def( |
| 4455 | &mut self, |
| 4456 | name: &str, |
| 4457 | parameters: &ast::Parameters, |
| 4458 | body: &[ast::Stmt], |
| 4459 | decorator_list: &[ast::Decorator], |
| 4460 | returns: Option<&ast::Expr>, // TODO: use type hint somehow.. |
| 4461 | is_async: bool, |
| 4462 | type_params: Option<&ast::TypeParams>, |
| 4463 | ) -> CompileResult<()> { |
| 4464 | // Save the source range of the `def` line before compiling decorators/defaults, |
| 4465 | // so that the function code object gets the correct co_firstlineno. |
| 4466 | let def_source_range = self.current_source_range; |
| 4467 | |
| 4468 | self.prepare_decorators(decorator_list)?; |
| 4469 | |
| 4470 | // compile defaults and return funcflags |
| 4471 | let funcflags = self.compile_default_arguments(parameters)?; |
| 4472 | |
| 4473 | // Restore the `def` line range so that enter_function → push_output → get_source_line_number() |
| 4474 | // records the `def` keyword's line as co_firstlineno, not the last default-argument line. |
| 4475 | self.set_source_range(def_source_range); |
| 4476 | |
| 4477 | let is_generic = type_params.is_some(); |
| 4478 | let mut num_typeparam_args = 0; |
| 4479 | |
| 4480 | // Save context before entering TypeParams scope |
| 4481 | let saved_ctx = self.ctx; |
| 4482 | |
| 4483 | if is_generic { |
| 4484 | // Count args to pass to type params scope |
| 4485 | if funcflags.contains(&bytecode::MakeFunctionFlag::Defaults) { |
| 4486 | num_typeparam_args += 1; |
| 4487 | } |
| 4488 | if funcflags.contains(&bytecode::MakeFunctionFlag::KwOnlyDefaults) { |
| 4489 | num_typeparam_args += 1; |
| 4490 | } |
| 4491 | |
| 4492 | // Enter type params scope |
| 4493 | let type_params_name = format!("<generic parameters of {name}>"); |
| 4494 | self.push_output( |
| 4495 | bytecode::CodeFlags::OPTIMIZED | bytecode::CodeFlags::NEWLOCALS, |
| 4496 | 0, |
| 4497 | num_typeparam_args as u32, |
| 4498 | 0, |
| 4499 | type_params_name, |
| 4500 | )?; |
| 4501 | |
| 4502 | // TypeParams scope is function-like |
| 4503 | self.ctx = CompileContext { |
| 4504 | loop_data: None, |
| 4505 | in_class: saved_ctx.in_class, |
| 4506 | func: FunctionContext::Function, |
| 4507 | in_async_scope: false, |
| 4508 | }; |
| 4509 | |
| 4510 | // Add parameter names to varnames for the type params scope |
| 4511 | // These will be passed as arguments when the closure is called |
no test coverage detected