Set annotation for function args

This commit is contained in:
Yiqun Ling 2020-07-14 17:58:58 +08:00 committed by GitHub
parent 1125a529e7
commit f6e25fc0cb

View file

@ -931,7 +931,7 @@ impl<O: OutputStream> Compiler<O> {
num_annotations += 1; num_annotations += 1;
} }
for arg in args.args.iter() { let mut visit_arg_annotation = |arg: &ast::Parameter| -> CompileResult<()> {
if let Some(annotation) = &arg.annotation { if let Some(annotation) = &arg.annotation {
self.emit(Instruction::LoadConst { self.emit(Instruction::LoadConst {
value: bytecode::Constant::String { value: bytecode::Constant::String {
@ -941,6 +941,19 @@ impl<O: OutputStream> Compiler<O> {
self.compile_expression(&annotation)?; self.compile_expression(&annotation)?;
num_annotations += 1; num_annotations += 1;
} }
Ok(())
};
for arg in args.args.iter().chain(args.kwonlyargs.iter()) {
visit_arg_annotation(arg)?;
}
if let ast::Varargs::Named(arg) = &args.vararg {
visit_arg_annotation(arg)?;
}
if let ast::Varargs::Named(arg) = &args.kwarg {
visit_arg_annotation(arg)?;
} }
if num_annotations > 0 { if num_annotations > 0 {
@ -1385,6 +1398,11 @@ impl<O: OutputStream> Compiler<O> {
self.compile_store(target)?; self.compile_store(target)?;
} }
// Annotations are only evaluated in a module or class.
if self.ctx.in_func() {
return Ok(());
}
// Compile annotation: // Compile annotation:
self.compile_expression(annotation)?; self.compile_expression(annotation)?;