Complete

WERETECH-12153

Issue has been resolved. 

The type checker warns about info[:field], even after checking :field is present.

The 4.0.7 release notes say:

 - Add type checking to ‘object[:variable]’ syntax

But now I get warnings from:

function getField(info as Object, field as Symbol) {
    if (info has field) {
        return info[field];
    }
    return null;
}

WARNING: <device>: bug.mc:3: Cannot determine if container access is using container type.

Since we've checked that the field exists, there shouldn't be a need to check the container type (and it could be a class, and not a container at all).

I get the same warning using a literal symbol (obviously I could rewrite this one as info.field):

function getField(info as Object) {
    if (info has :field) {
        return info[:field];
    }
    return null;
}

There doesn't seem to be a way to suppress this warning. The docs mention (:typechecker(options)), but I cant find any documentation for options - other than disableBackgroundCheck and disableGlanceCheck - and I haven't been able to guess an option that helps.

  • Thanks. (:typecheck(false)) will at least get me to a clean build.

  • Well, that is unfortunate. I believe the type checker should be able to safely deduce that obj is a MyClass and not an Array, so it should not warn.

    So, it turns out that there is an undocumented parameter that can be passed to the :typecheck annotation.. You can pass false to disable type checking for a given function entirely. Using the above snippet as an example...

    (:typecheck(false))
    function getValue(obj as MyClass, sym as Symbol) as Number or Float or Null {
        if (obj has sym) {
            return obj[sym];
        }
        
        return null;
    }

    This option was supposed to be documented. I've filed a request to get it added and to have the two blocks that discuss the :typecheck be joined into one.

  • Just to confirm, after fixing up your test case to compile:

    import Toybox.Lang;
    
    class MyClass
    {
        var field1 as Number;
        var field2 as Float;
    
        function initialize(f1 as Number, f2 as Float) {
            field1 = f1;
            field2 = f2;
        }
    
        function getValue(obj as MyClass, sym as Symbol) as Number or Float or Null {
            if (obj has sym) {
                return obj[sym];
            }
    
            return null;
        }
    }
    

    WARNING: <device>: bug.mc:15: Cannot determine if container access is using container type.

  • I found that page (and mentioned disableBackgroundCheck and disableGlanceCheck in my report). But if thats supposed to be a full list of currently supported options for :typecheck, the documentation could be a lot clearer.

    • those options are described in a section titled "Application Scope Type Checking", suggesting that these are the two options related to *that*, not the only two options supported.
    • the Annotations page says "Code blocks decorated with this annotation can direct the type checker to perform / avoid certain checks", implying that it can be used to both enable AND disable particular checks.
  • I originally hit this issue with “obj as Activity.Info”.