Skip to content

Syntax #2

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

Open
nrc opened this issue Jul 14, 2021 · 1 comment
Open

Syntax #2

nrc opened this issue Jul 14, 2021 · 1 comment

Comments

@nrc
Copy link
Member

nrc commented Jul 14, 2021

Previous discussion

There is an open question around what syntax (if any) should be required to cause the deref function to be considered in a pattern. From this Zulip discussion the currently preferred option is 'no syntax'. I.e., we extend the match ergonomics rules to apply in any situation where there is a Deref impl, not just in the &T case (to be more precise, where the scrutinee (or a sub-part) has type &T and there is Deref impl for T, then we can match a pattern (or sub-pattern) with type <T as Deref>::Target.

So, the following examples should type check:

fn proposed(a: Option<String>, b: Rc<Box<&Vec<i32>>>) {
    match a {
        Some("hello") => {}
        _ => {}
    }

    match b {
        [1, 2, 3] => {}
        _ => {}
    }
}
@AlbertMarashi
Copy link

AlbertMarashi commented Feb 14, 2022

Would it be possible to have a kind of "path" based pure de-referencing system, so it wouldn't involve arbitrary code execution?

Eg

struct Box<T> {
   pointer: *const T for deref
}
// OR
struct Box<T> {
  pointer: *const T for pattern
}
// OR (this involves no new keywords) & fully backwards compatible
struct Box<T> {
  pointer: *const T for match
}

// and then...

enum Foo {
  A,
  B,
}
let item = Box::new(Foo::A);
match a_box {
  Foo::A => ...,
  Foo::B => ...
}

Can anyone foresee any issues with this kind of syntax?

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

No branches or pull requests

2 participants