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: type declaration with initial value gets the value's type #13025

Merged

Conversation

asterite
Copy link
Member

@asterite asterite commented Jan 28, 2023

Fixes #13023

This actually slightly changes the compiler's semantic.

Look at this code:

a = (b : Int32 = 1)
p a

What do you expect it to print?

Well, previously it printed nil. Now it prints 1. The type (and value) of a local bariable type declaration with an initial value now is the initial value's type and value.

Note: another way to have fixed this would have been to produce a nil for that expression, in the interpreter, just like compiled mode. But I think it makes more sense to change the semantics here.

For type declarations without an initial value the type is still nil:

a = (b : Int32)
a # => nil

the reason is that b doesn't really have any valid value at that point.

This also should cause any breaking change because:

  1. The value of such expressions was probably rarely used.
  2. And if they were used, their value was nil, so unlikely to be used for something (like, say, putting things in a collection)

@Blacksmoke16 Blacksmoke16 added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler:interpreter labels Jan 28, 2023
@asterite asterite marked this pull request as ready for review January 29, 2023 12:21
@straight-shoota straight-shoota added this to the 1.8.0 milestone Jan 29, 2023
@HertzDevil HertzDevil changed the title Compiler: let the type of a type declaration with initial value be th… Compiler: let the type of a type declaration with initial value be that value's type Jan 29, 2023
@straight-shoota straight-shoota changed the title Compiler: let the type of a type declaration with initial value be that value's type Compiler: type declaration with initial value gets the value's type Jan 30, 2023
@straight-shoota straight-shoota merged commit f83e31e into crystal-lang:master Jan 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler:interpreter
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Interpreter: BUG: data left on stack
3 participants