@@ -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)]
15391544pub 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(
0 commit comments