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

add required when generating methods with nnbd named arguments. #3770

Closed
bsutton opened this issue Jan 6, 2022 · 8 comments
Closed

add required when generating methods with nnbd named arguments. #3770

bsutton opened this issue Jan 6, 2022 · 8 comments
Labels
in editor Relates to code editing or language features in lsp/analysis server Something to be fixed in the Dart analysis server is enhancement
Milestone

Comments

@bsutton
Copy link

bsutton commented Jan 6, 2022

If I use quickfix 'create method xxxx' and the method signature is:

final package = await DaoPackage().tryByMember(packageName: packageName);

The resulting generated signature is:

tryByMember({String packageName}) {}
`

The signature should be:
```dart
tryByMember({required String packageName}) {}

as nnbd named arguments require the 'required' keyword.

@DanTup
Copy link
Member

DanTup commented Jan 6, 2022

I think it would also be valid to have String? packageName (for example if packageName in the call site was nullable), in which case required would not be required.

I thought this would be an easy fix, but it seems like in some situations the ? is not included so that should probably be fixed first (so we can conditionally add required). I've raised dart-lang/sdk#48086 about that and will look at this again after it's fixed.

@DanTup DanTup added this to the v3.34.0 milestone Jan 6, 2022
@DanTup DanTup added in editor Relates to code editing or language features in lsp/analysis server Something to be fixed in the Dart analysis server labels Jan 6, 2022
@bsutton
Copy link
Author

bsutton commented Jan 6, 2022

I'm only suggesting that we add the required for String not String?.

We don't have enough information to know the intent of String? and I would think not requiring a nullable type would be the most common use case.

@DanTup
Copy link
Member

DanTup commented Jan 6, 2022

I'm only suggesting that we add the required for String not String?.

Yep, I understand. But in the case linked above, I'm not certain if we're always getting the type correctly (it seems to be non-nullable in cases where I expected it to be nullable). However, I added another comment and think maybe it's working as intended an dit's because my example was a literal string and the type was narrowed to just String. I'll wait for confirmation on that though, but I now think it's probably a non-issue :-)

@DanTup
Copy link
Member

DanTup commented Jan 10, 2022

@bwilkerson interested in your opinion on this. Inserting required in the case above seems to make sense, because without it the code is invalid. However the code used to generate the parameters here is shared with some other fixes such as "add missing named parameter" and I think it's less clear what the behaviour should be there. For example, here's an existing test:

  Future<void> test_method_noParameters() async {
    await resolveTestCode('''
class A {
  test() {}

  main() {
    test(named: 42);
  }
}
''');
    await assertHasFix('''
class A {
  test({int named}) {}

  main() {
    test(named: 42);
  }
}
''');
  }

The generated code is also invalid, but I'm not sure if it's clear that it's better to insert required int versus int?. I feel like both are going to be wrong at some point. Should we just pick one, or is it better to generate the code with a warning here and force the user to pick? (or, should there be two options in the fixes?)

(Edit: After typing this, I'm actually wondering whether making the parameter nullable in the original case it also reasonable... just because you're supplying a non-null value here, doesn't necessarily mean that's how you want the new function to be?)

@bsutton
Copy link
Author

bsutton commented Jan 10, 2022 via email

@bwilkerson
Copy link

Inserting required in the case above seems to make sense, because without it the code is invalid.

I agree.

After typing this, I'm actually wondering whether making the parameter nullable in the original case it also reasonable... just because you're supplying a non-null value here, doesn't necessarily mean that's how you want the new function to be?

That's true, but I think in this case it's better to choose the most likely option (which hopefully for most cases will be to use non-nullable types) and let the user change the code manually if necessary. This case has the advantage that we don't have any other invocations of the method being added, which means choosing to make it required won't produce any follow-on errors.

However the code used to generate the parameters here is shared with some other fixes such as "add missing named parameter" and I think it's less clear what the behaviour should be there.
...
The generated code is also invalid, but I'm not sure if it's clear that it's better to insert required int versus int?. I feel like both are going to be wrong at some point.

Yeah, that case is a bit more complicated. There might be other invocation sites, so there might not be a single change that won't cause follow-on errors.

I'm not opposed to having two options for users, but I'm also not opposed to providing a single required int option now and seeing how often users want the other option before adding support for it.

@bsutton
Copy link
Author

bsutton commented Jan 14, 2022

I'm not opposed to having two options for users, but I'm also not opposed to providing a single required int option now and seeing how often users want the other option before adding support for it.

agreed with single required int option for the moment.
Less work and if no one complains it was never needed.

We should be encouraging uses to use nnbd.
99% of my args are nnbd and I would imagine other developers are heading in the same direction as it's just easier to work with.

copybara-service bot pushed a commit to dart-lang/sdk that referenced this issue Jan 20, 2022
…named parameters

Fixes Dart-Code/Dart-Code#3770.

Change-Id: I954b9bbcac72ffc7a7b6e168214971bf1786872a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/228564
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
@DanTup
Copy link
Member

DanTup commented Jan 20, 2022

Fixed by dart-lang/sdk@32b3e18. It's an SDK change so will show up in a future SDK release rather than a Dart-Code one.

@DanTup DanTup closed this as completed Jan 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in editor Relates to code editing or language features in lsp/analysis server Something to be fixed in the Dart analysis server is enhancement
Projects
None yet
Development

No branches or pull requests

3 participants