Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import Method causing types to mismatch #33149

Closed
tensor-programming opened this issue May 17, 2018 · 12 comments
Closed

Import Method causing types to mismatch #33149

tensor-programming opened this issue May 17, 2018 · 12 comments
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion.

Comments

@tensor-programming
Copy link

tensor-programming commented May 17, 2018

  • Dart 2.0.0-dev.54.0.flutter-46ab040e58 (the current version with the master flutter build)
  • Windows

Steps to recreate issue:

  1. Create three files, each with a custom class inside of it. I.E. a.dart with class A, b.dart with class B and c.dart with class C
  2. Have one of these classes constructors (class B for instance) take in one of the other classes. B(A()) for example.
  3. In class C, import class A from path import A.dart and import b from package: import package:test/B.,dart
  4. In class C, make new instances of Class A and B and try to pass A into B's constructor.
import 'A.dart';
import 'package:bug_test/B.dart';

class C {
  final A a;
  final B b = B(a);
}
Error: [dart] The argument type 'A (<project-path>)' can't be assigned to the parameter type 'A (<project-path>)'.
A value of type '#lib1::A' can't be assigned to a variable of type '#lib2::A'.
Try changing the type of the left hand side, or casting the right hand side to '#lib2::A'.

If you change the Imports to all be from Path or all be from Package then the error goes away.


@zoechi
Copy link
Contributor

zoechi commented May 17, 2018

What is the location of the file that contains the main(){} and how do you run it?

Probably related to #33076

@tensor-programming
Copy link
Author

tensor-programming commented May 17, 2018

This is a flutter application so the main function sits in the main.dart file in the lib folder. The problem occured in a simple flutter application (only RxDart as a library) and I was able to recreate it with a clean flutter application.

Execution is done as normal with flutter run --preview-dart-2

I'm not sure how relevant to #33076 this is, there is only the one entry point (though maybe I am just misreading the issue there).

Edit: It is worth noting that the analyzer does pick up the error with the first error message:

Error: [dart] The argument type 'A (<project-path>)' can't be assigned to the parameter type 'A (<project-path>)'.

and then either on hot reload or during execution you see the other two error messages:

A value of type '#lib1::A' can't be assigned to a variable of type '#lib2::A'.
Try changing the type of the left hand side, or casting the right hand side to '#lib2::A'.

@zoechi
Copy link
Contributor

zoechi commented May 17, 2018

Currently you must not use relative imports in lib/main.dart.
#33076 is about what to change in language and tools for that to not cause issues.

@tensor-programming
Copy link
Author

tensor-programming commented May 17, 2018

The problem is not in main.dart however. As mentioned in the original post, I was able to recreate the problem in a c.dart file which does not contain a main function. (I did originally come across this bug in main.dart but was later able to recreate it using independent files).

Edit: Issue still occurs even if none of the types or files are referenced to in the main.dart file as well.

Issue can also be replicated in a normal dart project. If you have a bin folder with a main.dart file in it and a lib folder with the a.dart, b.dart and c.dart files then you will see this issue on the 2.0.0-dev.55.0 compiler.

Obviously, if you do not import the files into main.dart you can't run the project and see the compiler error but the analyzer still shows the Error: [dart] The argument type 'A (<project-path>)' can't be assigned to the parameter type 'A (<project-path>)'. error message.

@lrhn
Copy link
Member

lrhn commented May 17, 2018

The problem does not depend on the name of the file, but just that it's an entry point for your program that you specify on the command line as a file path, and that it imports a package library without using a package: URI, directly or indirectly.

No file outside of the lib dir, and no entry-point file in the lib dir (like main.dart) should ever refer to to a package library with a relative path, they should always use a package: URI.

We hope to catch entry points inside lib directories at the tool level, but that hasn't happened yet.

@tensor-programming
Copy link
Author

tensor-programming commented May 17, 2018

Right here is a gist of the file structure and the code that was used in the pure dart example which yielded this bug.

As you can see, there are no imports in main as mentioned multiple times before. I never mentioned anything about the names of these files (these names were made just to test the error; the error will occur regardless of what they are called). I am also not talking about dart "best practices"; I am talking about an error that is kicked back by the analyzer with mixed imports.

Even if I delete the bin folder (that contains the main.dart file), the problem persists (no entry points).

bug-example

This image should better demonstrate what it is that I am talking about.

Just to clarify, when I did have main.dart hooked up to this "library" It was in the bin folder and it used full package imports.

Edit: I don't mean to come across as harsh; I just am half asleep and a bit cranky for other reasons.

@zoechi
Copy link
Contributor

zoechi commented May 17, 2018

As you can see, there are no imports in main as mentioned multiple times before.

What is the location of the file that contains the main(){} and how do you run it?

c.dart doesn't contain main(), please show the content of the file that contains main() and how you run it.

@lrhn
Copy link
Member

lrhn commented May 17, 2018

According to the gist, the main file is in the bin folder and doesn't import anything.
The code in lib is not imported by main, so the analyzer seems to be giving warnings as if it doesn't recognize that c is a package file.

@lrhn lrhn added the area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. label May 17, 2018
@tensor-programming
Copy link
Author

tensor-programming commented May 17, 2018

Exactly. Even if I delete the entire Bin folder and remove Main.dart from the equation, the analyzer still kicks the error back. If I rebuild the entire thing as a library, the same thing also occurs.

@zoechi the original bug was found in a flutter application. This gist and the picture an app used to experiment so that I could nail down the specifics of this bug.

@zoechi
Copy link
Contributor

zoechi commented May 17, 2018

Ups, missed the here link

@tensor-programming
Copy link
Author

tensor-programming commented May 17, 2018

@zoechi it's not a problem. Just trying to do my part for the community.

Sorry accidentally hit the close issue button.

@matanlurey
Copy link
Contributor

Duplicate of #33076.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion.
Projects
None yet
Development

No branches or pull requests

4 participants