Skip to content

Commit

Permalink
Allow ImmutableList (and any type that extends List) to be the type o…
Browse files Browse the repository at this point in the history
…f an option with allowMultiple = true.

PiperOrigin-RevId: 436517607
  • Loading branch information
janakdr authored and copybara-github committed Mar 22, 2022
1 parent 4c79e05 commit ad103c2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,11 @@ private ImmutableList<TypeMirror> getAcceptedConverterReturnTypes(VariableElemen
optionType);
}
DeclaredType optionDeclaredType = (DeclaredType) optionType;
// optionDeclaredType.asElement().asType() gets us from List<actualType> to List<E>, so this
// is unfortunately necessary.
if (!typeUtils.isAssignable(optionDeclaredType.asElement().asType(), listType)) {
if (!typeUtils.isAssignable(typeUtils.erasure(optionDeclaredType), listType)) {
throw new OptionProcessorException(
optionField,
"Option that allows multiple occurrences must be of type %s, but is of type %s",
"Option that allows multiple occurrences must be assignable to type %s, but is of type"
+ " %s",
listType,
optionType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ public void allowMultipleOptionWithCollectionTypeIsRejected() {
.processedWith(new OptionProcessor())
.failsToCompile()
.withErrorContaining(
"Option that allows multiple occurrences must be of type java.util.List<E>, "
+ "but is of type java.util.Collection<java.lang.String>");
"Option that allows multiple occurrences must be assignable to type java.util.List<E>,"
+ " but is of type java.util.Collection<java.lang.String>");
}

@Test
Expand All @@ -247,8 +247,16 @@ public void allowMultipleOptionWithNonListTypeIsRejected() {
.processedWith(new OptionProcessor())
.failsToCompile()
.withErrorContaining(
"Option that allows multiple occurrences must be of type java.util.List<E>, "
+ "but is of type java.lang.String");
"Option that allows multiple occurrences must be assignable to type java.util.List<E>,"
+ " but is of type java.lang.String");
}

@Test
public void allowMultipleOptionWithImmutableListTypeIsAllowed() {
assertAbout(javaSource())
.that(getFile("ImmutableListTypeForAllowMultipleOption.java"))
.processedWith(new OptionProcessor())
.compilesWithoutError();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2017 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.common.options.processor.optiontestsources;

import com.google.common.collect.ImmutableList;
import com.google.devtools.common.options.Option;
import com.google.devtools.common.options.OptionDocumentationCategory;
import com.google.devtools.common.options.OptionEffectTag;
import com.google.devtools.common.options.OptionsBase;

/** This example options class should compile. */
public class ImmutableListTypeForAllowMultipleOption extends OptionsBase {
@Option(
name = "option",
defaultValue = "null",
documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
effectTags = {OptionEffectTag.NO_OP},
allowMultiple = true)
public ImmutableList<String> badOption;
}

0 comments on commit ad103c2

Please sign in to comment.