Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .cspell.dict/rust-more.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ biguint
bindgen
bitand
bitflags
bitflagset
bitor
bitvec
bitxor
Expand Down
33 changes: 33 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ phf = { version = "0.13.1", default-features = false, features = ["macros"]}
ahash = "0.8.12"
ascii = "1.1"
bitflags = "2.11.0"
bitflagset = "0.0.3"
bstr = "1"
bytes = "1.11.1"
cfg-if = "1.0"
Expand Down
75 changes: 37 additions & 38 deletions crates/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2509,7 +2509,7 @@ impl Compiler {
self.compile_expression(value)?;
emit!(self, Instruction::ReturnValue);
let value_code = self.exit_scope();
self.make_closure(value_code, bytecode::MakeFunctionFlags::empty())?;
self.make_closure(value_code, bytecode::MakeFunctionFlags::new())?;
// Stack: [type_params_tuple, value_closure]

// Swap so unpack_sequence reverse gives correct order
Expand All @@ -2522,7 +2522,7 @@ impl Compiler {

let code = self.exit_scope();
self.ctx = prev_ctx;
self.make_closure(code, bytecode::MakeFunctionFlags::empty())?;
self.make_closure(code, bytecode::MakeFunctionFlags::new())?;
emit!(self, Instruction::PushNull);
emit!(self, Instruction::Call { argc: 0 });

Expand Down Expand Up @@ -2561,7 +2561,7 @@ impl Compiler {

let code = self.exit_scope();
self.ctx = prev_ctx;
self.make_closure(code, bytecode::MakeFunctionFlags::empty())?;
self.make_closure(code, bytecode::MakeFunctionFlags::new())?;
// Stack: [name, None, closure]
}

Expand Down Expand Up @@ -2725,7 +2725,7 @@ impl Compiler {
self.ctx = prev_ctx;

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

Ok(())
}
Expand Down Expand Up @@ -3649,7 +3649,7 @@ impl Compiler {
&mut self,
parameters: &ast::Parameters,
) -> CompileResult<bytecode::MakeFunctionFlags> {
let mut funcflags = bytecode::MakeFunctionFlags::empty();
let mut funcflags = bytecode::MakeFunctionFlags::new();

// Handle positional defaults
let defaults: Vec<_> = core::iter::empty()
Expand All @@ -3669,7 +3669,7 @@ impl Compiler {
count: defaults.len().to_u32()
}
);
funcflags |= bytecode::MakeFunctionFlags::DEFAULTS;
funcflags.insert(bytecode::MakeFunctionFlag::Defaults);
}

// Handle keyword-only defaults
Expand All @@ -3694,7 +3694,7 @@ impl Compiler {
count: kw_with_defaults.len().to_u32(),
}
);
funcflags |= bytecode::MakeFunctionFlags::KW_ONLY_DEFAULTS;
funcflags.insert(bytecode::MakeFunctionFlag::KwOnlyDefaults);
}

Ok(funcflags)
Expand Down Expand Up @@ -3844,7 +3844,7 @@ impl Compiler {
let annotate_code = self.exit_annotation_scope(saved_ctx);

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

Ok(true)
}
Expand Down Expand Up @@ -4054,7 +4054,7 @@ impl Compiler {
);

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

// Store as __annotate_func__ for classes, __annotate__ for modules
let name = if parent_scope_type == CompilerScope::Class {
Expand Down Expand Up @@ -4092,10 +4092,10 @@ impl Compiler {

if is_generic {
// Count args to pass to type params scope
if funcflags.contains(bytecode::MakeFunctionFlags::DEFAULTS) {
if funcflags.contains(&bytecode::MakeFunctionFlag::Defaults) {
num_typeparam_args += 1;
}
if funcflags.contains(bytecode::MakeFunctionFlags::KW_ONLY_DEFAULTS) {
if funcflags.contains(&bytecode::MakeFunctionFlag::KwOnlyDefaults) {
num_typeparam_args += 1;
}

Expand All @@ -4120,13 +4120,13 @@ impl Compiler {
// Add parameter names to varnames for the type params scope
// These will be passed as arguments when the closure is called
let current_info = self.current_code_info();
if funcflags.contains(bytecode::MakeFunctionFlags::DEFAULTS) {
if funcflags.contains(&bytecode::MakeFunctionFlag::Defaults) {
current_info
.metadata
.varnames
.insert(".defaults".to_owned());
}
if funcflags.contains(bytecode::MakeFunctionFlags::KW_ONLY_DEFAULTS) {
if funcflags.contains(&bytecode::MakeFunctionFlag::KwOnlyDefaults) {
current_info
.metadata
.varnames
Expand All @@ -4144,11 +4144,10 @@ impl Compiler {
}

// Compile annotations as closure (PEP 649)
let annotations_flag = if self.compile_annotations_closure(name, parameters, returns)? {
bytecode::MakeFunctionFlags::ANNOTATE
} else {
bytecode::MakeFunctionFlags::empty()
};
let mut annotations_flag = bytecode::MakeFunctionFlags::new();
if self.compile_annotations_closure(name, parameters, returns)? {
annotations_flag.insert(bytecode::MakeFunctionFlag::Annotate);
}

// Compile function body
let final_funcflags = funcflags | annotations_flag;
Expand Down Expand Up @@ -4179,7 +4178,7 @@ impl Compiler {
self.ctx = saved_ctx;

// Make closure for type params code
self.make_closure(type_params_code, bytecode::MakeFunctionFlags::empty())?;
self.make_closure(type_params_code, bytecode::MakeFunctionFlags::new())?;

// Call the type params closure with defaults/kwdefaults as arguments.
// Call protocol: [callable, self_or_null, arg1, ..., argN]
Expand Down Expand Up @@ -4347,57 +4346,57 @@ impl Compiler {
emit!(
self,
Instruction::SetFunctionAttribute {
flag: bytecode::MakeFunctionFlags::CLOSURE
flag: bytecode::MakeFunctionFlag::Closure
}
);
}

// Set annotations if present
if flags.contains(bytecode::MakeFunctionFlags::ANNOTATIONS) {
if flags.contains(&bytecode::MakeFunctionFlag::Annotations) {
emit!(
self,
Instruction::SetFunctionAttribute {
flag: bytecode::MakeFunctionFlags::ANNOTATIONS
flag: bytecode::MakeFunctionFlag::Annotations
}
);
}

// Set __annotate__ closure if present (PEP 649)
if flags.contains(bytecode::MakeFunctionFlags::ANNOTATE) {
if flags.contains(&bytecode::MakeFunctionFlag::Annotate) {
emit!(
self,
Instruction::SetFunctionAttribute {
flag: bytecode::MakeFunctionFlags::ANNOTATE
flag: bytecode::MakeFunctionFlag::Annotate
}
);
}

// Set kwdefaults if present
if flags.contains(bytecode::MakeFunctionFlags::KW_ONLY_DEFAULTS) {
if flags.contains(&bytecode::MakeFunctionFlag::KwOnlyDefaults) {
emit!(
self,
Instruction::SetFunctionAttribute {
flag: bytecode::MakeFunctionFlags::KW_ONLY_DEFAULTS
flag: bytecode::MakeFunctionFlag::KwOnlyDefaults
}
);
}

// Set defaults if present
if flags.contains(bytecode::MakeFunctionFlags::DEFAULTS) {
if flags.contains(&bytecode::MakeFunctionFlag::Defaults) {
emit!(
self,
Instruction::SetFunctionAttribute {
flag: bytecode::MakeFunctionFlags::DEFAULTS
flag: bytecode::MakeFunctionFlag::Defaults
}
);
}

// Set type_params if present
if flags.contains(bytecode::MakeFunctionFlags::TYPE_PARAMS) {
if flags.contains(&bytecode::MakeFunctionFlag::TypeParams) {
emit!(
self,
Instruction::SetFunctionAttribute {
flag: bytecode::MakeFunctionFlags::TYPE_PARAMS
flag: bytecode::MakeFunctionFlag::TypeParams
}
);
}
Expand Down Expand Up @@ -4689,14 +4688,14 @@ impl Compiler {
emit!(self, Instruction::PushNull);

// Set up the class function with type params
let mut func_flags = bytecode::MakeFunctionFlags::empty();
let mut func_flags = bytecode::MakeFunctionFlags::new();
emit!(
self,
Instruction::LoadName {
namei: dot_type_params
}
);
func_flags |= bytecode::MakeFunctionFlags::TYPE_PARAMS;
func_flags.insert(bytecode::MakeFunctionFlag::TypeParams);

// Create class function with closure
self.make_closure(class_code, func_flags)?;
Expand Down Expand Up @@ -4819,7 +4818,7 @@ impl Compiler {
self.ctx = saved_ctx;

// Execute the type params function
self.make_closure(type_params_code, bytecode::MakeFunctionFlags::empty())?;
self.make_closure(type_params_code, bytecode::MakeFunctionFlags::new())?;
emit!(self, Instruction::PushNull);
emit!(self, Instruction::Call { argc: 0 });
} else {
Expand All @@ -4828,7 +4827,7 @@ impl Compiler {
emit!(self, Instruction::PushNull);

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

if let Some(arguments) = arguments {
Expand Down Expand Up @@ -7096,12 +7095,12 @@ impl Compiler {
}

self.enter_function(&name, params)?;
let mut func_flags = bytecode::MakeFunctionFlags::empty();
let mut func_flags = bytecode::MakeFunctionFlags::new();
if have_defaults {
func_flags |= bytecode::MakeFunctionFlags::DEFAULTS;
func_flags.insert(bytecode::MakeFunctionFlag::Defaults);
}
if have_kwdefaults {
func_flags |= bytecode::MakeFunctionFlags::KW_ONLY_DEFAULTS;
func_flags.insert(bytecode::MakeFunctionFlag::KwOnlyDefaults);
}

// Set qualname for lambda
Expand Down Expand Up @@ -7785,7 +7784,7 @@ impl Compiler {
self.ctx = prev_ctx;

// Create comprehension function with closure
self.make_closure(code, bytecode::MakeFunctionFlags::empty())?;
self.make_closure(code, bytecode::MakeFunctionFlags::new())?;
emit!(self, Instruction::PushNull);

// Evaluate iterated item:
Expand Down
1 change: 1 addition & 0 deletions crates/compiler-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ ruff_source_file = { workspace = true }
rustpython-wtf8 = { workspace = true }

bitflags = { workspace = true }
bitflagset = { workspace = true }
itertools = { workspace = true }
malachite-bigint = { workspace = true }
num-complex = { workspace = true }
Expand Down
4 changes: 2 additions & 2 deletions crates/compiler-core/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ pub use crate::bytecode::{
oparg::{
BinaryOperator, BuildSliceArgCount, CommonConstant, ComparisonOperator, ConvertValueOparg,
IntrinsicFunction1, IntrinsicFunction2, Invert, Label, LoadAttr, LoadSuperAttr,
MakeFunctionFlags, NameIdx, OpArg, OpArgByte, OpArgState, OpArgType, RaiseKind, ResumeType,
SpecialMethod, UnpackExArgs,
MakeFunctionFlag, MakeFunctionFlags, NameIdx, OpArg, OpArgByte, OpArgState, OpArgType,
RaiseKind, ResumeType, SpecialMethod, UnpackExArgs,
},
};

Expand Down
4 changes: 2 additions & 2 deletions crates/compiler-core/src/bytecode/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
oparg::{
self, BinaryOperator, BuildSliceArgCount, CommonConstant, ComparisonOperator,
ConvertValueOparg, IntrinsicFunction1, IntrinsicFunction2, Invert, Label, LoadAttr,
LoadSuperAttr, MakeFunctionFlags, NameIdx, OpArg, OpArgByte, OpArgType, RaiseKind,
LoadSuperAttr, MakeFunctionFlag, NameIdx, OpArg, OpArgByte, OpArgType, RaiseKind,
SpecialMethod, StoreFastLoadFast, UnpackExArgs,
},
},
Expand Down Expand Up @@ -264,7 +264,7 @@ pub enum Instruction {
i: Arg<u32>,
} = 107,
SetFunctionAttribute {
flag: Arg<MakeFunctionFlags>,
flag: Arg<MakeFunctionFlag>,
} = 108,
SetUpdate {
i: Arg<u32>,
Expand Down
Loading
Loading