Skip to content

Improved error message for impl on typedef. #12385

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

Closed
wants to merge 1 commit into from
Closed

Improved error message for impl on typedef. #12385

wants to merge 1 commit into from

Conversation

chromatic
Copy link
Contributor

This addresses GH #9767. The text of the error message can, of course, change to meet reviewer preferences.

item.span,
self_type.ty) {
None => {
// Error already reported from CoherenceChecker's
Copy link
Member

Choose a reason for hiding this comment

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

This branch is only triggered when self_type.ty is a typedef, right? Could the comment be updated to include this information?

@huonw
Copy link
Member

huonw commented Feb 19, 2014

BTW, if you write "Fixes #9767" or "Closes #9767" in the commit and/or pull request, github will automatically close that issue when this lands. (There's very little flexibility for the formatting.)

@chromatic
Copy link
Contributor Author

How about this comment? It's a lot more specific.

@huonw
Copy link
Member

huonw commented Feb 19, 2014

That comment looks good, yes.

I don't understand the code fully, but it seems like get_base_type_def_id returns None for primitive types like int and tuples (since it calls get_base_type), so does this cause impl int { ... } to emit the "cannot provide impl for typedefs" error?

Also, what about something like

struct Foo;
type Bar = Foo;
impl Bar { ... }

where the typedef is pointing to a non-primitive?

@chromatic
Copy link
Contributor Author

Yes, it does--but that case already emitted an error. Only the text of the error message changed in that particular branch.

@@ -276,8 +295,7 @@ impl CoherenceChecker {
None => {
let session = self.crate_context.tcx.sess;
session.span_err(item.span,
"no base type found for inherent implementation; \
implement a trait or new type instead");
"cannot provide impl for typedefs");
Copy link
Member

Choose a reason for hiding this comment

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

Are you sure that this branch only happens for typedefs?

Copy link
Member

Choose a reason for hiding this comment

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

I'm very unfamiliar with this code, so I'm mostly just curious

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not 100% positive. All of the existing tests passed, but that's an argument from code coverage. Where would I look to find other potential test cases?

@huonw
Copy link
Member

huonw commented Feb 19, 2014

@chromatic I'm not quite sure what you mean by "yes" (since I asked two questions).

@chromatic
Copy link
Contributor Author

@huonw, Sorry, that was really misleading of me!

impl int { ... } will trigger the error message as you suggested. As for the second question, when the typedef points to something non primitive, you get the error message "cannot associate methods with a type outside the crate the type is defined in; define and implement a trait or new type instead
". That behavior should be unchanged with or without this patch.

@huonw
Copy link
Member

huonw commented Feb 20, 2014

Impl-ing directly on primitive types like int should not trigger a message that says "typedefs", and typedefs like my second one should. So, unfortunately, it seems that this patch is not the correct way to implement this.

@chromatic
Copy link
Contributor Author

This discussion may have identified multiple issues. This list may not be exhaustive, but it's a start:

  1. What's the preferred error message for impl-ing a typedef?

  2. Should it be possible to impl a primitive type?

2a) If so, is the syntax you gave correct?

2b) If not, what should the error message be? (There's no test for that.)

  1. Should it be possible to impl a typedef pointing to a non-primitive?

3a) If so, is the syntax you gave correct?

3b) If not, what should the error message be? (There's no test for that.)

  1. Are there other cases where impl-ing something is prohibited?

4a) If so, what should those error messages be? (There are no tests for that.)

  1. If the existing code before this PR was in fact incomplete, should this single ticket and PR address all of those cases, or should the additional error cases have their own tickets?

I am very willing to continue addressing this issue, but it seems like these questions need to be answered before the code work can continue.

@huonw
Copy link
Member

huonw commented Feb 22, 2014

Most of these questions aren't really brought up by this issue specifically. The primitive/non-primitive distinction essentially only cropped up as an artifact of the implementation you've given us (because the compiler doesn't seem to provide a quick-and-easy way to identify if a type is a type synonym).

  1. What's the preferred error message for impl-ing a typedef?

"Cannot impl a type synonym"

  1. Should it be possible to impl a primitive type?

Maybe, some loose discussion has happened in a few places (spread across IRC and a few github issues).

2a) If so, is the syntax you gave correct?

Probably.

2b) If not, what should the error message be? (There's no test for that.)

Preferably "Cannot impl directly on a built-in type, use a trait" (or something like that).

  1. Should it be possible to impl a typedef pointing to a non-primitive?

I think type synonyms should be handled uniformly, whether or not they point to primitives.

3a) If so, is the syntax you gave correct?

Why would we change from impl Foo?

3b) If not, what should the error message be? (There's no test for that.)

See 1.

  1. Are there other cases where impl-ing something is prohibited?

A few, but barely related to type synonyms.

4a) If so, what should those error messages be? (There are no tests for that.)

Eh? There are a few tests for invalid impls.

  1. If the existing code before this PR was in fact incomplete, should this single ticket and PR address all of those cases, or should the additional error cases have their own tickets?

This PR is purporting to fix error messages for type synonyms, and so would preferably address every type synonym case (or at least most of them, without regressing other error reporting).

@alexcrichton
Copy link
Member

Closing due to a lack of activity, but feel free to reopen with a rebase!

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

Successfully merging this pull request may close these issues.

3 participants