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

TREE_ADDRESSABLE is not being set when required #804

Closed
philberty opened this issue Nov 15, 2021 · 0 comments · Fixed by #814
Closed

TREE_ADDRESSABLE is not being set when required #804

philberty opened this issue Nov 15, 2021 · 0 comments · Fixed by #814
Assignees
Labels

Comments

@philberty
Copy link
Member

I tried this code:

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

#[lang = "add_assign"]
pub trait AddAssign<Rhs = Self> {
    fn add_assign(&mut self, rhs: Rhs);
}

impl AddAssign for i32 {
    fn add_assign(&mut self, other: i32) {
        unsafe {
            let a = "add_assign\n\0";
            let b = a as *const str;
            let c = b as *const i8;

            printf(c);
        }
        *self += other
    }
}

fn main() -> i32 {
    let mut a = 1;
    a += 2;

    0
}

I expected to see this happen: The address is taken on the VAR_DECL not an implicit temporary

Instead, this happened: TREE_ADDRESSABLE is false on 'a' and is required to stop the implicit copy

void <i32 as AddAssign>::add_assign (i32 & self, const i32 other)
{
  {
    unsigned char * const & const a;
    unsigned char * const * const b;
    const i8 * const c;

    a = "add_assign\n";
    b = a;
    c = b;
    printf (c);
  }
  _1 = *self;
  _2 = other + _1;
  *self = _2;
}


i32 main ()
{
  i32 a.1;
  i32 D.86;
  i32 a;

  a = 1;
  a.1 = a; // this is wrong
  <i32 as AddAssign>::add_assign (&a.1, 2);
  D.86 = 0;
  return D.86;
}

Meta

  • What version of Rust GCC were you using, git sha if possible. This was found during dev of the operator overloading branch but exists on: dcd7585
@philberty philberty added the bug label Nov 15, 2021
@philberty philberty self-assigned this Nov 15, 2021
@philberty philberty added this to To do in Control Flow 2 via automation Nov 15, 2021
@philberty philberty mentioned this issue Nov 15, 2021
51 tasks
@philberty philberty moved this from To do to In progress in Control Flow 2 Nov 15, 2021
philberty added a commit that referenced this issue Nov 23, 2021
@philberty philberty moved this from In progress to Review in progress in Control Flow 2 Nov 23, 2021
@philberty philberty moved this from Review in progress to Reviewer approved in Control Flow 2 Nov 23, 2021
bors bot added a commit that referenced this issue Nov 23, 2021
814: Set TREE_ADDRESSABLE when we need to borrow any expression r=philberty a=philberty

GCC requires VAR_DECL's and PARAM_DECL's to be marked with TREE_ADDRESSABLE
when the declaration will be used in borrow's ('&' getting the address).
This takes into account the implicit addresses when we do autoderef in
method resolution/operator-overloading.

If it is not set we end up in cases like this:

```c
i32 main ()
{
  i32 a.1;
  i32 D.86;
  i32 a;

  a = 1;
  a.1 = a; // this is wrong
  <i32 as AddAssign>::add_assign (&a.1, 2);
  D.86 = 0;
  return D.86;
}
```

You can see GCC will automatically make a copy of the VAR_DECL resulting bad code-generation.

Fixes #804


Co-authored-by: Philip Herron <philip.herron@embecosm.com>
@bors bors bot closed this as completed in 8b36f2b Nov 23, 2021
@bors bors bot closed this as completed in #814 Nov 23, 2021
Control Flow 2 automation moved this from Reviewer approved to Done Nov 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
Development

Successfully merging a pull request may close this issue.

1 participant