Skip to content

Static typing error not caught by compiler (generic method, generic class) #45731

Closed
@zathras

Description

@zathras

Here's a fairly minimal program to reproduce the problem:

abstract class Foo<R, A> {
  const Foo();

  R frob(A arg);
}

class Bar extends Foo<int, String> {
  const Bar();

  int frob(String arg) => 42;
}

R select<R, A>(Foo<R, A> selector, A arg) => selector.frob(arg);

void main() {
  const b = Bar();
  final answer = select(b, 'How many roads must a man walk down?');
  print('');
  print('The answer is $answer.');
  print('');
  print("And here's the bug.  This shouldn't compile, but does.");
  print(select(b, 9));
}

Clearly, the argument "9" in the last print statement is a statically knowable typing error, but the compiler doesn't catch it, viz:

billf@zathras:~/tmp$ dart bug.dart

The answer is 42.

And here's the bug.  This shouldn't compile, but does.
Unhandled exception:
type 'int' is not a subtype of type 'String' of 'arg'
#0      Bar.frob (file:///home/billf/tmp/bug.dart)
#1      select (file:///home/billf/tmp/bug.dart:15:55)
#2      main (file:///home/billf/tmp/bug.dart:24:9)
#3      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:283:19)
#4      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)

This static typing error wasn't caught in the compiler, but the runtime of course did barf. I didn't probe too hard to see just how minimal this example is -- not sure if "const" has anything to do with it, for example.

This is on:

Dart SDK version: 2.12.2 (stable) (Wed Mar 17 10:30:20 2021 +0100) on "linux_x64"

Metadata

Metadata

Assignees

No one assigned

    Labels

    closed-as-intendedClosed as the reported issue is expected behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions