Skip to content
Draft
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
77 changes: 44 additions & 33 deletions crates/compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1477,7 +1477,27 @@ impl BarrySource<'_> {
parse_error: Option<&parser::ParseError>,
source_file: &SourceFile,
) -> Option<CompileError> {
if let Some(range) = parse_error.and_then(|error| self.invalid_legacy_operator(error)) {
if let Some(error) = parse_error
&& let Some(range) = self.invalid_legacy_operator(error)
{
if error.location.start() > range.start() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this the right place instead of patching parser?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parser is the cleaner place for this diagnostic. I used the existing BarrySource adapter to keep the extraction independent of a parser release, but the review uncovered a real weakness: for 2 <<> 3, the textual match overlaps the << token and incorrectly emits the obsolete-comparison hint. I confirmed that with compile-only CPython 3.14.8/native checks. A comparison-context check in the pinned parser is the appropriate fix; the compiler override must also stop replacing that parser diagnostic. I am checking the narrow dependency path rather than importing the broader parser changes. This draft is not ready in its current form.

return Some(CompileError::from_source_error(
source_file,
CpythonDiagnostic::new(
"invalid syntax. Maybe you meant '!=' instead of '<>'?".to_owned(),
range.start().to_usize(),
range.end().to_usize(),
),
));
}
// At expression start, '<' is already invalid: the comparison
// production (and its obsolete-operator hint) was never entered.
// Barry mode instead lexed the rewritten '!=' as one token.
let range = if self.source.as_bytes()[range.start().to_usize()] == b'<' {
ruff_text_size::TextRange::at(range.start(), TextSize::new(1))
} else {
range
};
return Some(barry_as_flufl_invalid_legacy_operator_error(
source_file,
range,
Expand Down Expand Up @@ -1520,47 +1540,25 @@ pub fn barry_as_flufl_invalid_legacy_operator_error(
)
}

/// Every `<>` in `source`, located by plain text search. Only used where the
/// operator is not rewritten, so an occurrence inside a string or a comment
/// costs nothing: it can never coincide with the location of a parse error.
fn textual_legacy_not_equal(source: &str) -> Vec<ruff_text_size::TextRange> {
source
.match_indices("<>")
.map(|(offset, matched)| {
ruff_text_size::TextRange::at(
TextSize::new(offset as u32),
TextSize::new(matched.len() as u32),
)
})
.collect()
}

#[doc(hidden)]
pub fn prepare_barry_as_flufl_source(
source: &str,
parser_options: parser::ParseOptions,
inherited: bool,
) -> BarrySource<'_> {
let scanned = (inherited || source.contains("barry_as_FLUFL"))
let scanned = (inherited || source.contains("barry_as_FLUFL") || source.contains("<>"))
.then(|| parser::parse_unchecked(source, parser_options));
let enabled = scanned.as_ref().is_some_and(|scanned| {
inherited
|| codegen::preprocess::future_features(scanned.syntax())
.contains(core::bytecode::CodeFlags::FUTURE_BARRY_AS_BDFL)
});
let Some(scanned) = scanned.filter(|_| enabled) else {
let Some(scanned) = scanned else {
return BarrySource {
source: Cow::Borrowed(source),
not_equal: None,
legacy_not_equal: textual_legacy_not_equal(source),
legacy_not_equal: Vec::new(),
};
};

let not_equal = scanned
.tokens()
.iter()
.find(|token| token.kind() == TokenKind::NotEqual)
.map(Ranged::range);
let enabled = inherited
|| codegen::preprocess::future_features(scanned.syntax())
.contains(core::bytecode::CodeFlags::FUTURE_BARRY_AS_BDFL);
// Match complete adjacent tokens, not the suffix of a left-shift token.
let replacements = scanned
.tokens()
.windows(2)
Expand All @@ -1575,6 +1573,19 @@ pub fn prepare_barry_as_flufl_source(
})
.collect::<Vec<_>>();

if !enabled {
return BarrySource {
source: Cow::Borrowed(source),
not_equal: None,
legacy_not_equal: replacements,
};
}
let not_equal = scanned
.tokens()
.iter()
.find(|token| token.kind() == TokenKind::NotEqual)
.map(Ranged::range);

let source = if replacements.is_empty() {
Cow::Borrowed(source)
} else {
Expand Down Expand Up @@ -2413,7 +2424,7 @@ mod tests {
fn obsolete_not_equal_diagnostic_spans_the_whole_operator() {
let err = compile("2 <> 3\n", Mode::Exec, "<obsolete>", CompileOpts::default())
.expect_err("'<>' outside Barry mode is a syntax error");
assert_eq!(err.to_string(), "invalid syntax");
assert!(matches!(err, CompileError::Parse(_)));
assert_eq!(err.python_location(), (1, 3));
assert_eq!(err.python_end_location(), Some((1, 5)));

Expand All @@ -2426,12 +2437,12 @@ mod tests {
assert_eq!(err.python_end_location(), Some((1, 5)));

// A `<>` that starts a statement is reported at the `<` too, where the
// parser stops instead of one character in.
// parser stops. Exact Python-visible diagnostics are covered by builtin_compile.py.
let err = compile("<>\n", Mode::Exec, "<obsolete>", CompileOpts::default())
.expect_err("a bare '<>' is a syntax error");
assert_eq!(err.to_string(), "invalid syntax");
assert_eq!(err.python_location(), (1, 1));
assert_eq!(err.python_end_location(), Some((1, 3)));
assert!(matches!(err, CompileError::Parse(_)));

// A bracket left open earlier in the source outranks the operator.
let err = compile(
Expand Down
52 changes: 52 additions & 0 deletions extra_tests/snippets/builtin_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,58 @@ def _check_flags_error(flags):
compile("from __future__ import barry_as_FLUFL\nx = 1\n", "<test>", "exec")
assert barry_code.co_flags & barry_flag


# Keep Python-visible obsolete-operator diagnostics in Python coverage.
for source, message, locations in (
(
"2 <> 3\n",
"invalid syntax. Maybe you meant '!=' instead of '<>'?",
(1, 3, 1, 5),
),
("<>\n", "invalid syntax", (1, 1, 1, 2)),
("2 <;\n", "invalid syntax", (1, 4, 1, 5)),
("2 <<> 3\n", "invalid syntax", (1, 5, 1, 6)),
("(2 <<> 3)\n", "invalid syntax", (1, 6, 1, 7)),
("# <>\n2 <<> 3\n", "invalid syntax", (2, 5, 2, 6)),
("(\n2 <> 3\n", "'(' was never closed", (1, 1, 1, 0)),
):
try:
compile(source, "<test>", "exec")
except SyntaxError as error:
assert error.msg == message
assert (
error.lineno,
error.offset,
error.end_lineno,
error.end_offset,
) == locations
else:
raise AssertionError("expected an obsolete-operator syntax error")

assert eval(compile("2 <> 3", "<test>", "eval", flags=barry_flag))

for source, locations in (
("<>\n", (1, 1, 1, 3)),
("2 <<> 3\n", (1, 5, 1, 6)),
("(2 <<> 3)\n", (1, 6, 1, 7)),
):
try:
compile(source, "<test>", "exec", flags=barry_flag)
except SyntaxError as error:
assert error.msg == "invalid syntax"
assert (
error.lineno,
error.offset,
error.end_lineno,
error.end_offset,
) == locations
else:
raise AssertionError("expected an invalid operator syntax error")

for flags in (0, barry_flag):
compile("2 << 3", "<test>", "exec", flags=flags)
compile('"<>"', "<test>", "exec", flags=flags)

n = ast.parse('x = "# type: int"\n', type_comments=True)
assert n.body[0].type_comment is None
n = ast.parse("x = '# type: int'\n", type_comments=True)
Expand Down
Loading