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

Disallow traits applied to public prelude shapes #317

Merged
merged 1 commit into from
Mar 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public void onTrait(ShapeId target, ShapeId trait, Node traitValue) {
onTrait(target, traitDef);
} else {
PendingTrait pendingTrait = new PendingTrait(trait, traitValue);
pendingTraits.computeIfAbsent(target, targetId -> new ArrayList<>()).add(pendingTrait);
addPendingTrait(target, traitValue.getSourceLocation(), trait, pendingTrait);
}
}

Expand All @@ -294,7 +294,22 @@ public void onTrait(ShapeId target, ShapeId trait, Node traitValue) {
*/
public void onTrait(ShapeId target, Trait trait) {
PendingTrait pending = new PendingTrait(target, trait);
pendingTraits.computeIfAbsent(target, targetId -> new ArrayList<>()).add(pending);
addPendingTrait(target, trait.getSourceLocation(), trait.toShapeId(), pending);
}

private void addPendingTrait(ShapeId target, SourceLocation sourceLocation, ShapeId trait, PendingTrait pending) {
if (Prelude.isImmutablePublicPreludeShape(target)) {
onError(ValidationEvent.builder()
.severity(Severity.ERROR)
.eventId(Validator.MODEL_ERROR)
.sourceLocation(sourceLocation)
.shapeId(target)
.message(String.format(
"Cannot apply `%s` to an immutable prelude shape defined in `smithy.api`.", trait))
.build());
} else {
pendingTraits.computeIfAbsent(target, targetId -> new ArrayList<>()).add(pending);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,16 @@ public static boolean isPublicPreludeShape(ToShapeId id) {
return PUBLIC_PRELUDE_SHAPE_IDS.contains(toId) || PRELUDE_TRAITS.contains(toId);
}

/**
* Checks if the given shape is an immutable public shape.
*
* @param id Shape to check.
* @return Returns true if the shape is immutable.
*/
static boolean isImmutablePublicPreludeShape(ToShapeId id) {
return PUBLIC_PRELUDE_SHAPE_IDS.contains(id.toShapeId());
}

// Used by the ModelAssembler to load the prelude into another visitor.
static Model getPreludeModel() {
return PreludeHolder.PRELUDE;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ERROR] smithy.api#String: Cannot apply `smithy.api#deprecated` to an immutable prelude shape defined in `smithy.api`. | Model
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace com.foo

apply smithy.api#String @deprecated