Comments (11)
Yep, bjorn3 is correct, the “tuple struct expression” is equivalent to parsing it as a function call to a constructor - I did not read the relevant reference page properly. As such, the tuple struct expression could be removed from the AST or could be designed to not be generated in normal parsing (and only exist after conversion from a method call after name resolution or something).
However, there remains one problem related to this - if structs and functions have separate namespaces (i.e. you can define a struct and a function in the same scope with the same name and presumably no shadowing occurs), as suggested here, then how should this constructor vs function ambiguity be dealt with? I’m not sure how rustc does it, and I don’t know if there are more complicated rules to how the syntax should be parsed that we currently don’t know about.
from gccrs.
struct Abc(u8);
fn Abc(_: u8) {}
fn main() {
Abc(0);
}
error[E0428]: the name `Abc` is defined multiple times
--> src/main.rs:3:1
|
1 | struct Abc(u8);
| --------------- previous definition of the value `Abc` here
2 |
3 | fn Abc(_: u8) {}
| ^^^^^^^^^^^^^ `Abc` redefined here
|
= note: `Abc` must be defined only once in the value namespace of this module
I think that reddit post was refering to something like
struct Abc { a: u8 }
fn Abc(_: u8) {}
fn main() {
Abc(0);
}
in which case the struct constructor is not in the value namespace, so there is no ambiguity.
from gccrs.
@philberty I think that's because we haven't done the name resolution
before you start to work on type resolution
. There is a similar issue I encountered in the macro expansion. BTW, it's better to make two files for type resolution
and name resolution
, if our single file keeps growing, my editor will halt each time when saving-and-indenting, and not good to maintain. If you don't mind, I'll rename the current rust-resolution.cc
to rust-type-resolution
, and work on name resolution
. ;-)
from gccrs.
It should be the exact opposite way:
struct Abc(u8);
let foo = Abc(0);
should have Abc(0)
parsed as function call. For tuple structs the constructor is just a regular function. For example let _: fn(u8) -> Abc = Abc;
is allowed.
from gccrs.
I guess its down to the arguments passed to the function call to help determine it in that case but it doesn't handle the case where arguments are the same for the struct and call maybe calls are just given preference over structs is about the only way i can think.
from gccrs.
I just tried this with rustc:
struct test {
x: i32
}
fn test(x: i32) {
}
fn main() {
let x = test(1);
}
and it gives:
warning: type `test` should have an upper camel case name
--> test2.rs:1:8
|
1 | struct test {
| ^^^^ help: convert the identifier to upper camel case: `Test`
|
= note: `#[warn(non_camel_case_types)]` on by default
warning: unused variable: `x`
--> test2.rs:5:9
|
5 | fn test(x: i32) {
| ^ help: consider prefixing with an underscore: `_x`
|
= note: `#[warn(unused_variables)]` on by default
warning: unused variable: `x`
--> test2.rs:10:9
|
10 | let x = test(1);
| ^ help: consider prefixing with an underscore: `_x`
warning: struct is never constructed: `test`
--> test2.rs:1:8
|
1 | struct test {
| ^^^^
|
= note: `#[warn(dead_code)]` on by default
So in this case the function is given preference
from gccrs.
That is because
struct test {
x: i32
}
fn main() {
let x = test(1);
}
is not allowed. Only tuple structs can be used with StructName(...)
. Structs with named fields like test
in this example can't be used this way.
from gccrs.
Good point my bad!
from gccrs.
Yes please @NalaGinrut sorry my bad there on the naming.
from gccrs.
I think this was wrongly closed with the linking from commits
from gccrs.
this is fixed with #26
from gccrs.
Related Issues (20)
- `Deref` and `DerefMut` cause ambiguity issues when resolving HOT 3
- Invalid "duplicate definitions" error in `Cell<T>` implementation
- Impl block after macro expansion triggers macro expansion recursion limit HOT 1
- Unknown reference for resolved name `Self` in impl of `Clone` for `!`
- ICE when implementing `Default` for `()`
- Add `va_list` lang item
- Name resolve `#[fundamental]` attribute properly
- Handle list of const intrinsics properly for 1.49
- Add `discriminant_kind` lang item
- Add `#[rustc_specialization_trait]` attribute HOT 1
- Add `#[rustc_unsafe_specialization_marker]` attribute
- Prevent `#[may_dangle]` attribute from being used in safe impl
- ICE when on invalid-code when construction enum
- Do not link with `-ldl` and `-lpthread` unless necessary
- Handle outer attributes properly for inline assembly
- Improve `-ldl -lpthread` checks in `./configure`
- Safe guard InlineAsm-related structs
- Remove unnecessary #include from rust-expr.h HOT 2
- Fully incorporate tl::expected into InlineAsm parsing
- asm! macro failed to exhaustively parse all of options(), clobber_abis() and register operands
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gccrs.