Skip to content

Commit 76fb633

Browse files
richmckeevercopybara-github
authored andcommitted
Fix parser bug with builtin function using builtin struct.
The issue was that we add the binding of the struct name twice, binding it first to the NameDef and then refining it to point to the StructDef. The special handling in bindings.h for the builtins module was not precise enough and would cause it to ignore the redefinition, leaving the name unexpectedly bound to the NameDef instead of the StructDef. PiperOrigin-RevId: 863021072
1 parent d811c00 commit 76fb633

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

xls/dslx/frontend/bindings.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,12 @@ class Bindings {
150150
// NameDefs shadow the corresponding BuiltinNameDefs for the same entities.
151151
// Without this check, a builtin that uses another builtin in its signature
152152
// would cause problems in type concretization.
153-
if (IsBuiltinModule() && ResolveNode(name).has_value()) {
154-
return;
153+
if (IsBuiltinModule()) {
154+
std::optional<BoundNode> existing = ResolveNode(name);
155+
if (existing.has_value() &&
156+
std::holds_alternative<BuiltinNameDef*>(*existing)) {
157+
return;
158+
}
155159
}
156160
local_bindings_[std::move(name)] = binding;
157161
}

xls/dslx/frontend/builtin_stubs.x

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
struct State<T: type> {}
16+
1517
fn and_reduce<N: u32>(x: uN[N]) -> u1;
1618

1719
fn array_rev<T: type, N: u32>(x: T[N]) -> T[N];
@@ -71,6 +73,8 @@ fn or_reduce<N: u32>(x: uN[N]) -> u1;
7173

7274
fn priority_sel<N: u32, M: u32, S: bool>(x: uN[N], y: xN[S][M][N], z: xN[S][M]) -> xN[S][M];
7375

76+
fn read<T: type>(source: State<T>) -> T;
77+
7478
fn recv_if_non_blocking<T: type>(tok: token, channel: chan<T> in, predicate: bool, value: T) -> (token, T, bool);
7579

7680
fn recv_if<T: type>(tok: token, channel: chan<T> in, predicate: bool, value: T) -> (token, T);
@@ -104,6 +108,8 @@ fn umulp<N: u32>(x: uN[N], y: uN[N]) -> (uN[N], uN[N]);
104108

105109
fn widening_cast<DEST: type, SRC: type>(x: SRC) -> DEST;
106110

111+
fn write<T: type>(dest: State<T>, value: T);
112+
107113
fn xor_reduce<N: u32>(x: uN[N]) -> u1;
108114

109115
fn zip<LHS_TYPE: type, N: u32, RHS_TYPE: type>(lhs: LHS_TYPE[N], rhs: RHS_TYPE[N]) ->
@@ -115,5 +121,3 @@ fn zip<LHS_TYPE: type, N: u32, RHS_TYPE: type>(lhs: LHS_TYPE[N], rhs: RHS_TYPE[N
115121
trait ToBits {
116122
fn to_bits(self) -> bits[bit_count<Self>()];
117123
}
118-
119-
struct State<T: type> {}

xls/dslx/type_system/testdata/type_info_to_proto_test_BitsConstructorTypeProto.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@
3535
5:46-5:50: NUMBER :: `true` :: uN[1]
3636
5:52-5:56: NUMBER :: `true` :: uN[1]
3737
5:60-5:65: NUMBER :: `false` :: uN[1]
38-
23:37-23:39: TYPE_ANNOTATION :: `()` :: typeof(())
38+
25:37-25:39: TYPE_ANNOTATION :: `()` :: typeof(())

0 commit comments

Comments
 (0)