Skip to content

Commit

Permalink
Cast array to slice. Fixes #719 (#720)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamphaus authored and elliotchance committed May 7, 2018
1 parent 503b421 commit b227b8f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
15 changes: 14 additions & 1 deletion tests/cast.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ void test_preprocessor()
}

typedef unsigned char pcre_uchar;
typedef unsigned char pcre_uint8;
typedef struct pcre_study_data {
pcre_uint8 start_bits[32];
} pcre_study_data;

void caststr() {
pcre_uchar str[] = "abcd";
Expand Down Expand Up @@ -131,7 +135,7 @@ void cast_pointer_diff(pcre_uchar *str, int *x) {

int main()
{
plan(38);
plan(39);

START_TEST(cast);
START_TEST(castbool);
Expand Down Expand Up @@ -238,6 +242,15 @@ int main()
cast_pointer_diff(&s[0], &b);
is_eq(b, 0);
}
diag("Cast array to slice");
{
pcre_study_data sdata;
sdata.start_bits[1] = 42;
const pcre_study_data *study = &sdata;
const pcre_uint8 *p = 0;
p = study->start_bits;
is_eq(p[1], 42);
}

done_testing();
}
7 changes: 7 additions & 0 deletions types/cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,13 @@ func CastExpr(p *program.Program, expr goast.Expr, cFromType, cToType string) (
}

if fromType == toType {
match := util.GetRegex(`\[(\d+)\]$`).FindStringSubmatch(cFromType)
if strings.HasSuffix(cToType, "*") && len(match) > 0 {
// we need to convert from array to slice
return &goast.SliceExpr{
X: expr,
}, nil
}
return expr, nil
}

Expand Down

0 comments on commit b227b8f

Please sign in to comment.