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

"if-elif-else" branches will break if one of the "elif" statements is written on one line with the block that contains the other "if" statement #90016

Open
vstalivyshel opened this issue Mar 29, 2024 · 6 comments

Comments

@vstalivyshel
Copy link

Tested versions

Tested in:

  • 4.2.1.stable.official.b09f793f5
  • 4.3.dev.custom_build.f9ebd84b5

System information

Godot v4.2.1.stable - Fedora Linux 39 (Thirty Nine) - Tty - GLES3 (Compatibility) - NVCF () - AMD Athlon(tm) II X2 210e Processor (2 Threads)

Issue description

When writing elif with the related block, which contains other if, on one line, the next branhces will not be checked, and no errors will be produced. 

Steps to reproduce

extends Node


func _process(_delta):
	# Doesn't work
	if false: print("Will not be printed. Ok")
	# Weird behaviour here
	elif false: if true: print("Will not be printed. Ok")
	else: print("Also will not be printed. Why?")


	# Works
	if false: print("Will not be printed. Ok")
	elif false:
		if true: print("Will not be printed. Ok")
	else: print("Will be printed. Ok")

Minimal reproduction project (MRP)

N/A

@rt9391
Copy link

rt9391 commented Mar 29, 2024

func _ready() -> void:
	# Doesn't work
	if false: print("Will not be printed. Ok")
	# Weird behaviour here
	elif false: if false: print("Will not be printed. Ok")
	else: print("Also will not be printed. Why?")
	else: print("This is it")

image
image
image

@AThousandShips
Copy link
Member

Related:

@dalexeev
Copy link
Member

dalexeev commented Mar 29, 2024

Your script is parsed as:

Class <unnamed> :
|   Function _process( _delta ) :
|   |   If false :
|   |   |   print( "Will not be printed. Ok" )
|   |   Else :
|   |   |   If false :
|   |   |   |   If true :
|   |   |   |   |   print( "Will not be printed. Ok" )
|   |   |   |   Else :
|   |   |   |   |   print( "Also will not be printed. Why?" )
|   |   If false :
|   |   |   print( "Will not be printed. Ok" )
|   |   Else :
|   |   |   If false :
|   |   |   |   If true :
|   |   |   |   |   print( "Will not be printed. Ok" )
|   |   |   Else :
|   |   |   |   print( "Will be printed. Ok" )

(Note that the parser converts elif to else if.)

More clearly:

While you expect:

This could probably be considered a bug, but I need to look at the source code.

@AThousandShips
Copy link
Member

See also (arguably a duplicate of this):

@vstalivyshel
Copy link
Author

@AThousandShips, yea, I understand now. It is a bit confusing, but make sense in some way. Still weird that it doesn't recognize missing indentation before else.

@ajreckof
Copy link
Contributor

ajreckof commented Apr 5, 2024

I would consider this a bug since I would expect it to be formated like this

# Doesn't work
if false: print("Will not be printed. Ok")
# Weird behaviour here
elif false: if true: print("Will not be printed. Ok")
    else: print("Will not be printed. Ok")
else : print("Will not be printed. Ok")

but changing it now will break compat so I'm not sure we should fix it.

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

5 participants