Skip to content

Commit 0c143af

Browse files
authored
Merge pull request #286 from layus/checkArity
Ensure field unicity for records
2 parents d630f26 + 295249f commit 0c143af

File tree

2 files changed

+45
-14
lines changed

2 files changed

+45
-14
lines changed

vm/vm/main/dynbuilders-decl.hh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,6 @@ struct UnstableField {
8181
UnstableNode value;
8282
};
8383

84-
template <class T>
85-
inline
86-
void sortFeatures(VM vm, size_t width, T features[]);
87-
8884
template <class T>
8985
inline
9086
UnstableNode buildArityDynamic(VM vm, RichNode label, size_t width,

vm/vm/main/dynbuilders.hh

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,16 @@ namespace internal {
143143
return const_cast<UnstableNode&>(element.feature);
144144
}
145145

146+
inline
147+
UnstableNode valueOf(VM vm, const UnstableNode& element) {
148+
return UnstableNode();
149+
}
150+
151+
inline
152+
UnstableNode& valueOf(VM vm, const UnstableField& element) {
153+
return const_cast<UnstableNode&>(element.value);
154+
}
155+
146156
template <class T>
147157
inline
148158
bool isTupleFeatureArray(VM vm, size_t width, T elements[]) {
@@ -155,17 +165,37 @@ namespace internal {
155165

156166
return true;
157167
}
158-
}
159168

160-
template <class T>
161-
void sortFeatures(VM vm, size_t width, T features[]) {
162-
using internal::featureOf;
169+
template <class T>
170+
inline
171+
void sortFeatures(VM vm, size_t width, T features[]) {
172+
std::sort(features, features+width,
173+
[vm] (const T& lhs, const T& rhs) -> bool {
174+
return compareFeatures(vm, featureOf(lhs), featureOf(rhs)) < 0;
175+
}
176+
);
177+
}
178+
179+
template <class T>
180+
inline
181+
bool findDuplicateFeature(VM vm, size_t width, T features[]) {
182+
return features+width != std::adjacent_find(features, features+width,
183+
[vm] (const T& lhs, const T& rhs) -> bool {
184+
return !compareFeatures(vm, featureOf(lhs), featureOf(rhs));
185+
}
186+
);
187+
}
188+
189+
template <class T>
190+
inline
191+
UnstableNode featuresList(VM vm, size_t width, T features[]) {
192+
return buildListDynamic(vm, width, features,
193+
[vm] (const T& e) -> UnstableNode {
194+
return buildSharp(vm, featureOf(e), valueOf(vm, e));
195+
}
196+
);
197+
}
163198

164-
std::sort(features, features+width,
165-
[vm] (const T& lhs, const T& rhs) -> bool {
166-
return compareFeatures(vm, featureOf(lhs), featureOf(rhs)) < 0;
167-
}
168-
);
169199
}
170200

171201
template <class T>
@@ -181,7 +211,12 @@ UnstableNode buildArityDynamic(VM vm, RichNode label, size_t width,
181211
requireFeature(vm, featureOf(elements[i]));
182212

183213
// Sort the features
184-
sortFeatures(vm, width, elements);
214+
internal::sortFeatures(vm, width, elements);
215+
216+
// Forbid duplicated features
217+
if (internal::findDuplicateFeature(vm, width, elements))
218+
raiseKernelError(vm, "recordConstruction", label,
219+
internal::featuresList(vm, width, elements));
185220

186221
// Check if the corresponding record should be a Tuple instead
187222
if (internal::isTupleFeatureArray(vm, width, elements))

0 commit comments

Comments
 (0)