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

Gccrs gets confused by mutable reference binding in match expressions #3174

Open
1 task
liamnaddell opened this issue Sep 19, 2024 · 0 comments · May be fixed by #3376
Open
1 task

Gccrs gets confused by mutable reference binding in match expressions #3174

liamnaddell opened this issue Sep 19, 2024 · 0 comments · May be fixed by #3376

Comments

@liamnaddell
Copy link
Contributor

Summary

I was trying to take a look at constexpr folding, and came across this bug. This seems to be due to a lack of ref mut support, i.e. #2082 's TODO list.

Rustc compiles this code happily

Reproducer

I tried this code:

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

enum Option {
    Some(i32), None
}

impl Option {
    fn add(&mut self) {
        match *self {
            Option::Some(ref mut a) => {*a += 1},
            Option::None => {},
        }
    }
}

fn main() {
    unsafe {
        let mut a = Option::None;
        a.add();
        let _s = "%d\n\0";
        let _s = _s as *const str;
        let s = _s as *const i8;
        printf(s,a);
    }
}

Does the code make use of any (1.49) nightly feature ?

  • Nightly

Godbolt link

No response

Actual behavior

The current behavior is...

main.rs:12:41: error: expected reference type got i32
   12 |             Option::Some(ref mut a) => {*a += 1},
      |                                         ^
main.rs:12:41: error: cannot apply operator ‘+=’ to types <tyty::error> and <integer>

Expected behavior

Rustc compiles the code

GCC Version

483ee3a

@philberty philberty self-assigned this Jan 10, 2025
@philberty philberty added this to the Remaining typecheck issues milestone Jan 10, 2025
philberty added a commit that referenced this issue Jan 21, 2025
Fixes #3174

gcc/rust/ChangeLog:

	* backend/rust-compile-pattern.cc (CompilePatternBindings::visit): make recursive
	* typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::visit): handle ref flag

gcc/testsuite/ChangeLog:

	* rust/compile/nr2/exclude: nr2 cant handle this
	* rust/compile/issue-3174.rs: New test.

Signed-off-by: Philip Herron <[email protected]>
@philberty philberty linked a pull request Jan 21, 2025 that will close this issue
philberty added a commit that referenced this issue Jan 23, 2025
Fixes #3174

gcc/rust/ChangeLog:

	* backend/rust-compile-pattern.cc (CompilePatternBindings::visit): make recursive
	* typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::visit): handle ref flag

gcc/testsuite/ChangeLog:

	* rust/compile/nr2/exclude: nr2 cant handle this
	* rust/compile/issue-3174.rs: New test.

Signed-off-by: Philip Herron <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Todo
Development

Successfully merging a pull request may close this issue.

2 participants