Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Use it in preprocess as well
  • Loading branch information
ShaharNaveh committed Jun 28, 2026
commit b68f0e2c2d1450b06414559219057bd3a5f1f089
48 changes: 31 additions & 17 deletions crates/codegen/src/preprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use ruff_python_ast::{
visitor::transformer::{self, Transformer},
};
use ruff_text_size::{Ranged, TextRange};

use crate::compile::FutureFeature;
use rustpython_compiler_core::bytecode;

const MAXDIGITS: usize = 3;
Expand Down Expand Up @@ -232,30 +234,41 @@ pub fn checked_future_features_in_body(
..
}) if *level == 0 && module.as_ref().map(|id| id.as_str()) == Some("__future__") => {
for alias in names {
match alias.name.as_str() {
"nested_scopes" | "generators" | "division" | "absolute_import"
| "with_statement" | "print_function" | "unicode_literals"
| "generator_stop" => {}
"annotations" => {
future_features.insert(bytecode::CodeFlags::FUTURE_ANNOTATIONS);
}
// Accept the future feature name, but leave it
// as a RustPython no-op.
"barry_as_FLUFL" => {}
"braces" => {
return Err(FutureFeatureError {
let future_feature =
alias
.name
.as_str()
.try_into()
.map_err(|name| FutureFeatureError {
features: future_features,
range: alias.range,
kind: FutureFeatureErrorKind::InvalidBraces,
});
}
other => {
kind: FutureFeatureErrorKind::InvalidFeature(name),
})?;

match future_feature {
FutureFeature::Braces => {
return Err(FutureFeatureError {
features: future_features,
range: alias.range,
kind: FutureFeatureErrorKind::InvalidFeature(other.to_owned()),
kind: FutureFeatureErrorKind::InvalidBraces,
});
}
FutureFeature::Annotations => {
future_features.insert(bytecode::CodeFlags::FUTURE_ANNOTATIONS)
}
FutureFeature::BarryAsFLUFL => {
// We do not support Barry-as-BDFL parser mode yet. This is a nop for now.
}
FutureFeature::AbsoluteImport
| FutureFeature::Division
| FutureFeature::GeneratorStop
| FutureFeature::Generators
| FutureFeature::NestedScopes
| FutureFeature::PrintFunction
| FutureFeature::UnicodeLiterals
| FutureFeature::WithStatement => {
// Python 3 features. They are already implemented by default.
}
}
}
}
Expand Down Expand Up @@ -298,6 +311,7 @@ pub fn preprocess_mod(
}
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct AstPreprocessor {
optimize: u8,
future_annotations: bool,
Expand Down
Loading