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

Compiler accepts multiple +/- signs between numbers #7019

Closed
vicuna opened this issue Oct 14, 2015 · 2 comments
Closed

Compiler accepts multiple +/- signs between numbers #7019

vicuna opened this issue Oct 14, 2015 · 2 comments

Comments

@vicuna
Copy link

vicuna commented Oct 14, 2015

Original bug ID: 7019
Reporter: jessicah
Status: closed (set by @xavierleroy on 2017-02-16T14:14:49Z)
Resolution: not a bug
Priority: low
Severity: minor
Platform: Any
OS: Any
OS Version: Any
Version: 4.02.1
Category: typing
Monitored by: @gasche @diml @hcarty

Bug description

OCaml accepts two numbers separated by 1 or more + or - signs, which should probably be syntax errors.

When using the pre-processor, the examples do turn into syntax errors.

Steps to reproduce

1 + + 2;;

  • : int = 3

1 + + + + + + + + + 2;;

  • : int = 3

1 + + + + +2;;

  • : int = 3

1 - - 3;;

  • : int = -2

1 - - - - - - 3;;

  • : int = - 2

1 - -3;;

  • : int = 4

1 - - -3;;

  • : int = -2

1 - - - -3;;

  • : int = 4

1 - - - - -3;;

  • : int = -2
@vicuna
Copy link
Author

vicuna commented Oct 14, 2015

Comment author: @xavierleroy

I agree these examples are surprising, but they are valid OCaml syntax. "a - b" means subtraction, but "- a" means "opposite of a". Hence, "a - - b" means "a - (- b)". Likewise, "a + b" means addition, but "+ a" means "the same value as a". (This is a dubious feature that OCaml inherited from other languages such as C, C++ and Java.) Hence "a + + b" means "a + (+ b)", i.e. "a + b". Hope this explains the mystery.

@vicuna
Copy link
Author

vicuna commented Oct 16, 2015

Comment author: jessicah

They don't work with the preprocessor though, so there's still a bug somewhere...

cat test.ml:
1 + + 2;;
ocamlopt -pp camlp4o test.ml:
File "test.ml", line 1, characters 2-3:
Parse error: [expr] expected after [infix operator (level 2) (start with '+', '-')] (in [expr])
File "test.ml", line 1:
Error: Error while running external preprocessor
Command line: camlp4o 'test.ml' > /tmp/ocamlppd45529

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant