Skip to content

Commit

Permalink
Fix some problems with sequence type assumpions
Browse files Browse the repository at this point in the history
Also fix `[x...]` sometimes re-returning `x` when it should actually create a new copy in the form of a list.
  • Loading branch information
GrieferAtWork committed Nov 28, 2023
1 parent a56ac75 commit 00ca9b9
Show file tree
Hide file tree
Showing 8 changed files with 316 additions and 247 deletions.
6 changes: 6 additions & 0 deletions include/deemon/compiler/optimize.h
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,12 @@ ast_predict_type_ex(struct ast *__restrict self, unsigned int flags);
/* Same as `ast_predict_type()', but don't take type annotations into account */
#define ast_predict_type_noanno(self) ast_predict_type_ex(self, AST_PREDICT_TYPE_F_NOANNO)

/* Predict the reference count of a given AST at runtime (if predictable)
* If not predictable, return `0' (which is never a valid reference count) */
INTDEF WUNUSED NONNULL((1)) Dee_refcnt_t DFCALL
ast_predict_object_refcnt(struct ast *__restrict self);

#define ast_predict_object_shared(self) (ast_predict_object_refcnt(self) != 1)


#define CONSTEXPR_ILLEGAL 0 /* Use of this object is not allowed. */
Expand Down
13 changes: 9 additions & 4 deletions src/deemon/compiler/asm/genasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ struct seqops {
STATIC_ASSERT((AST_FMULTIPLE_GENERIC & 3) <= 1);
STATIC_ASSERT((AST_FMULTIPLE_GENERIC_KEYS & 3) == (AST_FMULTIPLE_DICT & 3));

PRIVATE struct seqops seqops_info[4] = {
INTDEF struct seqops seqops_info[4];
INTERN struct seqops seqops_info[4] = {
/* [AST_FMULTIPLE_TUPLE & 3] = */ { &DeeTuple_Type, { ASM_PACK_TUPLE, ASM16_PACK_TUPLE }, ASM_CAST_TUPLE },
/* [AST_FMULTIPLE_LIST & 3] = */ { &DeeList_Type, { ASM_PACK_LIST, ASM16_PACK_LIST }, ASM_CAST_LIST },
/* [AST_FMULTIPLE_HASHSET & 3] = */ { &DeeHashSet_Type, { ASM_PACK_HASHSET, ASM16_PACK_HASHSET }, ASM_CAST_HASHSET },
Expand Down Expand Up @@ -499,8 +500,12 @@ INTERN WUNUSED NONNULL((1)) int
/* The AST starts with an expand expression.
* Because of that, we have to make sure that the entire
* branch gets the correct type by casting now. */
if (ast_predict_type(elem->a_expand) !=
seqops_info[self->a_flag & 3].so_typ) {
DeeTypeObject *expected_type = seqops_info[self->a_flag & 3].so_typ;
if ((expected_type == &DeeTuple_Type /* Immutable sequence type */ ||
!ast_predict_object_shared(elem->a_expand)) &&
(ast_predict_type(elem->a_expand) == expected_type)) {
/* Sequence type is immutable, or not shared */
} else {
if (asm_putddi(self))
goto err;
if unlikely(cast_sequence(self->a_flag))
Expand All @@ -511,7 +516,7 @@ INTERN WUNUSED NONNULL((1)) int
active_size = 0;
} else {
if (need_all) {
if unlikely(active_size == UINT16_MAX) {
if unlikely(active_size >= UINT16_MAX) {
PERRAST(self, W_ASM_SEQUENCE_TOO_LONG);
goto err;
}
Expand Down
Loading

0 comments on commit 00ca9b9

Please sign in to comment.