Skip to content

Commit 8766493

Browse files
committed
apply bitflagset
1 parent 375b547 commit 8766493

File tree

10 files changed

+212
-149
lines changed

10 files changed

+212
-149
lines changed

Cargo.lock

Lines changed: 40 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ phf = { version = "0.13.1", default-features = false, features = ["macros"]}
166166
ahash = "0.8.12"
167167
ascii = "1.1"
168168
bitflags = "2.11.0"
169+
bitflagset = "0.0.2"
169170
bstr = "1"
170171
bytes = "1.11.1"
171172
cfg-if = "1.0"

crates/codegen/src/compile.rs

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2500,7 +2500,7 @@ impl Compiler {
25002500
self.compile_expression(value)?;
25012501
emit!(self, Instruction::ReturnValue);
25022502
let value_code = self.exit_scope();
2503-
self.make_closure(value_code, bytecode::MakeFunctionFlags::empty())?;
2503+
self.make_closure(value_code, bytecode::MakeFunctionFlags::new())?;
25042504
// Stack: [type_params_tuple, value_closure]
25052505

25062506
// Swap so unpack_sequence reverse gives correct order
@@ -2513,7 +2513,7 @@ impl Compiler {
25132513

25142514
let code = self.exit_scope();
25152515
self.ctx = prev_ctx;
2516-
self.make_closure(code, bytecode::MakeFunctionFlags::empty())?;
2516+
self.make_closure(code, bytecode::MakeFunctionFlags::new())?;
25172517
emit!(self, Instruction::PushNull);
25182518
emit!(self, Instruction::Call { argc: 0 });
25192519

@@ -2552,7 +2552,7 @@ impl Compiler {
25522552

25532553
let code = self.exit_scope();
25542554
self.ctx = prev_ctx;
2555-
self.make_closure(code, bytecode::MakeFunctionFlags::empty())?;
2555+
self.make_closure(code, bytecode::MakeFunctionFlags::new())?;
25562556
// Stack: [name, None, closure]
25572557
}
25582558

@@ -2716,7 +2716,7 @@ impl Compiler {
27162716
self.ctx = prev_ctx;
27172717

27182718
// Create closure for lazy evaluation
2719-
self.make_closure(code, bytecode::MakeFunctionFlags::empty())?;
2719+
self.make_closure(code, bytecode::MakeFunctionFlags::new())?;
27202720

27212721
Ok(())
27222722
}
@@ -3640,7 +3640,7 @@ impl Compiler {
36403640
&mut self,
36413641
parameters: &ast::Parameters,
36423642
) -> CompileResult<bytecode::MakeFunctionFlags> {
3643-
let mut funcflags = bytecode::MakeFunctionFlags::empty();
3643+
let mut funcflags = bytecode::MakeFunctionFlags::new();
36443644

36453645
// Handle positional defaults
36463646
let defaults: Vec<_> = core::iter::empty()
@@ -3660,7 +3660,7 @@ impl Compiler {
36603660
count: defaults.len().to_u32()
36613661
}
36623662
);
3663-
funcflags |= bytecode::MakeFunctionFlags::DEFAULTS;
3663+
funcflags.insert(bytecode::MakeFunctionFlag::Defaults);
36643664
}
36653665

36663666
// Handle keyword-only defaults
@@ -3685,7 +3685,7 @@ impl Compiler {
36853685
count: kw_with_defaults.len().to_u32(),
36863686
}
36873687
);
3688-
funcflags |= bytecode::MakeFunctionFlags::KW_ONLY_DEFAULTS;
3688+
funcflags.insert(bytecode::MakeFunctionFlag::KwOnlyDefaults);
36893689
}
36903690

36913691
Ok(funcflags)
@@ -3835,7 +3835,7 @@ impl Compiler {
38353835
let annotate_code = self.exit_annotation_scope(saved_ctx);
38363836

38373837
// Make a closure from the code object
3838-
self.make_closure(annotate_code, bytecode::MakeFunctionFlags::empty())?;
3838+
self.make_closure(annotate_code, bytecode::MakeFunctionFlags::new())?;
38393839

38403840
Ok(true)
38413841
}
@@ -4045,7 +4045,7 @@ impl Compiler {
40454045
);
40464046

40474047
// Make a closure from the code object
4048-
self.make_closure(annotate_code, bytecode::MakeFunctionFlags::empty())?;
4048+
self.make_closure(annotate_code, bytecode::MakeFunctionFlags::new())?;
40494049

40504050
// Store as __annotate_func__ for classes, __annotate__ for modules
40514051
let name = if parent_scope_type == CompilerScope::Class {
@@ -4083,10 +4083,10 @@ impl Compiler {
40834083

40844084
if is_generic {
40854085
// Count args to pass to type params scope
4086-
if funcflags.contains(bytecode::MakeFunctionFlags::DEFAULTS) {
4086+
if funcflags.contains(&bytecode::MakeFunctionFlag::Defaults) {
40874087
num_typeparam_args += 1;
40884088
}
4089-
if funcflags.contains(bytecode::MakeFunctionFlags::KW_ONLY_DEFAULTS) {
4089+
if funcflags.contains(&bytecode::MakeFunctionFlag::KwOnlyDefaults) {
40904090
num_typeparam_args += 1;
40914091
}
40924092

@@ -4111,13 +4111,13 @@ impl Compiler {
41114111
// Add parameter names to varnames for the type params scope
41124112
// These will be passed as arguments when the closure is called
41134113
let current_info = self.current_code_info();
4114-
if funcflags.contains(bytecode::MakeFunctionFlags::DEFAULTS) {
4114+
if funcflags.contains(&bytecode::MakeFunctionFlag::Defaults) {
41154115
current_info
41164116
.metadata
41174117
.varnames
41184118
.insert(".defaults".to_owned());
41194119
}
4120-
if funcflags.contains(bytecode::MakeFunctionFlags::KW_ONLY_DEFAULTS) {
4120+
if funcflags.contains(&bytecode::MakeFunctionFlag::KwOnlyDefaults) {
41214121
current_info
41224122
.metadata
41234123
.varnames
@@ -4134,11 +4134,10 @@ impl Compiler {
41344134
}
41354135

41364136
// Compile annotations as closure (PEP 649)
4137-
let annotations_flag = if self.compile_annotations_closure(name, parameters, returns)? {
4138-
bytecode::MakeFunctionFlags::ANNOTATE
4139-
} else {
4140-
bytecode::MakeFunctionFlags::empty()
4141-
};
4137+
let mut annotations_flag = bytecode::MakeFunctionFlags::new();
4138+
if self.compile_annotations_closure(name, parameters, returns)? {
4139+
annotations_flag.insert(bytecode::MakeFunctionFlag::Annotate);
4140+
}
41424141

41434142
// Compile function body
41444143
let final_funcflags = funcflags | annotations_flag;
@@ -4169,7 +4168,7 @@ impl Compiler {
41694168
self.ctx = saved_ctx;
41704169

41714170
// Make closure for type params code
4172-
self.make_closure(type_params_code, bytecode::MakeFunctionFlags::empty())?;
4171+
self.make_closure(type_params_code, bytecode::MakeFunctionFlags::new())?;
41734172

41744173
// Call the type params closure with defaults/kwdefaults as arguments.
41754174
// Call protocol: [callable, self_or_null, arg1, ..., argN]
@@ -4337,57 +4336,57 @@ impl Compiler {
43374336
emit!(
43384337
self,
43394338
Instruction::SetFunctionAttribute {
4340-
flag: bytecode::MakeFunctionFlags::CLOSURE
4339+
flag: bytecode::MakeFunctionFlag::Closure
43414340
}
43424341
);
43434342
}
43444343

43454344
// Set annotations if present
4346-
if flags.contains(bytecode::MakeFunctionFlags::ANNOTATIONS) {
4345+
if flags.contains(&bytecode::MakeFunctionFlag::Annotations) {
43474346
emit!(
43484347
self,
43494348
Instruction::SetFunctionAttribute {
4350-
flag: bytecode::MakeFunctionFlags::ANNOTATIONS
4349+
flag: bytecode::MakeFunctionFlag::Annotations
43514350
}
43524351
);
43534352
}
43544353

43554354
// Set __annotate__ closure if present (PEP 649)
4356-
if flags.contains(bytecode::MakeFunctionFlags::ANNOTATE) {
4355+
if flags.contains(&bytecode::MakeFunctionFlag::Annotate) {
43574356
emit!(
43584357
self,
43594358
Instruction::SetFunctionAttribute {
4360-
flag: bytecode::MakeFunctionFlags::ANNOTATE
4359+
flag: bytecode::MakeFunctionFlag::Annotate
43614360
}
43624361
);
43634362
}
43644363

43654364
// Set kwdefaults if present
4366-
if flags.contains(bytecode::MakeFunctionFlags::KW_ONLY_DEFAULTS) {
4365+
if flags.contains(&bytecode::MakeFunctionFlag::KwOnlyDefaults) {
43674366
emit!(
43684367
self,
43694368
Instruction::SetFunctionAttribute {
4370-
flag: bytecode::MakeFunctionFlags::KW_ONLY_DEFAULTS
4369+
flag: bytecode::MakeFunctionFlag::KwOnlyDefaults
43714370
}
43724371
);
43734372
}
43744373

43754374
// Set defaults if present
4376-
if flags.contains(bytecode::MakeFunctionFlags::DEFAULTS) {
4375+
if flags.contains(&bytecode::MakeFunctionFlag::Defaults) {
43774376
emit!(
43784377
self,
43794378
Instruction::SetFunctionAttribute {
4380-
flag: bytecode::MakeFunctionFlags::DEFAULTS
4379+
flag: bytecode::MakeFunctionFlag::Defaults
43814380
}
43824381
);
43834382
}
43844383

43854384
// Set type_params if present
4386-
if flags.contains(bytecode::MakeFunctionFlags::TYPE_PARAMS) {
4385+
if flags.contains(&bytecode::MakeFunctionFlag::TypeParams) {
43874386
emit!(
43884387
self,
43894388
Instruction::SetFunctionAttribute {
4390-
flag: bytecode::MakeFunctionFlags::TYPE_PARAMS
4389+
flag: bytecode::MakeFunctionFlag::TypeParams
43914390
}
43924391
);
43934392
}
@@ -4679,14 +4678,14 @@ impl Compiler {
46794678
emit!(self, Instruction::PushNull);
46804679

46814680
// Set up the class function with type params
4682-
let mut func_flags = bytecode::MakeFunctionFlags::empty();
4681+
let mut func_flags = bytecode::MakeFunctionFlags::new();
46834682
emit!(
46844683
self,
46854684
Instruction::LoadName {
46864685
namei: dot_type_params
46874686
}
46884687
);
4689-
func_flags |= bytecode::MakeFunctionFlags::TYPE_PARAMS;
4688+
func_flags.insert(bytecode::MakeFunctionFlag::TypeParams);
46904689

46914690
// Create class function with closure
46924691
self.make_closure(class_code, func_flags)?;
@@ -4809,7 +4808,7 @@ impl Compiler {
48094808
self.ctx = saved_ctx;
48104809

48114810
// Execute the type params function
4812-
self.make_closure(type_params_code, bytecode::MakeFunctionFlags::empty())?;
4811+
self.make_closure(type_params_code, bytecode::MakeFunctionFlags::new())?;
48134812
emit!(self, Instruction::PushNull);
48144813
emit!(self, Instruction::Call { argc: 0 });
48154814
} else {
@@ -4818,7 +4817,7 @@ impl Compiler {
48184817
emit!(self, Instruction::PushNull);
48194818

48204819
// Create class function with closure
4821-
self.make_closure(class_code, bytecode::MakeFunctionFlags::empty())?;
4820+
self.make_closure(class_code, bytecode::MakeFunctionFlags::new())?;
48224821
self.emit_load_const(ConstantData::Str { value: name.into() });
48234822

48244823
if let Some(arguments) = arguments {
@@ -7086,12 +7085,12 @@ impl Compiler {
70867085
}
70877086

70887087
self.enter_function(&name, params)?;
7089-
let mut func_flags = bytecode::MakeFunctionFlags::empty();
7088+
let mut func_flags = bytecode::MakeFunctionFlags::new();
70907089
if have_defaults {
7091-
func_flags |= bytecode::MakeFunctionFlags::DEFAULTS;
7090+
func_flags.insert(bytecode::MakeFunctionFlag::Defaults);
70927091
}
70937092
if have_kwdefaults {
7094-
func_flags |= bytecode::MakeFunctionFlags::KW_ONLY_DEFAULTS;
7093+
func_flags.insert(bytecode::MakeFunctionFlag::KwOnlyDefaults);
70957094
}
70967095

70977096
// Set qualname for lambda
@@ -7775,7 +7774,7 @@ impl Compiler {
77757774
self.ctx = prev_ctx;
77767775

77777776
// Create comprehension function with closure
7778-
self.make_closure(code, bytecode::MakeFunctionFlags::empty())?;
7777+
self.make_closure(code, bytecode::MakeFunctionFlags::new())?;
77797778
emit!(self, Instruction::PushNull);
77807779

77817780
// Evaluate iterated item:

crates/compiler-core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ ruff_source_file = { workspace = true }
1414
rustpython-wtf8 = { workspace = true }
1515

1616
bitflags = { workspace = true }
17+
bitflagset = { workspace = true }
1718
itertools = { workspace = true }
1819
malachite-bigint = { workspace = true }
1920
num-complex = { workspace = true }

crates/compiler-core/src/bytecode.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ pub use crate::bytecode::{
2626
oparg::{
2727
BinaryOperator, BuildSliceArgCount, CommonConstant, ComparisonOperator, ConvertValueOparg,
2828
IntrinsicFunction1, IntrinsicFunction2, Invert, Label, LoadAttr, LoadSuperAttr,
29-
MakeFunctionFlags, NameIdx, OpArg, OpArgByte, OpArgState, OpArgType, RaiseKind, ResumeType,
30-
SpecialMethod, UnpackExArgs,
29+
MakeFunctionFlag, MakeFunctionFlags, NameIdx, OpArg, OpArgByte, OpArgState, OpArgType,
30+
RaiseKind, ResumeType, SpecialMethod, UnpackExArgs,
3131
},
3232
};
3333

crates/compiler-core/src/bytecode/instruction.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
oparg::{
77
BinaryOperator, BuildSliceArgCount, CommonConstant, ComparisonOperator,
88
ConvertValueOparg, IntrinsicFunction1, IntrinsicFunction2, Invert, Label, LoadAttr,
9-
LoadSuperAttr, MakeFunctionFlags, NameIdx, OpArg, OpArgByte, OpArgType, RaiseKind,
9+
LoadSuperAttr, MakeFunctionFlag, NameIdx, OpArg, OpArgByte, OpArgType, RaiseKind,
1010
SpecialMethod, StoreFastLoadFast, UnpackExArgs,
1111
},
1212
},
@@ -264,7 +264,7 @@ pub enum Instruction {
264264
i: Arg<u32>,
265265
} = 107,
266266
SetFunctionAttribute {
267-
flag: Arg<MakeFunctionFlags>,
267+
flag: Arg<MakeFunctionFlag>,
268268
} = 108,
269269
SetUpdate {
270270
i: Arg<u32>,

0 commit comments

Comments
 (0)