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

External function from Rust not being called #873

Closed
crides opened this issue Aug 19, 2020 · 6 comments · Fixed by #874 or #891
Closed

External function from Rust not being called #873

crides opened this issue Aug 19, 2020 · 6 comments · Fixed by #874 or #891
Labels

Comments

@crides
Copy link

crides commented Aug 19, 2020

Hi, I'm looking to using Gluon in a system I'm developing, where I'm using Gluon to mutate a single stateful object. I created this simple example to test things out:

#[macro_use] extern crate gluon;
use gluon::{new_vm, vm::ExternModule, ThreadExt, base::error::AsDiagnostic, import::add_extern_module};
use gluon_codegen::*;
use lazy_static::lazy_static;
use std::sync::{Arc, Mutex};

#[derive(Clone, Debug, Trace, VmType, Userdata)]
#[gluon_trace(skip)]
#[gluon(vm_type = "test.Counter")]
struct Counter(Arc<Mutex<usize>>);

impl Counter {
    fn new() -> Counter {
        Counter(Arc::new(Mutex::new(0)))
    }
}

lazy_static! {
    static ref COUNTER: Counter = Counter::new();
}

fn main() {
    let vm = new_vm();
    let load_mod = |thread: &gluon::Thread| {
        thread.register_type::<Counter>("test.Counter", &[])?;
        let module = record! {
            type Counter => Counter,
            get_count => primitive!(0, || *COUNTER.0.lock().unwrap()),
            inc_counter => primitive!(0, || {
                let mut counter = COUNTER.0.lock().unwrap();
                println!("before: {}", *counter);
                *counter += 1;
                println!("after: {}", *counter);
            }),
        };
        ExternModule::new(thread, module)
    };
    add_extern_module(&vm, "counter", load_mod);
    vm.run_io(true);
    let res = vm.run_expr::<()>(
        "test",
        r#"
let { get_count, inc_counter, ? } = import! counter
inc_counter
        "#,
    );
    if let Err(e) = res {
        println!("{}", e.as_diagnostic().message);
    }
    dbg!(*COUNTER.0.lock().unwrap());
}

However, the dbg! at the end gave me 0, and the println!s in the middle are not being printed.

I suspected that I'm not calling the function, so I tried inc_counter () (I can only guess, as there's no documentation on how to call a function with no arugments), but I got the following errors:

error: Expected the following types to be equal
Expected: () -> a
Found: ()
1 errors were found during unification:
Types do not match:
    Expected: () -> a
    Found: ()
- <test>:3:13
  |
3 | inc_counter ()
  |             ^^
  |
- Attempted to call a non-function value

I have no idea what this error means, as inc_counter is for sure defined as a function through primitive!.

So is the inc_counter function being optimized out? If so, how can I include the call back? And where can I find the documentation for it, or around which source files should I look into to understand that behaviour?

Thanks a lot for any pointers and suggestions!

Marwes added a commit to Marwes/gluon that referenced this issue Aug 19, 2020
These cannot be called from the gluon side, instead a one argument
function that takes `()` should be used.

Fixes gluon-lang#873
bors bot added a commit that referenced this issue Aug 19, 2020
874: fix: Prevent zero-argument functions from being created in Rust r=Marwes a=Marwes

These cannot be called from the gluon side, instead a one argument
function that takes `()` should be used.

bors r+

Fixes #873

Co-authored-by: Markus Westerlind <marwes91@gmail.com>
@Marwes
Copy link
Member

Marwes commented Aug 19, 2020

That should have been a compilation error (see #873)

@Marwes Marwes added the bug label Aug 19, 2020
@crides
Copy link
Author

crides commented Aug 19, 2020

I think you meant #874, but yeah I see what you mean. So basically there can be no zero argument functions in Gluon?

Edit: And it works now by making the primitive! call to take 1 argument instead of 0.

@Marwes
Copy link
Member

Marwes commented Aug 19, 2020

Yeah, the implementation still exists since IO actions are implemented as zero argument functions under the hood but I will see if I can't make it so zero argument functions only work for IO actions to prevent this mistake.

(Technically there is only 1-argument functions in gluon, two argument functions are just a 1 argument function that returns a function that takes 1 argument, (+) : Int -> (Int -> Int) )

@crides
Copy link
Author

crides commented Aug 19, 2020

I was testing other things, and while this is not my original problem, it seems to be in the same vein. If I change the imports to be in a record, then the function is only called once.

// ...
    let res = vm.run_expr::<()>(
        "test",
        r#"
let counter @ { ? } = import! test
counter.inc_counter ()
counter.inc_counter ()
        "#,
    );
// ...

In this case, the before & after pair is only shown once, and the counter at the end is 1.

Also, what should I look into if I need to debug further problems like this?

@Marwes
Copy link
Member

Marwes commented Aug 20, 2020

Are you using 0.17.1, this may be an optimizer issue on lower versions.

@crides
Copy link
Author

crides commented Aug 20, 2020

I was using 0.15.0, but I just tested on 0.17.1, and the issue persists.

Marwes added a commit to Marwes/gluon that referenced this issue Sep 13, 2020
These cannot be called from the gluon side, instead a one argument
function that takes `()` should be used.

Fixes gluon-lang#873
Marwes added a commit to Marwes/gluon that referenced this issue Sep 13, 2020
These cannot be called from the gluon side, instead a one argument
function that takes `()` should be used.

Fixes gluon-lang#873
Marwes added a commit to Marwes/gluon that referenced this issue Sep 14, 2020
These cannot be called from the gluon side, instead a one argument
function that takes `()` should be used.

Fixes gluon-lang#873
Marwes added a commit to Marwes/gluon that referenced this issue Sep 14, 2020
These cannot be called from the gluon side, instead a one argument
function that takes `()` should be used.

Fixes gluon-lang#873
bors bot added a commit that referenced this issue Sep 20, 2020
874: fix: Prevent zero-argument functions from being created in Rust r=Marwes a=Marwes

These cannot be called from the gluon side, instead a one argument
function that takes `()` should be used.

bors r+

Fixes #873

Co-authored-by: Markus Westerlind <marwes91@gmail.com>
@bors bors bot closed this as completed in e91ea06 Sep 20, 2020
Marwes added a commit to Marwes/gluon that referenced this issue Oct 25, 2020
<a name="v0.17.2"></a>
### v0.17.2 (2020-10-25)

#### Features

*   Allow the http module to be used without a tcp listener ([c45353d](gluon-lang@c45353d))
*   Format seq expressions without seq ([5c0cec2](gluon-lang@5c0cec2))
*   Compile block expressions as monadic sequences ([bce5973](gluon-lang@bce5973), closes [gluon-lang#884](gluon-lang#884))
* **std:**
  *  add Option assertions to std.test ([28e5053](gluon-lang@28e5053))
  *  add modulo functions to int and float ([92f188a](gluon-lang@92f188a))

#### Bug Fixes

*   Recognize raw string literals without any `#` ([4d66fbb](gluon-lang@4d66fbb), closes [gluon-lang#885](gluon-lang#885))
*   Prevent zero-argument functions from being created in Rust ([e91ea06](gluon-lang@e91ea06), closes [gluon-lang#873](gluon-lang#873))
*   Give tuple fields a span ([2a1c2c7](gluon-lang@2a1c2c7))
*   xor_shift_new inconsistent description ([591b64b](gluon-lang@591b64b))
@Marwes Marwes mentioned this issue Oct 25, 2020
bors bot added a commit that referenced this issue Oct 25, 2020
891: Version 0.17.2 r=Marwes a=Marwes

<a name="v0.17.2"></a>
### v0.17.2 (2020-10-25)

#### Features

*   Allow the http module to be used without a tcp listener ([c45353d](c45353d))
*   Format seq expressions without seq ([5c0cec2](5c0cec2))
*   Compile block expressions as monadic sequences ([bce5973](bce5973), closes [#884](#884))
* **std:**
  *  add Option assertions to std.test ([28e5053](28e5053))
  *  add modulo functions to int and float ([92f188a](92f188a))

#### Bug Fixes

*   Recognize raw string literals without any `#` ([4d66fbb](4d66fbb), closes [#885](#885))
*   Prevent zero-argument functions from being created in Rust ([e91ea06](e91ea06), closes [#873](#873))
*   Give tuple fields a span ([2a1c2c7](2a1c2c7))
*   xor_shift_new inconsistent description ([591b64b](591b64b))

Co-authored-by: Markus Westerlind <marwes91@gmail.com>
Marwes added a commit to Marwes/gluon that referenced this issue Oct 3, 2021
<a name="v0.18.0"></a>
## v0.18.0 (2021-10-03)

#### Bug Fixes

*   Don't use the empty span in derive macros ([d05f1ca](gluon-lang@d05f1ca))
*   Provide the type of imported modules with errors ([d3bfc59](gluon-lang@d3bfc59))
*   Don't refine already refined skolems ([f39b396](gluon-lang@f39b396), closes [gluon-lang#842](gluon-lang#842))
*   Recognize raw string literals without any `#` ([4d66fbb](gluon-lang@4d66fbb), closes [gluon-lang#885](gluon-lang#885))
*   Prevent zero-argument functions from being created in Rust ([e91ea06](gluon-lang@e91ea06), closes [gluon-lang#873](gluon-lang#873))
*   Give tuple fields a span ([2a1c2c7](gluon-lang@2a1c2c7))
*   xor_shift_new inconsistent description ([591b64b](gluon-lang@591b64b))

#### Performance

*   Avoid recreating the vm for each formatted file ([0335733](gluon-lang@0335733))

#### Features

*   Make channels and reference require IO ([c904189](gluon-lang@c904189), breaks [#](https://github.com/gluon-lang/gluon/issues/))
*   Allow specifying type signatures in do bindings ([fac08dc](gluon-lang@fac08dc))
*   Allow macros to refer to symbols in scope at the expansion site ([1a5489c](gluon-lang@1a5489c), closes [gluon-lang#895](gluon-lang#895))
*   Allow the http module to be used without a tcp listener ([c45353d](gluon-lang@c45353d))
*   Format seq expressions without seq ([5c0cec2](gluon-lang@5c0cec2))
*   Compile block expressions as monadic sequences ([bce5973](gluon-lang@bce5973), closes [gluon-lang#884](gluon-lang#884))
* **std:**
  *  add Option assertions to std.test ([28e5053](gluon-lang@28e5053))
  *  add modulo functions to int and float ([92f188a](gluon-lang@92f188a))

#### Breaking Changes

*   Make channels and reference require IO ([c904189](gluon-lang@c904189), breaks [#](https://github.com/gluon-lang/gluon/issues/))
Marwes added a commit to Marwes/gluon that referenced this issue Oct 3, 2021
<a name="v0.18.0"></a>
## v0.18.0 (2021-10-03)

#### Performance

*   Avoid recreating the vm for each formatted file ([0335733](gluon-lang@0335733))

#### Breaking Changes

*   Make channels and reference require IO ([c904189](gluon-lang@c904189), breaks [#](https://github.com/gluon-lang/gluon/issues/))

#### Features

*   Make channels and reference require IO ([c904189](gluon-lang@c904189), breaks [#](https://github.com/gluon-lang/gluon/issues/))
*   Allow specifying type signatures in do bindings ([fac08dc](gluon-lang@fac08dc))
*   Allow macros to refer to symbols in scope at the expansion site ([1a5489c](gluon-lang@1a5489c), closes [gluon-lang#895](gluon-lang#895))
*   Allow the http module to be used without a tcp listener ([c45353d](gluon-lang@c45353d))
*   Format seq expressions without seq ([5c0cec2](gluon-lang@5c0cec2))
*   Compile block expressions as monadic sequences ([bce5973](gluon-lang@bce5973), closes [gluon-lang#884](gluon-lang#884))
* **std:**
  *  add Option assertions to std.test ([28e5053](gluon-lang@28e5053))
  *  add modulo functions to int and float ([92f188a](gluon-lang@92f188a))

#### Bug Fixes

*   Allow the repl to compile concurrently ([2118f4d](gluon-lang@2118f4d))
*   Don't use the empty span in derive macros ([d05f1ca](gluon-lang@d05f1ca))
*   Provide the type of imported modules with errors ([d3bfc59](gluon-lang@d3bfc59))
*   Don't refine already refined skolems ([f39b396](gluon-lang@f39b396), closes [gluon-lang#842](gluon-lang#842))
*   Recognize raw string literals without any `#` ([4d66fbb](gluon-lang@4d66fbb), closes [gluon-lang#885](gluon-lang#885))
*   Prevent zero-argument functions from being created in Rust ([e91ea06](gluon-lang@e91ea06), closes [gluon-lang#873](gluon-lang#873))
*   Give tuple fields a span ([2a1c2c7](gluon-lang@2a1c2c7))
*   xor_shift_new inconsistent description ([591b64b](gluon-lang@591b64b))
bors bot added a commit that referenced this issue Oct 3, 2021
915: Version 0.18.0 r=Marwes a=Marwes

<a name="v0.18.0"></a>
## v0.18.0 (2021-10-03)

#### Performance

*   Avoid recreating the vm for each formatted file ([0335733](0335733))

#### Breaking Changes

*   Make channels and reference require IO ([c904189](c904189), breaks [#](https://github.com/gluon-lang/gluon/issues/))

#### Features

*   Make channels and reference require IO ([c904189](c904189), breaks [#](https://github.com/gluon-lang/gluon/issues/))
*   Allow specifying type signatures in do bindings ([fac08dc](fac08dc))
*   Allow macros to refer to symbols in scope at the expansion site ([1a5489c](1a5489c), closes [#895](#895))
*   Allow the http module to be used without a tcp listener ([c45353d](c45353d))
*   Format seq expressions without seq ([5c0cec2](5c0cec2))
*   Compile block expressions as monadic sequences ([bce5973](bce5973), closes [#884](#884))
* **std:**
  *  add Option assertions to std.test ([28e5053](28e5053))
  *  add modulo functions to int and float ([92f188a](92f188a))

#### Bug Fixes

*   Allow the repl to compile concurrently ([2118f4d](2118f4d))
*   Don't use the empty span in derive macros ([d05f1ca](d05f1ca))
*   Provide the type of imported modules with errors ([d3bfc59](d3bfc59))
*   Don't refine already refined skolems ([f39b396](f39b396), closes [#842](#842))
*   Recognize raw string literals without any `#` ([4d66fbb](4d66fbb), closes [#885](#885))
*   Prevent zero-argument functions from being created in Rust ([e91ea06](e91ea06), closes [#873](#873))
*   Give tuple fields a span ([2a1c2c7](2a1c2c7))
*   xor_shift_new inconsistent description ([591b64b](591b64b))

Co-authored-by: Markus Westerlind <marwes91@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants