Boo

Implicit downcast in strict mode

Details

  • Type: Wish Wish
  • Status: Resolved Resolved
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: 0.8, 0.9
  • Fix Version/s: 0.9.2
  • Component/s: Compiler, Emitter
  • Labels:
    None
  • Testcase included:
    yes
  • Number of attachments :
    0

Description

Testcase :

names as (string) = ("titi", "toto", 1,)

This compiles, and throws a InvalidCastException at run-time.

Reported by Marc Dassonneville.

Issue Links

Activity

Hide
Daniel Grunwald added a comment -

This is not a bug.

Your code is equivalent to:
array1 as (object) = ("titi", "toto", 1,)
names as (string) = array1

This is a valid implicit cast, just like:
o1 as object = 1
name as string = o1

Although I would prefer if downcasts weren't implicit.

Show
Daniel Grunwald added a comment - This is not a bug. Your code is equivalent to: array1 as (object) = ("titi", "toto", 1,) names as (string) = array1 This is a valid implicit cast, just like: o1 as object = 1 name as string = o1 Although I would prefer if downcasts weren't implicit.
Hide
Cedric Vivier added a comment - - edited

True.

Actually you made me remember we have had some discussion about this on the list some time ago :

http://groups.google.com/group/boolang/browse_thread/thread/6ca52fca357c4b37

The downcast is by design, however there is still the option of a gentle booc warning about this, after looking at the discussion again maybe we could emit the warning only when the implicit cast occurs in a assignment expression in order to keep the "Dispatch example" Rodrigo pointed out in the discussion to compile without warning.

What do you think?

Show
Cedric Vivier added a comment - - edited True. Actually you made me remember we have had some discussion about this on the list some time ago : http://groups.google.com/group/boolang/browse_thread/thread/6ca52fca357c4b37 The downcast is by design, however there is still the option of a gentle booc warning about this, after looking at the discussion again maybe we could emit the warning only when the implicit cast occurs in a assignment expression in order to keep the "Dispatch example" Rodrigo pointed out in the discussion to compile without warning. What do you think?
Hide
Cedric Vivier added a comment - - edited

I'm thinking of three options right now wrt to implicit downcasts in strict context :

1) simply forbid all implicit downcasts
2) forbid implicit interface downcasts (since that feature in effect removes any type safety when a side of an expression is an interface, something unacceptable imho in strict mode, see BOO-1211)
3) warn about implicit downcasts (but it would need to be noisier for interface implicit downcasts imo)

I'm leaning towards (1), as it is simpler (no exceptions) and because using strict mode implies type safety is more important than the convenience of being able to perform a downcast without an explicit cast.

Opinions?

Show
Cedric Vivier added a comment - - edited I'm thinking of three options right now wrt to implicit downcasts in strict context : 1) simply forbid all implicit downcasts 2) forbid implicit interface downcasts (since that feature in effect removes any type safety when a side of an expression is an interface, something unacceptable imho in strict mode, see BOO-1211) 3) warn about implicit downcasts (but it would need to be noisier for interface implicit downcasts imo) I'm leaning towards (1), as it is simpler (no exceptions) and because using strict mode implies type safety is more important than the convenience of being able to perform a downcast without an explicit cast. Opinions?
Hide
Daniel Grunwald added a comment -

In strict mode, I'd say "1) simply forbid all implicit downcasts".

Though I've been thinking that all strict-mode features should be warnings so that the user can specify how to handle each case (treat warning as error, warning, ignore) - Microsoft handles the optional strong typing features in VB.NET this way.

Show
Daniel Grunwald added a comment - In strict mode, I'd say "1) simply forbid all implicit downcasts". Though I've been thinking that all strict-mode features should be warnings so that the user can specify how to handle each case (treat warning as error, warning, ignore) - Microsoft handles the optional strong typing features in VB.NET this way.
Hide
Rodrigo B. de Oliveira added a comment -

(1)

But there should be a way to make implicit downcasts errors into warnings.

Show
Rodrigo B. de Oliveira added a comment - (1) But there should be a way to make implicit downcasts errors into warnings.
Hide
Cedric Vivier added a comment - - edited

Daniel, most strict-mode features are already warnings, and you can use -warnaserror[:W1,Wn] or -nowarn[:W1,Wn] to fine-tune it to your wish Or even macros for that matter, using COmpilerParameters.SuppressWarning("BCW0000") etc..

Rodrigo, let's go for one then I guess we can add a general -erroraswarn:[W1:Wn] flag to handle what you suggest.

Show
Cedric Vivier added a comment - - edited Daniel, most strict-mode features are already warnings, and you can use -warnaserror[:W1,Wn] or -nowarn[:W1,Wn] to fine-tune it to your wish Or even macros for that matter, using COmpilerParameters.SuppressWarning("BCW0000") etc.. Rodrigo, let's go for one then I guess we can add a general -erroraswarn:[W1:Wn] flag to handle what you suggest.
Hide
Cedric Vivier added a comment -

Landed in rev. 3330

Added new optional warning BCW0028 for implicit downcasts.
Strict mode by default disables implicit downcast feature and as such issues a regular context-dependent error.
If only a warning is desired use -warn:BCW0028 (which also works when not in strict mode of course).

Show
Cedric Vivier added a comment - Landed in rev. 3330 Added new optional warning BCW0028 for implicit downcasts. Strict mode by default disables implicit downcast feature and as such issues a regular context-dependent error. If only a warning is desired use -warn:BCW0028 (which also works when not in strict mode of course).

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: