Skip to content

fix(checker): stop treating unary + as a no-op, so +TRUE is integer and +"a" errors - #911

Merged
sims1253 merged 3 commits into
mainfrom
fix/891-unary-plus
Nov 13, 2026
Merged

sims1253 merged 3 commits into
mainfrom
fix/891-unary-plus

Conversation

@sims1253

@sims1253 sims1253 commented Nov 13, 2026 •

Copy link
Copy Markdown
Owner
 lower_unary
   "-" => UnaryOpKind::Neg
-  "+" => return Some(expr)          # +x became x
+  "+" over Integer/Double literal => return Some(expr)   # +2L is 2L
+  "+" => UnaryOpKind::Pos                                 # +TRUE, +x, +f()

 infer(UnaryOp { op, expr })
   Ops method / Ops.factor dispatch  # now also for +
-  Neg => coerce logical->integer, keep class if mode stays, negate bounds
+  Neg | Pos => coerce logical->integer, keep class if mode stays
+               (only Neg negates bounds), RY020 on character/raw/list/function
   Not => ...

The parser dropped unary +, so +x exported x's type: +TRUE stayed logical, +factor("a") stayed an integer factor, a project Ops.qux was never asked, and +"a", which R rejects, was silent. + now stays in the AST as UnaryOpKind::Pos and takes #890's unary - path. The one exception is + over an integer or double literal, which the parser still folds into the literal: R gives the same value and type, and a classless literal never dispatches Ops. So x[+2], switch(+2L, ...), rep("a", +3) and sum(v, NA) == +1 keep the proofs a bare literal has. UnaryOpKind::symbol() replaces the five copies of the operator-to-string match.

On the issue's table, dump-facts now gives: +TRUE integer/no class, +structure(TRUE, class = "foo") integer/no class, +factor("a") logical with RY042, +structure(1, class = "qux") with Ops.qux unknown, +structure(1, class = "foo") double foo, +Sys.Date() unknown class (the same as #890's -Sys.Date()), and +"a" RY020. -"a" and !"a" still report RY020/RY021.

Proof: the new unit test unary_plus_follows_the_unary_minus_rule failed before the fix (+TRUE was Logical) and passes after. It also pins the RY020 wording for +"a". The parser test unary_plus_folds_only_over_numeric_literals pins the AST shape. New fixtures: ry020_unary_plus.R, oracle unary_plus_type_claim.R (R errors on +"a") and facts oracle facts_ops_unary_plus_class.R (checked against GNU R). dump-facts on +1, +2L, v[+2], rep("a", +3) and switch(+2L, ...) gives exact types and lengths again. Locally cargo test -p ry-checker --test corpus, --test oracle, -p ry-core, -p ry-cli --test e2e facts_oracle -- --include-ignored and cargo fmt --check pass, and ecosystem/run.sh --check reports both corpus manifests current, so no ledger changes.

Reviewed with sol-loop: 2 rounds with gpt-6.1-sol on high. Sol verdict: approved

Closes #891

Claude Opus 5.5 on behalf of Max

Summary by CodeRabbit

  • Bug Fixes
    • Unary + now follows R’s arithmetic behavior instead of always returning its operand unchanged. Numeric literals retain their existing behavior, while other values are coerced or dispatched according to their type and class.
    • Unsupported operands now produce the appropriate error, and factor operands may produce a warning.

@pullfrog

pullfrog Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Custom returned an error, so this run stopped. If it names your key, plan or balance, fix that with the provider or pick another model.

Rate limit reached for requests (429)

Pick another model →

Pullfrog  | Rerun failed job ➔ | View workflow run | via Pullfrog | Using glm-5.3

@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The parser now preserves unary + for operands other than integer and double literals. The checker applies unary inference to the retained operator, including mode and class handling, method dispatch, numeric bounds, and invalid-operand diagnostics.

Changes: Unary plus semantics

Layer / File(s) Summary
Parser representation
crates/ry-core/src/ast.rs, crates/ry-core/src/parser.rs, crates/ry-core/src/types.rs, crates/ry-checker/src/infer/recall.rs, crates/ry-checker/src/infer/value_kinds.rs, crates/ry-checker/testdata/cases/recall_rules.R
UnaryOpKind::symbol() provides operator spellings. The parser folds unary + over integer and double literals, but retains it for other operands. Related comments describe the parser behavior.
Checker inference and validation
crates/ry-checker/src/infer/*, crates/ry-checker/src/tests/ops_fallback.rs, crates/ry-checker/testdata/*, CHANGELOG.md
Unary inference handles + for modes, classes, dispatch, and numeric bounds. Invalid operands use the applied operator in diagnostics. Tests and oracle fixtures cover result facts and errors; the changelog describes the change.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

  • sims1253/ry#892: Changes unary operator class inference and fallback paths that this PR extends to unary +.
Merge Risk: Minimal · up to 6ba4f

Unary plus is now preserved and checked like unary minus, including RY020 for character operands. There is a minor, narrow gap: unary plus on a union-typed operand may hide a downstream diagnostic. This does not block merging.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: unary + no longer gets dropped as a no-op. It gives two concrete outcomes that match the changeset: +TRUE becomes integer and +"a" raises RY020. The raw summary …
Linked Issues check Passed [#891] Requirements: keep unary + in the AST, apply the #890 unary-minus rule (logical to integer, keep class when mode is unchanged, unknown class where Ops may dispatch), handle every UnaryOpKind ma…
Out of Scope Changes check Passed All changes relate to unary + handling. Parser and infer changes implement #891. UnaryOpKind::symbol() replaces duplicated operator matches in the touched unary paths, as the issue requires for depars…
Full details: Title check

Explanation

The title names the main change: unary + no longer gets dropped as a no-op. It gives two concrete outcomes that match the changeset: +TRUE becomes integer and +"a" raises RY020. The raw summary confirms unary + now stays as UnaryOpKind::Pos in the AST (except for numeric literals), and the checker applies logical-to-integer coercion and an RY020 error for character operands. The title matches the objectives and linked issue #891.

Full details: Linked Issues check

Explanation

[#891] Requirements: keep unary + in the AST, apply the #890 unary-minus rule (logical to integer, keep class when mode is unchanged, unknown class where Ops may dispatch), handle every UnaryOpKind match, and reject +"a" with RY020. The whole-PR summary (merge base f4368f8 to head 6ba4f1b) shows: lower_unary in parser.rs emits UnaryOpKind::Pos for non-literal operands; infer/mod.rs routes Pos through the Neg factor and numeric-mode path and negates bounds only for Neg; binop.rs dispatches Ops for +; +"a" reports RY020 (ry020_unary_plus.R, unary_plus_type_claim.R). The PR reports dump-facts results that match each row of the issue table, including Ops.qux and Ops.Date leaving class unknown. UnaryOpKind::symbol() replaces the duplicated operator matches, covering the deparse path. Unit test unary_plus_follows_the_unary_minus_rule covers these cases and the RY020 wording. Deviation: the parser folds unary + over Expr::Integer and Expr::Double literals, so +2 has no Pos node. The issue proposed keeping + in the AST. The folded fact has the same type and class as the unfolded result, so the issue's behavioral requirements still hold. Treated as a documented, behavior-preserving exception. The rebase onto main and the types.rs comment fix since the previous review are comment or rebase changes. They do not alter the requirement evidence. quoting.rs is not named in the summary, so its deparse coverage relies on the shared symbol() helper and is not directly read.

Full details: Out of Scope Changes check

Explanation

All changes relate to unary + handling. Parser and infer changes implement #891. UnaryOpKind::symbol() replaces duplicated operator matches in the touched unary paths, as the issue requires for deparse. Literal folding in lower_unary preserves existing literal-based recognizers (RY105, RY107), which the PR states as its reason. Test and fixture additions (unit test, parser AST test, ry020_unary_plus.R, unary_plus_type_claim.R, facts_ops_unary_plus_class.R, recall_rules.R comments) support the change. Comment-only edits in value_kinds.rs, types.rs, recall.rs, and the CHANGELOG entry describe the new behavior. The summary shows no unrelated behavior change. The rebase onto main after #912 is not reflected in the summary as added code, so no unrelated change from #912 is evident. The ignored snapshot file crates/ry-checker/tests/snapshots/corpus__checker_fixture_diagnostics.snap matches the **/*.snap exclusion rule and was not inspected. It records expected outputs rather than defining a coding requirement, so this does not change the result.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit checks the plus sign's place,
It keeps its work in every case.
For digits, folds the sign away,
For other forms, it stays to play.
Bounds stay still; errors name the sign,
The rabbit nibbles, pleased with the design.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: aca9059c-aa8e-4262-9da8-fa91c2f204e5
Commits

Reviewing files that changed from the base of the PR and between 42b7bd4 and 6ba4f1b.

Files ignored due to path filters: 1

  • crates/ry-checker/tests/snapshots/corpus__checker_fixture_diagnostics.snap is excluded by !**/*.snap
Files selected for processing: 4

  • CHANGELOG.md
  • crates/ry-checker/src/infer/mod.rs
  • crates/ry-core/src/parser.rs
  • crates/ry-core/src/types.rs

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

🔍 OpenCodeReview found 1 issue(s) in this PR.

  • ✅ Successfully posted inline: 1 comment(s)

Comment thread crates/ry-checker/src/infer/mod.rs Resolved
Comment thread crates/ry-core/src/ast.rs Resolved
Comment thread crates/ry-core/src/parser.rs Resolved
Comment thread crates/ry-core/src/parser.rs Resolved
@sims1253

Copy link
Copy Markdown
Owner Author

Note

🤖 Claude Opus 5.5 on behalf of Max

All five OpenCodeReview findings are fixed in 42b7bd4.

  • mod.rs:1650 and parser.rs:635 (+-spelled literals lost their proofs): fixed in the parser instead of at each Neg-only match site. + over an integer or double literal folds back into the literal, as on main: R gives the same value and type, and a classless literal never dispatches Ops. +TRUE, +x and calls stay Pos. dump-facts gives exact facts again for +1, +2L, v[+2], rep("a", +3) and switch(+2L, ...), and sum(v, NA) == +1 compares a plain literal again. The numeric_literal Pos arm is no longer needed and is gone.
  • ops_fallback.rs (no RY020 wording check): added an assertion on the exact message cannot apply unary `+` to `character`.
  • parser.rs (no AST shape test): added unary_plus_folds_only_over_numeric_literals.
  • ast.rs:591 (missing doc comment): added a doc comment on UnaryOpKind::symbol().

Corpus ecosystem/run.sh --check is still current for both manifests.

@pullfrog

pullfrog Bot commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Custom returned an error, so this run stopped. If it names your key, plan or balance, fix that with the provider or pick another model.

Rate limit reached for requests (429)

Pick another model →

Pullfrog  | Rerun failed job ➔ | View workflow run | via Pullfrog | Using glm-5.3

Comment thread crates/ry-core/src/types.rs Resolved
@sims1253

Copy link
Copy Markdown
Owner Author

Note

🤖 Claude Opus 5.5 on behalf of Max

Rebased onto main after #912 (both touch infer/mod.rs). Also fixed OpenCodeReview's doc finding on types.rs: the comment now says the parser drops + only over a numeric literal (363fb28f → rebased head). After the rebase, cargo test -p ry-core -p ry-checker --lib --test corpus passes locally (1,066 + 134 unit tests, corpus 5/5) and cargo fmt --check is clean. CodeRabbit had no actionable comments.

Comment thread crates/ry-checker/src/infer/mod.rs Resolved
sims1253 and others added 3 commits November 13, 2026 02:52
The parser lowered `+x` to `x`, so every fact about `+x` was the
operand's: `+TRUE` stayed logical, `+factor("a")` an integer factor, and
a project `Ops` method was never consulted. `+"a"`, which R rejects, was
silent.

`UnaryOpKind` gains `Pos` and a `symbol()` helper. Inference sends `+`
down the unary `-` path without negating numeric bounds: logical coerces
to integer, the class stays only when the storage mode stays, an `Ops`
method or `Ops.factor` decides classed operands, and character, raw,
list or function operands report RY020. `numeric_literal` folds `+2` to
2 so RY105/RY107 still see the bound.

Closes #891

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Keeping every `+` as `UnaryOpKind::Pos` cost `+`-spelled literals the
proofs a bare literal has: `x[+2]`, `switch(+2L, ...)`, `rep("a", +3)`
and `sum(v, NA) == +1` went opaque, because those recognisers match
only `-literal`. `+2L` is the literal 2L in R (same value and type, and
a classless literal never dispatches `Ops`), so the parser folds `+`
over an integer or double literal as it did before #891. `+TRUE`, `+x`
and calls keep the operator. `numeric_literal` no longer needs a `Pos`
arm.

Also pins the AST shape in a parser test, asserts the RY020 wording for
`+"a"`, and documents `UnaryOpKind::symbol()`.

Refs #891

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sims1253
sims1253 merged commit b7da329 into main Nov 13, 2026
21 checks passed
// `+` over a numeric literal is that literal: same value and
// type, and a classless literal never dispatches `Ops`. Folding
// it keeps every literal proof `2` has (`x[+2]`, `switch(+2L)`).
"+" if matches!(expr, Expr::Integer(..) | Expr::Double(..)) => return Some(expr),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

bug · low
This parse-time fold erases the `+` node, but in R +2 lowers to the call `+`(2) and evaluation resolves `+` like any other binding — `+` <- function(e1) 0 changes the value of +2 even over a classless literal (this is symbol lookup, independent of the Ops group-generic dispatch the comment cites). The checker models exactly this via operator_rebound/resolves_to_base for unary ops: it still sees -2 (the Neg node is kept) and, after this change, +x (the Pos node is kept), but a rebound `+` is invisible for +2, so the checker proves a numeric literal where R returns the rebound value. Consider either documenting this accepted gap, or leaving the literal fold to the infer layer where operator_rebound(self, "+", scope) can gate it (still emitting the literal proof when `+` resolves to base, like the Neg path does).

Suggestion:

Suggested change
"+" if matches!(expr, Expr::Integer(..) | Expr::Double(..)) => return Some(expr),
// `+` over a numeric literal is that literal: same value and
// type, and a classless literal never dispatches `Ops`. Folding
// it keeps every literal proof `2` has (`x[+2]`, `switch(+2L)`).
// Accepted gap: a rebound `` `+` `` (`+ <- function(e1) 0`) also
// changes `+2` in R via plain symbol lookup, but only the infer
// layer (`operator_rebound`) can see that; the fold bypasses it.
"+" if matches!(expr, Expr::Integer(..) | Expr::Double(..)) => return Some(expr),

@sims1253
sims1253 deleted the fix/891-unary-plus branch November 13, 2026 02:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unary + is lowered away, so +TRUE, +factor() and Ops methods export the operand's type

1 participant