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

Stored value type does not match pointer operand type #4136

Open
faustinoaq opened this issue Mar 12, 2017 · 8 comments
Open

Stored value type does not match pointer operand type #4136

faustinoaq opened this issue Mar 12, 2017 · 8 comments

Comments

@faustinoaq
Copy link
Contributor

I was playing with fun and found something. I try to show you a minimal example:

fun print_chrray(value : Char[5])
  puts value
end

print_chrray(StaticArray(Char, 5).new('x'))

The above code produce the following message:

Module validation failed: Stored value type does not match pointer operand type!
  store [5 x i32]* %value, [5 x i32]* %value1, !dbg !54
 [5 x i32]
0x53c10e: ??? at ??
0x14e67a0: ??? at ??
0x1422e3d: ??? at ??
0x1cac555: ??? at ??
0x597c79: ??? at ??
0x56374d: main at ??
0x7f2f715c4291: __libc_start_main at ??
0x53b831: ??? at ??

Error: you've found a bug in the Crystal compiler.
Please open an issue, including source code that will allow us to reproduce the bug:
https://github.com/crystal-lang/crystal/issues
@asterite
Copy link
Member

I don't think static arrays can be passed around in C, so maybe this shouldn't even compile. Maybe it's a bug, but it has very low priority.

@faustinoaq
Copy link
Contributor Author

faustinoaq commented Mar 24, 2017

@asterite Yes C don't allow array literal as argument but array pointer

Pass by reference work as in C

fun print_chrray(sarr : Char[5]*)
  puts sarr.value
end

sarr = StaticArray(Char, 5).new('x')

print_chrray(pointerof(sarr)) # => StaticArray['x', 'x', 'x', 'x', 'x']

@bew
Copy link
Contributor

bew commented Mar 24, 2017

What do you mean by passing array literal in C, I got this to work: (C code)

#include <stdio.h>

int func(const int bla[3])
{
  return bla[0];
}

int main()
{
  printf("%d\n", func( (const int[3]){42, 43, 44} )); // => 42
  return (0);
}

Is it what you're trying to do in Crystal @faustinoaq ?

@faustinoaq
Copy link
Contributor Author

@bew You're right. I was confused. My mistake 😅

@refi64
Copy link
Contributor

refi64 commented Mar 24, 2017

@bew Actually, you're just passing a pointer to the first element of the array, because C is C and reasons unknown.

@bew
Copy link
Contributor

bew commented Mar 24, 2017

@kirbyfan64 You're right, and I was confused too, so @faustinoaq you're right 😄

edit:

I think the type syntax in fun is weird, because in C:

void print_chrray(char value[5]);

The argument value has type char*, and in Crystal, you need an extra * to represent the same thing:

fun print_chrray(value : Char[5] *)

It feels weird, because then the crystal type Char[5] has no representation in C (if I understand it well)

@refi64
Copy link
Contributor

refi64 commented Mar 24, 2017

@bew Well...

Actually, C already completely ignores the bound on an array parameter. So, like you already said, value is of type char* in C. Therefore, the equivalent in Crystal is just Char* (no [5]).

@konovod
Copy link
Contributor

konovod commented Mar 24, 2017

Maybe C ignore bounds, but Crystal doesn't, so i like this little bit of additional typesafety in bindings: https://github.com/konovod/monocypher/blob/master/src/monocypher/libmonocypher.cr. Never encountered this issue though, maybe because i wrap all staticarrays in a structs with to_unsafe method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants