Issue Details (XML | Word | Printable)

Key: BOO-1087
Type: Bug Bug
Status: Resolved Resolved
Resolution: Fixed
Priority: Major Major
Assignee: Cedric Vivier
Reporter: Cedric Vivier
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
Boo

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

Created: 17/Oct/08 12:37 PM   Updated: 22/Jan/09 12:54 AM   Resolved: 21/Jan/09 04:58 AM
Return to search
Component/s: Compiler
Affects Version/s: 0.8.2
Fix Version/s: 0.9

Time Tracking:
Not Specified

Issue Links:
Related
 

Testcase included: yes


 Description  « Hide

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?)



Avishay Lavie added a comment - 17/Oct/08 01:13 PM

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


Cedric Vivier added a comment - 16/Jan/09 09:29 PM

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


Rodrigo B. de Oliveira added a comment - 17/Jan/09 08:09 PM

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


Cedric Vivier added a comment - 18/Jan/09 12:34 AM

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


Cedric Vivier added a comment - 21/Jan/09 04:58 AM

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


Avishay Lavie added a comment - 21/Jan/09 12:23 PM

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.


Cedric Vivier added a comment - 22/Jan/09 12:54 AM

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."