Skip to content

Commit dd0c42f

Browse files
committed
Match obsolete comparison diagnostics with operator tokens
Use the existing Barry adapter token spans for ordinary obsolete-comparison diagnostics as well. Preserve parser errors for left-shift suffixes, malformed comparisons, comments and strings, with final Python 3.15 message and span regressions. Assisted-by: OpenAI Codex:model identifier unavailable
1 parent a107443 commit dd0c42f

2 files changed

Lines changed: 96 additions & 33 deletions

File tree

‎crates/compiler/src/lib.rs‎

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,7 +1477,27 @@ impl BarrySource<'_> {
14771477
parse_error: Option<&parser::ParseError>,
14781478
source_file: &SourceFile,
14791479
) -> Option<CompileError> {
1480-
if let Some(range) = parse_error.and_then(|error| self.invalid_legacy_operator(error)) {
1480+
if let Some(error) = parse_error
1481+
&& let Some(range) = self.invalid_legacy_operator(error)
1482+
{
1483+
if error.location.start() > range.start() {
1484+
return Some(CompileError::from_source_error(
1485+
source_file,
1486+
CpythonDiagnostic::new(
1487+
"invalid syntax. Maybe you meant '!=' instead of '<>'?".to_owned(),
1488+
range.start().to_usize(),
1489+
range.end().to_usize(),
1490+
),
1491+
));
1492+
}
1493+
// At expression start, '<' is already invalid: the comparison
1494+
// production (and its obsolete-operator hint) was never entered.
1495+
// Barry mode instead lexed the rewritten '!=' as one token.
1496+
let range = if self.source.as_bytes()[range.start().to_usize()] == b'<' {
1497+
ruff_text_size::TextRange::at(range.start(), TextSize::new(1))
1498+
} else {
1499+
range
1500+
};
14811501
return Some(barry_as_flufl_invalid_legacy_operator_error(
14821502
source_file,
14831503
range,
@@ -1520,47 +1540,25 @@ pub fn barry_as_flufl_invalid_legacy_operator_error(
15201540
)
15211541
}
15221542

1523-
/// Every `<>` in `source`, located by plain text search. Only used where the
1524-
/// operator is not rewritten, so an occurrence inside a string or a comment
1525-
/// costs nothing: it can never coincide with the location of a parse error.
1526-
fn textual_legacy_not_equal(source: &str) -> Vec<ruff_text_size::TextRange> {
1527-
source
1528-
.match_indices("<>")
1529-
.map(|(offset, matched)| {
1530-
ruff_text_size::TextRange::at(
1531-
TextSize::new(offset as u32),
1532-
TextSize::new(matched.len() as u32),
1533-
)
1534-
})
1535-
.collect()
1536-
}
1537-
15381543
#[doc(hidden)]
15391544
pub fn prepare_barry_as_flufl_source(
15401545
source: &str,
15411546
parser_options: parser::ParseOptions,
15421547
inherited: bool,
15431548
) -> BarrySource<'_> {
1544-
let scanned = (inherited || source.contains("barry_as_FLUFL"))
1549+
let scanned = (inherited || source.contains("barry_as_FLUFL") || source.contains("<>"))
15451550
.then(|| parser::parse_unchecked(source, parser_options));
1546-
let enabled = scanned.as_ref().is_some_and(|scanned| {
1547-
inherited
1548-
|| codegen::preprocess::future_features(scanned.syntax())
1549-
.contains(core::bytecode::CodeFlags::FUTURE_BARRY_AS_BDFL)
1550-
});
1551-
let Some(scanned) = scanned.filter(|_| enabled) else {
1551+
let Some(scanned) = scanned else {
15521552
return BarrySource {
15531553
source: Cow::Borrowed(source),
15541554
not_equal: None,
1555-
legacy_not_equal: textual_legacy_not_equal(source),
1555+
legacy_not_equal: Vec::new(),
15561556
};
15571557
};
1558-
1559-
let not_equal = scanned
1560-
.tokens()
1561-
.iter()
1562-
.find(|token| token.kind() == TokenKind::NotEqual)
1563-
.map(Ranged::range);
1558+
let enabled = inherited
1559+
|| codegen::preprocess::future_features(scanned.syntax())
1560+
.contains(core::bytecode::CodeFlags::FUTURE_BARRY_AS_BDFL);
1561+
// Match complete adjacent tokens, not the suffix of a left-shift token.
15641562
let replacements = scanned
15651563
.tokens()
15661564
.windows(2)
@@ -1575,6 +1573,19 @@ pub fn prepare_barry_as_flufl_source(
15751573
})
15761574
.collect::<Vec<_>>();
15771575

1576+
if !enabled {
1577+
return BarrySource {
1578+
source: Cow::Borrowed(source),
1579+
not_equal: None,
1580+
legacy_not_equal: replacements,
1581+
};
1582+
}
1583+
let not_equal = scanned
1584+
.tokens()
1585+
.iter()
1586+
.find(|token| token.kind() == TokenKind::NotEqual)
1587+
.map(Ranged::range);
1588+
15781589
let source = if replacements.is_empty() {
15791590
Cow::Borrowed(source)
15801591
} else {
@@ -2413,7 +2424,7 @@ mod tests {
24132424
fn obsolete_not_equal_diagnostic_spans_the_whole_operator() {
24142425
let err = compile("2 <> 3\n", Mode::Exec, "<obsolete>", CompileOpts::default())
24152426
.expect_err("'<>' outside Barry mode is a syntax error");
2416-
assert_eq!(err.to_string(), "invalid syntax");
2427+
assert!(matches!(err, CompileError::Parse(_)));
24172428
assert_eq!(err.python_location(), (1, 3));
24182429
assert_eq!(err.python_end_location(), Some((1, 5)));
24192430

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

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

24362447
// A bracket left open earlier in the source outranks the operator.
24372448
let err = compile(

‎extra_tests/snippets/builtin_compile.py‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,58 @@ def _check_flags_error(flags):
6767
compile("from __future__ import barry_as_FLUFL\nx = 1\n", "<test>", "exec")
6868
assert barry_code.co_flags & barry_flag
6969

70+
71+
# Keep Python-visible obsolete-operator diagnostics in Python coverage.
72+
for source, message, locations in (
73+
(
74+
"2 <> 3\n",
75+
"invalid syntax. Maybe you meant '!=' instead of '<>'?",
76+
(1, 3, 1, 5),
77+
),
78+
("<>\n", "invalid syntax", (1, 1, 1, 2)),
79+
("2 <;\n", "invalid syntax", (1, 4, 1, 5)),
80+
("2 <<> 3\n", "invalid syntax", (1, 5, 1, 6)),
81+
("(2 <<> 3)\n", "invalid syntax", (1, 6, 1, 7)),
82+
("# <>\n2 <<> 3\n", "invalid syntax", (2, 5, 2, 6)),
83+
("(\n2 <> 3\n", "'(' was never closed", (1, 1, 1, 0)),
84+
):
85+
try:
86+
compile(source, "<test>", "exec")
87+
except SyntaxError as error:
88+
assert error.msg == message
89+
assert (
90+
error.lineno,
91+
error.offset,
92+
error.end_lineno,
93+
error.end_offset,
94+
) == locations
95+
else:
96+
raise AssertionError("expected an obsolete-operator syntax error")
97+
98+
assert eval(compile("2 <> 3", "<test>", "eval", flags=barry_flag))
99+
100+
for source, locations in (
101+
("<>\n", (1, 1, 1, 3)),
102+
("2 <<> 3\n", (1, 5, 1, 6)),
103+
("(2 <<> 3)\n", (1, 6, 1, 7)),
104+
):
105+
try:
106+
compile(source, "<test>", "exec", flags=barry_flag)
107+
except SyntaxError as error:
108+
assert error.msg == "invalid syntax"
109+
assert (
110+
error.lineno,
111+
error.offset,
112+
error.end_lineno,
113+
error.end_offset,
114+
) == locations
115+
else:
116+
raise AssertionError("expected an invalid operator syntax error")
117+
118+
for flags in (0, barry_flag):
119+
compile("2 << 3", "<test>", "exec", flags=flags)
120+
compile('"<>"', "<test>", "exec", flags=flags)
121+
70122
n = ast.parse('x = "# type: int"\n', type_comments=True)
71123
assert n.body[0].type_comment is None
72124
n = ast.parse("x = '# type: int'\n", type_comments=True)

0 commit comments

Comments
 (0)