Boo

Compiler should warn when an implicit callable is used in a boolean context

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 0.8.2
  • Fix Version/s: 0.9
  • Component/s: Compiler
  • Labels:
    None
  • Testcase included:
    yes
  • Number of attachments :
    0

Description

Consider an (badly named) API having a method like bool IsFoo().
If one uses that method in a condition, but in thinking it is a property (ie. if x.IsFoo), then Boo will treat it as an 'implicit' callable and thus the condition will always be true.
The only way to notice that error is by seeing unexpected results at run-time :s

cedric@laptop:~/dev/workspace/boo/boo$ cat tests/testcases/regression/BOO-1087-1.boo 
"""
:)
:D
"""

static class StaticBadNaming:
	def IsGood():
		return false

class BadNaming:
	def IsGood():
		return false

if StaticBadNaming.IsGood: #IsGood is a callable, condition is true
	print ":("
else:
	print ":)"

bad = BadNaming()
if bad.IsGood: #IsGood is a callable, condition is true
	print ":'("
else:
	print ":D"

cedric@laptop:~/dev/workspace/boo/boo$ booc tests/testcases/regression/BOO-1087-1.boo 
Boo Compiler version 0.8.2.3038 (CLR v2.0.50727.1433)
cedric@laptop:~/dev/workspace/boo/boo$ ./BOO-1087-1.exe 
:(
:'(

Boo should emit a warning when a implicit callable is used in a boolean context.
Or actually should implicit callables be allowed in a boolean context at all ?? (I'm not sure how it is useful?)

Issue Links

Activity

Hide
Avishay Lavie added a comment -

Sounds like a good idea to provide a warning, at least.

Show
Avishay Lavie added a comment - Sounds like a good idea to provide a warning, at least.
Hide
Cedric Vivier added a comment -

Should this always warn or in strict mode only?
I'd favor the first (always).

Show
Cedric Vivier added a comment - Should this always warn or in strict mode only? I'd favor the first (always).
Hide
Rodrigo B. de Oliveira added a comment -

This could be a general "Condition is always true" warning. Always enabled.

Show
Rodrigo B. de Oliveira added a comment - This could be a general "Condition is always true" warning. Always enabled.
Hide
Cedric Vivier added a comment -

Makes sense.
An "always true or always false" warning.

Show
Cedric Vivier added a comment - Makes sense. An "always true or always false" warning.
Hide
Cedric Vivier added a comment -

Landed in rev. 3152 as a generalized "WARNING: Expression is constant (always true or always false)."

Show
Cedric Vivier added a comment - Landed in rev. 3152 as a generalized "WARNING: Expression is constant (always true or always false)."
Hide
Avishay Lavie added a comment -

I think slightly better wording would be "WARNING: Expression will always have the same boolean value".

Also, it just hit me - callable are not necessarily always true. Any callable that isn't an implicit conversion from a method group might evaluate to false if it's null:

event MyEvent as callable()

MyEvent() if MyEvent # raise MyEvent if it has any subscribers

We don't want a warning on that. So warnings should only be emitted when using method names in a boolean context.

Show
Avishay Lavie added a comment - I think slightly better wording would be "WARNING: Expression will always have the same boolean value". Also, it just hit me - callable are not necessarily always true. Any callable that isn't an implicit conversion from a method group might evaluate to false if it's null:
event MyEvent as callable()

MyEvent() if MyEvent # raise MyEvent if it has any subscribers
We don't want a warning on that. So warnings should only be emitted when using method names in a boolean context.
Hide
Cedric Vivier added a comment -

This case with event does not warn since code is already looking specifically for the method kind of callables.

Anyways your comment made me review code and I noticed only implicit internal callables issued a warning, this is fixed now (rev. 3164).

Changed the text to "WARNING: Boolean expression will always have the same value."

Show
Cedric Vivier added a comment - This case with event does not warn since code is already looking specifically for the method kind of callables. Anyways your comment made me review code and I noticed only implicit internal callables issued a warning, this is fixed now (rev. 3164). Changed the text to "WARNING: Boolean expression will always have the same value."

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: