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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement trait #39

Merged
merged 3 commits into from Oct 23, 2022
Merged

Implement trait #39

merged 3 commits into from Oct 23, 2022

Conversation

Bromeon
Copy link
Collaborator

@Bromeon Bromeon commented Oct 8, 2022

Parses traits, largely reusing the impl implementation. I added code to parse_impl.rs, didn't have a new good name for the file, so I left it for now...

Unfortunately this introduces a breaking change, because existing TyDefinition did not account for : Bounds and optional = initializer. In general, we should probably mark everything #[non_exhaustive] to at least have the option to add fields in non-breaking ways.

But no urgency from my side for any release, just thought I'd implement this while I'm in the flow 馃槈

@Bromeon Bromeon added the enhancement New feature or request label Oct 8, 2022
@PoignardAzur
Copy link
Owner

My first instinct would be to avoid adding #[non_exhaustive] to structs unless we also give them constructors.

@Bromeon
Copy link
Collaborator Author

Bromeon commented Oct 9, 2022

My first instinct would be to avoid adding #[non_exhaustive] to structs unless we also give them constructors.

Good point. I would expect the typical usage is to read those structs (from a parsed result), but there may be use cases where people create them (for testing, or local manipulation).

I removed #[non_exhaustive] from TyDefinition. I guess it's OK to have small breaking API changes every now and then, as we're still in 0.x.

Copy link
Owner

@PoignardAzur PoignardAzur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

src/parse_type.rs Outdated Show resolved Hide resolved
@@ -1289,3 +1289,41 @@ fn parse_mod() {
let mod_decl = parse_declaration_checked(expr);
assert_debug_snapshot!(mod_decl);
}

// ==================
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also add a test for something like:

trait Foo {
    type X = i32;
    const N: i32 = 42;
}

Eg a trait declaration which is semantically invalid but syntactically valid.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we already test those, with SHORT_NAME and TypeWithDefault?

const LONG_NAME: &'static str;
const SHORT_NAME: char = 'T';

type AssocType: Bound;
type TypeWithDefault = Rc<RefCell<MyStruct>>;

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be more expressive to add a test where those are in a trait body, to show the range of accepted syntax?

Eh, it doesn't really matter. The types are orthogonal enough that you can probably assume that if some construct parses in one context, it parses in another.

@PoignardAzur PoignardAzur merged commit 13bd5ea into PoignardAzur:main Oct 23, 2022
@Bromeon Bromeon deleted the feature/trait branch October 23, 2022 10:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants