Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logical right and left shift diagnostics. #102067

Closed
wants to merge 4 commits into from
Closed
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
4 changes: 4 additions & 0 deletions compiler/rustc_error_messages/locales/en-US/parser.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ parser_invalid_comparison_operator = invalid comparison operator `{$invalid}`
.use_instead = `{$invalid}` is not a valid comparison operator, use `{$correct}`
.spaceship_operator_invalid = `<=>` is not a valid comparison operator, use `std::cmp::Ordering`

parser_invalid_shift_operator = invalid shift operator `{$invalid}`
.logical_left_shift_operator_invalid = `<<<` is not a valid left shift operator, consider shifting normally and fixing the sign as needed
.logical_right_shift_operator_invalid = `>>>` is not a valid right shift operator, consider casting to an unsigned integer and right shifting normally

parser_invalid_logical_operator = `{$incorrect}` is not a logical operator
.note = unlike in e.g., python and PHP, `&&` and `||` are used for logical operators
.use_amp_amp_for_conjunction = use `&&` to perform logical conjunction
Expand Down
18 changes: 18 additions & 0 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,24 @@ pub(crate) enum InvalidComparisonOperatorSub {
Spaceship(#[primary_span] Span),
}

#[derive(SessionDiagnostic)]
#[diag(parser::invalid_shift_operator)]
pub(crate) struct InvalidShiftOperator {
#[primary_span]
pub span: Span,
pub invalid: String,
#[subdiagnostic]
pub sub: InvalidShiftOperatorSub,
}

#[derive(SessionSubdiagnostic)]
pub(crate) enum InvalidShiftOperatorSub {
#[label(parser::logical_left_shift_operator_invalid)]
LogicalLeftShift(#[primary_span] Span),
#[label(parser::logical_right_shift_operator_invalid)]
LogicalRightShift(#[primary_span] Span),
}

#[derive(SessionDiagnostic)]
#[diag(parser::invalid_logical_operator)]
#[note]
Expand Down
38 changes: 34 additions & 4 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ use super::diagnostics::{
FloatLiteralRequiresIntegerPart, IfExpressionMissingCondition, IfExpressionMissingThenBlock,
IfExpressionMissingThenBlockSub, InvalidBlockMacroSegment, InvalidComparisonOperator,
InvalidComparisonOperatorSub, InvalidLogicalOperator, InvalidLogicalOperatorSub,
LeftArrowOperator, LifetimeInBorrowExpression, MacroInvocationWithQualifiedPath,
MalformedLoopLabel, MissingInInForLoop, MissingInInForLoopSub, MissingSemicolonBeforeArray,
NotAsNegationOperator, OuterAttributeNotAllowedOnIfElse, RequireColonAfterLabeledExpression,
SnapshotParser, TildeAsUnaryOperator, UnexpectedTokenAfterLabel,
InvalidShiftOperator, InvalidShiftOperatorSub, LeftArrowOperator, LifetimeInBorrowExpression,
MacroInvocationWithQualifiedPath, MalformedLoopLabel, MissingInInForLoop,
MissingInInForLoopSub, MissingSemicolonBeforeArray, NotAsNegationOperator,
OuterAttributeNotAllowedOnIfElse, RequireColonAfterLabeledExpression, SnapshotParser,
TildeAsUnaryOperator, UnexpectedTokenAfterLabel,
};
use super::pat::{CommaRecoveryMode, RecoverColon, RecoverComma, PARAM_EXPECTED};
use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
Expand Down Expand Up @@ -279,6 +280,35 @@ impl<'a> Parser<'a> {
self.bump();
}

// Look for the logical right shift operator (`>>>`) and recover.
if op.node == AssocOp::ShiftRight
&& self.token.kind == token::Gt
&& self.prev_token.span.hi() == self.token.span.lo()
{
let sp = op.span.to(self.token.span);
self.sess.emit_err(InvalidShiftOperator {
span: sp,
invalid: ">>>".into(),
sub: InvalidShiftOperatorSub::LogicalRightShift(sp),
});
self.bump();
}

// Look for the logical left shift operator (`<<<`) and recover.
// Yea nobody uses this but it's worth being thorough.
moonheart08 marked this conversation as resolved.
Show resolved Hide resolved
if op.node == AssocOp::ShiftLeft
&& self.token.kind == token::Lt
&& self.prev_token.span.hi() == self.token.span.lo()
{
let sp = op.span.to(self.token.span);
self.sess.emit_err(InvalidShiftOperator {
span: sp,
invalid: "<<<".into(),
sub: InvalidShiftOperatorSub::LogicalLeftShift(sp),
});
self.bump();
}

if self.prev_token == token::BinOp(token::Plus)
&& self.token == token::BinOp(token::Plus)
&& self.prev_token.span.between(self.token.span).is_empty()
Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/operator-recovery/logical-left-shift.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
println!("{}", 1 <<< 2);
//~^ERROR invalid shift operator `<<<`
}
8 changes: 8 additions & 0 deletions src/test/ui/operator-recovery/logical-left-shift.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: invalid shift operator `<<<`
--> $DIR/logical-left-shift.rs:2:22
|
LL | println!("{}", 1 <<< 2);
| ^^^ `<<<` is not a valid left shift operator, consider shifting normally and fixing the sign as needed.

error: aborting due to previous error

4 changes: 4 additions & 0 deletions src/test/ui/operator-recovery/logical-right-shift.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
println!("{}", 1 >>> 2);
//~^ERROR invalid shift operator `>>>`
}
8 changes: 8 additions & 0 deletions src/test/ui/operator-recovery/logical-right-shift.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: invalid shift operator `>>>`
--> $DIR/logical-right-shift.rs:2:22
|
LL | println!("{}", 1 >>> 2);
| ^^^ `>>>` is not a valid right shift operator, consider casting to an unsigned integer and right shifting normally.

error: aborting due to previous error