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

RFC: Allow matching against n-ary variants with just the name #1701

Closed
marijnh opened this issue Jan 29, 2012 · 14 comments
Closed

RFC: Allow matching against n-ary variants with just the name #1701

marijnh opened this issue Jan 29, 2012 · 14 comments
Labels
A-frontend Area: Compiler frontend (errors, parsing and HIR) E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Comments

@marijnh
Copy link
Contributor

marijnh commented Jan 29, 2012

There's lot of stuff like this in the current code:

alt expr.node {
    expr_field(_, _) | expr_index(_, _) { ... }
}

Where we need to have the (_, _, _) in there even though we just want to match a variant, and don't care about the arguments. If I understand correctly, we're already disallowing shadowing such variants in patterns, so it is probably a good idea to also allow matching against them with just their name. I.e.

alt expr.node {
    expr_field | expr_index { ... }
}
@catamorphism
Copy link
Contributor

Haskell does something like Foo{} to mean "match on Foo and I don't care how many arguments it has". The exact syntax doesn't matter, but it seems like distinguishing that case from the case where Foo is nullary might make the implementation a little simpler.

@marijnh
Copy link
Contributor Author

marijnh commented Jan 29, 2012

The implementation is already trivial even without extra syntax -- in fact, I think it'd mostly be a matter of removing the check that currently causes the error.

@brson
Copy link
Contributor

brson commented Jan 30, 2012

I like the idea of making this case easier, but I think using the same syntax as nullary variants could make refactoring very difficult in one situation. Right now every time you change a tag you can lean on the compiler to tell you everything you need to update. Giving both these scenarios the same syntax will mean the compiler can't distinguish between the 'I believe this is a nullary variant' and 'I don't care how many arguments this variant has' cases. So promoting a variant from 0 arguments to some arguments gets you no feedback from the compiler.

@marijnh
Copy link
Contributor Author

marijnh commented Jan 30, 2012

But what is the situation where this might actually introduce a bug? If you
add a new arg to a variant, presumably existing code matching it doesn't
need this value. There are cases where it does, but you can't protect the
user from all refactoring problems (they could also change arg order,
rename something, etc, which is easily more dangerous).

@catamorphism
Copy link
Contributor

What if you change a variant from nullary to unary? It's possible that code might have to treat values constructed with that constructor differently depending on the value of the field.

It's true that we can't protect the user from all refactoring hazards, but to me that doesn't seem like a very good argument for disregarding any particular hazard. Obviously, there are some we'll address and some we won't, but the fact that there'll be some that we won't doesn't provide any help in choosing which ones to address.

@marijnh
Copy link
Contributor Author

marijnh commented Jan 30, 2012

A) Extra syntax is also a cost.

B) The idea to distinguish nullary from n-ary doesn't even solve the (theoretical) problem. You'd still have the same issue adding an argument to a tag that already had arguments—the existing cases that match 'this variant with any arguments' would keep on matching.

But that is exactly the point of this proposal. To reduce needless coupling. If coupling is something you guys consider a good thing, then we're just arguing from completely different standpoints.

@pcwalton
Copy link
Contributor

pcwalton commented Feb 2, 2012

I'd tend to agree with Tim — sometimes coupling is good because it helps refactoring, and sometimes it's a nuisance. Depends on the situation, I think.

I like the idea of having a pattern (maybe foo _, distinct from foo(_) or foo), that means "just match against foo, I don't care how many arguments it has—or even if it's nullary or n-ary".

@marijnh
Copy link
Contributor Author

marijnh commented Feb 3, 2012

How about name* (rather than name _)? I like the way it looks slightly more.

@nikomatsakis
Copy link
Contributor

I think this would be great. I was going to propose the syntax name(*) or name(_*) but I see there are many other proposals... I don't really care but I think @marijnh is right that we need something like this. It's very annoying to refactor otherwise.

@graydon
Copy link
Contributor

graydon commented Feb 15, 2012

foo(...) perhaps?

@nikomatsakis
Copy link
Contributor

I personally like foo(...).

@marijnh
Copy link
Contributor Author

marijnh commented Feb 16, 2012

It is my understanding that ... is already claimed by the macro system, in pretty much every syntactic context.

@catamorphism
Copy link
Contributor

It seems like there is consensus that we should implement this, and it's just down to choosing the syntax. I volunteer to do it -- I favor name(*) as suggested by @nikomatsakis but if you object, comment soon.

@catamorphism
Copy link
Contributor

I've implemented the new syntax, but going through and using it is still a separate task (probably easiest to do it incrementally, or I suppose someone could do an automated search-and-replace).

Dylan-DPC-zz referenced this issue in Dylan-DPC-zz/rust Mar 21, 2021
…chenkov

Move some tests to more reasonable directories - 5

cc rust-lang#73494

Threshold is 0.95. Next time I promise I will take a look into the special/misclassified directories.

- [issues/issue-23208.rs](https://github.com/rust-lang/rust/blob/master/src/test/ui/issues/issue-23208.rs) <sup>[issue](https://github.com/rust-lang/rust/issues/23208)</sup>: associated-types 0.951
- [weird-exprs.rs](https://github.com/rust-lang/rust/blob/master/src/test/ui/weird-exprs.rs) <sup>unknown</sup>: destructuring-assignment 0.958
- [issues/issue-1701.rs](https://github.com/rust-lang/rust/blob/master/src/test/ui/issues/issue-1701.rs) <sup>[issue](https://github.com/rust-lang/rust/issues/1701)</sup>: structs-enums 0.974
- [issues/issue-48508-aux.rs](https://github.com/rust-lang/rust/blob/master/src/test/ui/issues/issue-48508-aux.rs) <sup>[issue](https://github.com/rust-lang/rust/issues/48508)</sup>: numbers-arithmetic 0.991
- [fn_must_use.rs](https://github.com/rust-lang/rust/blob/master/src/test/ui/fn_must_use.rs) <sup>unknown</sup>: lint 1.000
- [mir_check_nonconst.rs](https://github.com/rust-lang/rust/blob/master/src/test/ui/mir_check_nonconst.rs) <sup>unknown</sup>: consts 1.002
- [issues/issue-52060.rs](https://github.com/rust-lang/rust/blob/master/src/test/ui/issues/issue-52060.rs) <sup>[issue](https://github.com/rust-lang/rust/issues/52060)</sup>: consts 1.017
- [issues/issue-45729-unsafe-in-generator.rs](https://github.com/rust-lang/rust/blob/master/src/test/ui/issues/issue-45729-unsafe-in-generator.rs) <sup>[issue](https://github.com/rust-lang/rust/issues/45729)</sup>: generator 1.024
- [issues/issue-10392.rs](https://github.com/rust-lang/rust/blob/master/src/test/ui/issues/issue-10392.rs) <sup>[issue](https://github.com/rust-lang/rust/issues/10392)</sup>: pattern 1.039
- [no-implicit-prelude.rs](https://github.com/rust-lang/rust/blob/master/src/test/ui/no-implicit-prelude.rs) <sup>unknown</sup>: resolve 1.071
- [issues/issue-68000-unicode-ident-after-missing-comma.rs](https://github.com/rust-lang/rust/blob/master/src/test/ui/issues/issue-68000-unicode-ident-after-missing-comma.rs) <sup>[issue](https://github.com/rust-lang/rust/issues/68000)</sup>: parser 1.079
- [shadow.rs](https://github.com/rust-lang/rust/blob/master/src/test/ui/shadow.rs) <sup>unknown</sup>: binding 1.099
- [issues/issue-65611.rs](https://github.com/rust-lang/rust/blob/master/src/test/ui/issues/issue-65611.rs) <sup>[issue](https://github.com/rust-lang/rust/issues/65611)</sup>: consts 1.139
- [concat-rpass.rs](https://github.com/rust-lang/rust/blob/master/src/test/ui/concat-rpass.rs) <sup>unknown</sup>: macros 1.194
- [issues/issue-31597.rs](https://github.com/rust-lang/rust/blob/master/src/test/ui/issues/issue-31597.rs) <sup>[issue](https://github.com/rust-lang/rust/issues/31597)</sup>: associated-types 1.195
- [issues/issue-78372.rs](https://github.com/rust-lang/rust/blob/master/src/test/ui/issues/issue-78372.rs) <sup>[issue](https://github.com/rust-lang/rust/issues/78372)</sup>: resolve 1.426
- [impl-trait-in-bindings-issue-73003.rs](https://github.com/rust-lang/rust/blob/master/src/test/ui/impl-trait-in-bindings-issue-73003.rs) <sup>[issue](https://github.com/rust-lang/rust/issues/73003)</sup>: impl-trait 1.471
- [impl-trait-in-bindings.rs](https://github.com/rust-lang/rust/blob/master/src/test/ui/impl-trait-in-bindings.rs) <sup>unknown</sup>: impl-trait 2.500

r? `@petrochenkov`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-frontend Area: Compiler frontend (errors, parsing and HIR) E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Projects
None yet
Development

No branches or pull requests

6 participants