mismatched types

Open
#4,904 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 3 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
50/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
rust
Domain
compilers

Research direction

Run the supplied test.rs reproducer first and confirm the recursive ADT mismatched-type errors. Then trace the compiler's recursive ADT and type-checking handling to determine why references are treated as mismatched types; done means the reproducer compiles without these errors.

Written by the indexing model from the issue text.

Description

typechecking
#![feature(no_core, lang_items)]
#![no_core]

#[lang = "sized"]
pub trait Sized {}

extern "C" {
    fn printf(format: *const i8, ...);
}

enum ast<'a> {
    num(usize),
    add(&'a ast<'a>, &'a ast<'a>),
}

fn eval(node: &ast) -> usize {
    match node {
        ast::num(n) => *n,
        ast::add(l, r) => eval(l) + eval(r),
    }
}

fn main() -> i32 {
    let a = ast::num(3);
    let b = ast::num(4);
    let c = ast::add(&a, &b);
    let d = ast::num(10);
    let e = ast::add(&c, &d);
    unsafe {
        printf("%d\n\0" as *const str as *const i8, eval(&e) as i32);
    }
    0
}

with my recusive adts work this fails with:

test.rs:31:5: error: mismatched types, expected ‘& ast’ but got ‘ast’ [E0308]
   31 |     match node {
      |     ^~~~~ ~~~~
   32 |         ast::num(n) => *n,
      |         ~~~
test.rs:31:5: error: mismatched types, expected ‘& ast’ but got ‘ast’ [E0308]
   31 |     match node {
      |     ^~~~~ ~~~~
   32 |         ast::num(n) => *n,
   33 |         ast::add(l, r) => eval(l) + eval(r),
      |         ~~~
test.rs:40:22: error: mismatched types, expected ‘ast’ but got ‘ast’ [E0308]
   27 |     add(&'a ast<'a>, &'a ast<'a>),
      |             ~~~       
......
   40 |     let c = ast::add(&a, &b);
      |                      ^~
test.rs:42:23: error: cannot find value ‘c’ in this scope [E0425]
   42 |     let e = ast::add(&c, &d);
      |                       ^
test.rs:42:22: error: failed to resolve argument type
   42 |     let e = ast::add(&c, &d);
      |                      ^
test.rs:44:60: error: cannot find value ‘e’ in this scope [E0425]
   44 |         printf("%d\n\0" as *const str as *const i8, eval(&e) as i32);
      |                                                            ^
Dominant language
C++
Stars
3k
Forks
238
Avg merge
4d 8h
Merged PRs (30d)
31

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rust-GCC/gccrs

All issues in Rust-GCC/gccrs

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.