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

GDScript: Disallow type inference with untyped initializer #71349

Merged
merged 1 commit into from
Jan 25, 2023

Conversation

vonagam
Copy link
Contributor

@vonagam vonagam commented Jan 13, 2023

func test():
  var untyped = 1
  var inferred := untyped # complains now:
  # Cannot infer the type of "inferred" variable because the value doesn't have a set type.

inferred will be confident in its type, while in reality untyped can end up being a different thing. (Not in such simple example, but if untyped is a member variable or an argument.)

Related to discussions in #69163 and #70733.

@vonagam vonagam requested a review from a team as a code owner January 13, 2023 18:56
@akien-mga akien-mga added this to the 4.0 milestone Jan 13, 2023
@dalexeev
Copy link
Member

I tested this PR, for variables and parameters it works correctly. We probably need to add tests for the parameters as well.

Notes:

1. It's not directly related to this PR, but I'm surprised that := works for constants at all. I thought that this is forbidden at the parser level.

const A = 1
const B := A

2. The error does not occur if, for example, the initializer is a method that returns Variant:

var a = 1
var b1 := a # Error
var b2 := self.a # Error
var b3 := get("a") # OK (Variant)

This can be confusing for users.

@vonagam
Copy link
Contributor Author

vonagam commented Jan 16, 2023

Logic for constants was that they always do infer with or without a colon. Did not want to change much of behaviors, so I kept this logic during unification.

As for variables, we discussed this at last meeting, currently there is a distinction between typed Variant and untyped Variant. You cannot infer on untyped Variant (as message correctly says there is no set type), but you can on Variant (since the type is set). Before unification it wasn't like that.

I propose to add warning with default level of error for this, so that by default your b3 will complain, but user can disable those.

@akien-mga akien-mga merged commit 41e5f5b into godotengine:master Jan 25, 2023
@akien-mga
Copy link
Member

Thanks!

@vonagam vonagam deleted the disallow-infer-on-weak branch January 26, 2023 00:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants