[M] Change in dart/sdk[main]: Add support for constant-requiring patterns

0 views
Skip to first unread message

Brian Wilkerson (Gerrit)

unread,
Feb 7, 2023, 4:19:58 PM2/7/23
to Konstantin Shcheglov, rev...@dartlang.org, Brian Wilkerson

Attention is currently required from: Konstantin Shcheglov.

Brian Wilkerson would like Konstantin Shcheglov to review this change.

View Change

Add support for constant-requiring patterns

Change-Id: Iaad0ec10a35059da85a79823cbf5de5eadd8d4e5
---
M pkg/analysis_server/lib/src/services/completion/dart/suggestion_builder.dart
M pkg/analyzer_plugin/lib/src/utilities/completion/optype.dart
2 files changed, 33 insertions(+), 19 deletions(-)

diff --git a/pkg/analysis_server/lib/src/services/completion/dart/suggestion_builder.dart b/pkg/analysis_server/lib/src/services/completion/dart/suggestion_builder.dart
index e9c0389..4fe3a28 100644
--- a/pkg/analysis_server/lib/src/services/completion/dart/suggestion_builder.dart
+++ b/pkg/analysis_server/lib/src/services/completion/dart/suggestion_builder.dart
@@ -250,6 +250,10 @@
bool get _isNonNullableByDefault =>
request.libraryElement.isNonNullableByDefault;

+ /// Return `true` if the context requires a constant expression.
+ bool get _preferConstants =>
+ request.inConstantContext || request.opType.mustBeConst;
+
/// Add a suggestion for an [accessor] declared within a class or extension.
/// If the accessor is being invoked with a target of `super`, then the
/// [containingMemberName] should be the name of the member containing the
@@ -287,7 +291,7 @@
var elementKind =
_computeElementKind(accessor, distance: inheritanceDistance);
var hasDeprecated = featureComputer.hasDeprecatedFeature(accessor);
- var isConstant = request.inConstantContext
+ var isConstant = _preferConstants
? featureComputer.isConstantFeature(accessor)
: 0.0;
var startsWithDollar =
@@ -324,7 +328,7 @@
var contextType = request.featureComputer
.contextTypeFeature(request.contextType, variableType);
var elementKind = _computeElementKind(parameter);
- var isConstant = request.inConstantContext
+ var isConstant = _preferConstants
? request.featureComputer.isConstantFeature(parameter)
: 0.0;
var relevance = _computeRelevance(
@@ -539,9 +543,8 @@
var elementKind =
_computeElementKind(field, distance: inheritanceDistance);
var hasDeprecated = featureComputer.hasDeprecatedFeature(field);
- var isConstant = request.inConstantContext
- ? featureComputer.isConstantFeature(field)
- : 0.0;
+ var isConstant =
+ _preferConstants ? featureComputer.isConstantFeature(field) : 0.0;
var startsWithDollar =
featureComputer.startsWithDollarFeature(field.name);
var superMatches = featureComputer.superMatchesFeature(
@@ -694,7 +697,7 @@
request.featureComputer.localVariableDistanceFeature(node, variable);
var elementKind =
_computeElementKind(variable, distance: localVariableDistance);
- var isConstant = request.inConstantContext
+ var isConstant = _preferConstants
? request.featureComputer.isConstantFeature(variable)
: 0.0;
var relevance = _computeRelevance(
@@ -728,9 +731,8 @@
var elementKind =
_computeElementKind(method, distance: inheritanceDistance);
var hasDeprecated = featureComputer.hasDeprecatedFeature(method);
- var isConstant = request.inConstantContext
- ? featureComputer.isConstantFeature(method)
- : 0.0;
+ var isConstant =
+ _preferConstants ? featureComputer.isConstantFeature(method) : 0.0;
var isNoSuchMethod = featureComputer.isNoSuchMethodFeature(
_containingMemberName, method.name);
var startsWithDollar = featureComputer.startsWithDollarFeature(method.name);
@@ -991,7 +993,7 @@
var contextType = request.featureComputer
.contextTypeFeature(request.contextType, variableType);
var elementKind = _computeElementKind(parameter);
- var isConstant = request.inConstantContext
+ var isConstant = _preferConstants
? request.featureComputer.isConstantFeature(parameter)
: 0.0;
var relevance = _computeRelevance(
@@ -1165,7 +1167,7 @@
featureComputer.contextTypeFeature(request.contextType, type);
var elementKind = _computeElementKind(accessor);
var hasDeprecated = featureComputer.hasDeprecatedFeature(accessor);
- var isConstant = request.inConstantContext
+ var isConstant = _preferConstants
? featureComputer.isConstantFeature(accessor)
: 0.0;
var startsWithDollar =
@@ -1244,7 +1246,7 @@
/// Add a suggestion for a type [parameter].
void suggestTypeParameter(TypeParameterElement parameter) {
var elementKind = _computeElementKind(parameter);
- var isConstant = request.inConstantContext
+ var isConstant = _preferConstants
? request.featureComputer.isConstantFeature(parameter)
: 0.0;
var relevance = _computeRelevance(
@@ -1381,9 +1383,8 @@
featureComputer.contextTypeFeature(request.contextType, elementType);
var elementKind = _computeElementKind(element);
var hasDeprecated = featureComputer.hasDeprecatedFeature(element);
- var isConstant = request.inConstantContext
- ? featureComputer.isConstantFeature(element)
- : 0.0;
+ var isConstant =
+ _preferConstants ? featureComputer.isConstantFeature(element) : 0.0;
return _computeRelevance(
contextType: contextType,
elementKind: elementKind,
diff --git a/pkg/analyzer_plugin/lib/src/utilities/completion/optype.dart b/pkg/analyzer_plugin/lib/src/utilities/completion/optype.dart
index 61eab0d..fc6faa0 100644
--- a/pkg/analyzer_plugin/lib/src/utilities/completion/optype.dart
+++ b/pkg/analyzer_plugin/lib/src/utilities/completion/optype.dart
@@ -65,6 +65,12 @@
/// Indicates whether the completion target is prefixed.
bool isPrefixed = false;

+ /// Indicates whether the completion location requires a constant expression
+ /// without being a constant context.
+ // TODO(brianwilkerson) Consider using this value to control whether non-const
+ // elements are suggested.
+ bool mustBeConst = false;
+
/// The suggested completion kind.
CompletionSuggestionKind suggestKind = CompletionSuggestionKind.INVOCATION;

@@ -949,14 +955,11 @@

@override
void visitListPattern(ListPattern node) {
- // TODO(brianwilkerson) We need a way to specify that only constants are
- // allowed in this context that is different from "this is a constant
- // context". In both cases, we need to not suggest elements that aren't
- // valid in those contexts.
optype.completionLocation = 'ListPattern_element';
optype.includeReturnValueSuggestions = true;
optype.includeTypeNameSuggestions = true;
optype.includeVarNameSuggestions = true;
+ optype.mustBeConst = true;
}

@override
@@ -1244,6 +1247,7 @@
}
// TODO(brianwilkerson) Suggest things valid for a constant expression.
optype.completionLocation = 'RelationalPattern_operand';
+ optype.mustBeConst = true;
}
}


To view, visit change 281344. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: sdk
Gerrit-Branch: main
Gerrit-Change-Id: Iaad0ec10a35059da85a79823cbf5de5eadd8d4e5
Gerrit-Change-Number: 281344
Gerrit-PatchSet: 1
Gerrit-Owner: Brian Wilkerson <brianwi...@google.com>
Gerrit-Reviewer: Brian Wilkerson <brianwi...@google.com>
Gerrit-Reviewer: Konstantin Shcheglov <sche...@google.com>
Gerrit-Attention: Konstantin Shcheglov <sche...@google.com>
Gerrit-MessageType: newchange

Brian Wilkerson (Gerrit)

unread,
Feb 7, 2023, 4:19:58 PM2/7/23
to Brian Wilkerson, rev...@dartlang.org, Konstantin Shcheglov

Attention is currently required from: Konstantin Shcheglov.

View Change

    To view, visit change 281344. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: sdk
    Gerrit-Branch: main
    Gerrit-Change-Id: Iaad0ec10a35059da85a79823cbf5de5eadd8d4e5
    Gerrit-Change-Number: 281344
    Gerrit-PatchSet: 1
    Gerrit-Owner: Brian Wilkerson <brianwi...@google.com>
    Gerrit-Reviewer: Brian Wilkerson <brianwi...@google.com>
    Gerrit-Reviewer: Konstantin Shcheglov <sche...@google.com>
    Gerrit-Attention: Konstantin Shcheglov <sche...@google.com>
    Gerrit-Comment-Date: Tue, 07 Feb 2023 21:19:55 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: No
    Gerrit-MessageType: comment

    Konstantin Shcheglov (Gerrit)

    unread,
    Feb 7, 2023, 4:27:29 PM2/7/23
    to Brian Wilkerson, rev...@dartlang.org, Commit Queue

    Attention is currently required from: Brian Wilkerson.

    Patch set 1:Code-Review +1

    View Change

      To view, visit change 281344. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: sdk
      Gerrit-Branch: main
      Gerrit-Change-Id: Iaad0ec10a35059da85a79823cbf5de5eadd8d4e5
      Gerrit-Change-Number: 281344
      Gerrit-PatchSet: 1
      Gerrit-Owner: Brian Wilkerson <brianwi...@google.com>
      Gerrit-Reviewer: Brian Wilkerson <brianwi...@google.com>
      Gerrit-Reviewer: Konstantin Shcheglov <sche...@google.com>
      Gerrit-Attention: Brian Wilkerson <brianwi...@google.com>
      Gerrit-Comment-Date: Tue, 07 Feb 2023 21:27:24 +0000
      Gerrit-HasComments: No
      Gerrit-Has-Labels: Yes
      Gerrit-MessageType: comment

      CBuild (Gerrit)

      unread,
      Feb 7, 2023, 4:59:44 PM2/7/23
      to Brian Wilkerson, rev...@dartlang.org, Konstantin Shcheglov, Commit Queue

      Attention is currently required from: Brian Wilkerson.

      go/dart-cbuild result: SUCCESS

      Details: https://goto.google.com/dart-cbuild/find/38ac79d0262e2522f924a94bc964bdb4e5f6532b

      View Change

        To view, visit change 281344. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-Project: sdk
        Gerrit-Branch: main
        Gerrit-Change-Id: Iaad0ec10a35059da85a79823cbf5de5eadd8d4e5
        Gerrit-Change-Number: 281344
        Gerrit-PatchSet: 1
        Gerrit-Owner: Brian Wilkerson <brianwi...@google.com>
        Gerrit-Reviewer: Brian Wilkerson <brianwi...@google.com>
        Gerrit-Reviewer: Konstantin Shcheglov <sche...@google.com>
        Gerrit-Attention: Brian Wilkerson <brianwi...@google.com>
        Gerrit-Comment-Date: Tue, 07 Feb 2023 21:59:39 +0000

        Brian Wilkerson (Gerrit)

        unread,
        Feb 7, 2023, 5:08:58 PM2/7/23
        to Brian Wilkerson, rev...@dartlang.org, CBuild, Konstantin Shcheglov, Commit Queue

        Attention is currently required from: Brian Wilkerson.

        Patch set 1:Commit-Queue +2

        View Change

          To view, visit change 281344. To unsubscribe, or for help writing mail filters, visit settings.

          Gerrit-Project: sdk
          Gerrit-Branch: main
          Gerrit-Change-Id: Iaad0ec10a35059da85a79823cbf5de5eadd8d4e5
          Gerrit-Change-Number: 281344
          Gerrit-PatchSet: 1
          Gerrit-Owner: Brian Wilkerson <brianwi...@google.com>
          Gerrit-Reviewer: Brian Wilkerson <brianwi...@google.com>
          Gerrit-Reviewer: Konstantin Shcheglov <sche...@google.com>
          Gerrit-Attention: Brian Wilkerson <brianwi...@google.com>
          Gerrit-Comment-Date: Tue, 07 Feb 2023 22:08:54 +0000

          Commit Queue (Gerrit)

          unread,
          Feb 7, 2023, 5:18:11 PM2/7/23
          to Brian Wilkerson, rev...@dartlang.org, CBuild, Konstantin Shcheglov

          Commit Queue submitted this change.

          View Change

          Approvals: Konstantin Shcheglov: Looks good to me, approved Brian Wilkerson: Commit
          Add support for constant-requiring patterns

          Change-Id: Iaad0ec10a35059da85a79823cbf5de5eadd8d4e5
          Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/281344
          Reviewed-by: Konstantin Shcheglov <sche...@google.com>
          Commit-Queue: Brian Wilkerson <brianwi...@google.com>

          ---
          M pkg/analysis_server/lib/src/services/completion/dart/suggestion_builder.dart
          M pkg/analyzer_plugin/lib/src/utilities/completion/optype.dart
          2 files changed, 36 insertions(+), 19 deletions(-)

          To view, visit change 281344. To unsubscribe, or for help writing mail filters, visit settings.

          Gerrit-Project: sdk
          Gerrit-Branch: main
          Gerrit-Change-Id: Iaad0ec10a35059da85a79823cbf5de5eadd8d4e5
          Gerrit-Change-Number: 281344
          Gerrit-PatchSet: 2
          Gerrit-Owner: Brian Wilkerson <brianwi...@google.com>
          Gerrit-Reviewer: Brian Wilkerson <brianwi...@google.com>
          Gerrit-Reviewer: Konstantin Shcheglov <sche...@google.com>
          Gerrit-MessageType: merged

          CBuild (Gerrit)

          unread,
          Feb 7, 2023, 5:48:41 PM2/7/23
          to Commit Queue, Brian Wilkerson, rev...@dartlang.org, Konstantin Shcheglov

          go/dart-cbuild result: SUCCESS

          Details: https://goto.google.com/dart-cbuild/find/22882c4e28de6abce91d0afdd3e0d05a80c4a4d6

          View Change

            To view, visit change 281344. To unsubscribe, or for help writing mail filters, visit settings.

            Gerrit-Project: sdk
            Gerrit-Branch: main
            Gerrit-Change-Id: Iaad0ec10a35059da85a79823cbf5de5eadd8d4e5
            Gerrit-Change-Number: 281344
            Gerrit-PatchSet: 2
            Gerrit-Owner: Brian Wilkerson <brianwi...@google.com>
            Gerrit-Reviewer: Brian Wilkerson <brianwi...@google.com>
            Gerrit-Reviewer: Konstantin Shcheglov <sche...@google.com>
            Gerrit-Comment-Date: Tue, 07 Feb 2023 22:48:36 +0000
            Reply all
            Reply to author
            Forward
            0 new messages