Skip to content

Commit a310f3c

Browse files
committed
Add annotation setup
1 parent 3945c31 commit a310f3c

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

bytecode/src/bytecode.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ pub enum Instruction {
205205
ReturnValue,
206206
YieldValue,
207207
YieldFrom,
208+
SetupAnnotation,
208209
SetupLoop {
209210
start: Label,
210211
end: Label,
@@ -570,6 +571,7 @@ impl Instruction {
570571
ReturnValue => w!(ReturnValue),
571572
YieldValue => w!(YieldValue),
572573
YieldFrom => w!(YieldFrom),
574+
SetupAnnotation => w!(SetupAnnotation),
573575
SetupLoop { start, end } => w!(SetupLoop, label_map[start], label_map[end]),
574576
SetupExcept { handler } => w!(SetupExcept, label_map[handler]),
575577
SetupFinally { handler } => w!(SetupFinally, label_map[handler]),

compiler/src/compile.rs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -984,15 +984,14 @@ impl<O: OutputStream> Compiler<O> {
984984
Ok(())
985985
}
986986

987-
fn option_stmt_to_bool(&mut self, stmts: &Option<ast::Suite>) -> bool {
988-
match &stmts {
989-
Some(stmts) => self.find_ann(stmts),
990-
None => false,
991-
}
992-
}
993-
994-
fn find_ann(&mut self, body: &[ast::Statement]) -> bool {
987+
fn find_ann(&self, body: &[ast::Statement]) -> bool {
995988
use ast::StatementType::*;
989+
let option_stmt_to_bool = |suit: &Option<ast::Suite>| -> bool {
990+
match suit {
991+
Some(stmts) => self.find_ann(stmts),
992+
None => false,
993+
}
994+
};
996995

997996
for statement in body {
998997
let res = match &statement.node {
@@ -1007,17 +1006,17 @@ impl<O: OutputStream> Compiler<O> {
10071006
iter: _,
10081007
body,
10091008
orelse,
1010-
} => self.find_ann(body) || self.option_stmt_to_bool(orelse),
1009+
} => self.find_ann(body) || option_stmt_to_bool(orelse),
10111010
If {
10121011
test: _,
10131012
body,
10141013
orelse,
1015-
} => self.find_ann(body) || self.option_stmt_to_bool(orelse),
1014+
} => self.find_ann(body) || option_stmt_to_bool(orelse),
10161015
While {
10171016
test: _,
10181017
body,
10191018
orelse,
1020-
} => self.find_ann(body) || self.option_stmt_to_bool(orelse),
1019+
} => self.find_ann(body) || option_stmt_to_bool(orelse),
10211020
With {
10221021
is_async: _,
10231022
items: _,
@@ -1029,9 +1028,9 @@ impl<O: OutputStream> Compiler<O> {
10291028
orelse,
10301029
finalbody,
10311030
} => {
1032-
self.find_ann(body)
1033-
|| self.option_stmt_to_bool(orelse)
1034-
|| self.option_stmt_to_bool(finalbody)
1031+
self.find_ann(&body)
1032+
|| option_stmt_to_bool(orelse)
1033+
|| option_stmt_to_bool(finalbody)
10351034
}
10361035
_ => false,
10371036
};
@@ -1097,15 +1096,7 @@ impl<O: OutputStream> Compiler<O> {
10971096
});
10981097
// setup annotations
10991098
if self.find_ann(body) {
1100-
self.emit(Instruction::BuildMap {
1101-
size: 0,
1102-
unpack: false,
1103-
for_call: false,
1104-
});
1105-
self.emit(Instruction::StoreName {
1106-
name: "__annotations__".to_owned(),
1107-
scope: bytecode::NameScope::Free,
1108-
});
1099+
self.emit(Instruction::SetupAnnotation);
11091100
}
11101101
self.compile_statements(new_body)?;
11111102
self.emit(Instruction::LoadConst {

vm/src/frame.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,13 @@ impl ExecutingFrame<'_> {
456456
Ok(Some(ExecutionResult::Yield(value)))
457457
}
458458
bytecode::Instruction::YieldFrom => self.execute_yield_from(vm),
459+
bytecode::Instruction::SetupAnnotation => {
460+
let locals = self.scope.get_locals();
461+
if let Err(_e) = locals.get_item("__annotations__", vm) {
462+
locals.set_item("__annotations__", vm.ctx.new_dict().into_object(), vm)?;
463+
}
464+
Ok(None)
465+
}
459466
bytecode::Instruction::SetupLoop { start, end } => {
460467
self.push_block(BlockType::Loop {
461468
start: *start,

0 commit comments

Comments
 (0)