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

Hygienic bug in macro (Ok function) #53

Closed
dcecile opened this issue Dec 17, 2023 · 3 comments · Fixed by #54
Closed

Hygienic bug in macro (Ok function) #53

dcecile opened this issue Dec 17, 2023 · 3 comments · Fixed by #54

Comments

@dcecile
Copy link

dcecile commented Dec 17, 2023

Similar to #16, but this time self_cell doesn't allow a custom Ok function:

use self_cell::self_cell;

// Remove this, and it compiles
pub fn Ok<T>(t: T) -> Result<T, ()> {
  Result::Ok(t)
}

#[derive(Debug, Eq, PartialEq)]
struct Ast<'a>(pub Vec<&'a str>);

self_cell!(
    struct AstCell {
        owner: String,

        #[covariant]
        dependent: Ast,
    }

    impl {Debug, Eq, PartialEq}
);

fn build_ast_cell(code: &str) -> AstCell {
  // Create owning String on stack.
  let pre_processed_code = code.trim().to_string();

  // Move String into AstCell, then build Ast inplace.
  AstCell::new(pre_processed_code, |code| {
    Ast(code.split(' ').filter(|word| word.len() > 1).collect())
  })
}

fn main() {
  let ast_cell = build_ast_cell("fox = cat + dog");

  println!("ast_cell -> {:?}", &ast_cell);
  println!("ast_cell.borrow_owner() -> {:?}", ast_cell.borrow_owner());
  println!(
    "ast_cell.borrow_dependent().0[1] -> {:?}",
    ast_cell.borrow_dependent().0[1]
  );
}

Compile error:

error[E0308]: mismatched types
  --> svc-gateway-host-run/src/e.rs:11:1
   |
11 | / self_cell!(
12 | |     struct AstCell {
13 | |         owner: String,
14 | |
...  |
19 | |     impl {Debug, Eq, PartialEq}
20 | | );
   | | ^
   | | |
   | | expected `Result<AstCell, Err>`, found `Result<AstCell, ()>`
   | |_expected this type parameter
   |   expected `Result<AstCell, Err>` because of return type
   |
   = note: expected enum `Result<_, Err>`
              found enum `Result<_, ()>`
   = note: this error originates in the macro `self_cell` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0308]: mismatched types
  --> svc-gateway-host-run/src/e.rs:11:1
   |
11 | / self_cell!(
12 | |     struct AstCell {
13 | |         owner: String,
14 | |
...  |
19 | |     impl {Debug, Eq, PartialEq}
20 | | );
   | | ^
   | | |
   | |_expected `Result<AstCell, (..., ...)>`, found `Result<AstCell, ()>`
   |   expected `Result<AstCell, (String, Err)>` because of return type
   |
   = note: expected enum `Result<_, (String, Err)>`
              found enum `Result<_, ()>`
   = note: this error originates in the macro `self_cell` (in Nightly builds, run with -Z macro-backtrace for more info)

Context: was trying out anyhow's Ok helper

@Voultapher
Copy link
Owner

Thanks for bringing up the issue. I've created a PR with a fix for the issue. Please let me know if that addresses your issues.

@Voultapher
Copy link
Owner

I just released https://github.com/Voultapher/self_cell/releases/tag/v1.0.3 which contains the fix.

@dcecile
Copy link
Author

dcecile commented Dec 23, 2023

Thanks!

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

Successfully merging a pull request may close this issue.

2 participants