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

Tweaks to invalid ctor messages #47116

Merged
merged 8 commits into from
Jan 21, 2018
Merged

Conversation

estebank
Copy link
Contributor

@estebank estebank commented Jan 2, 2018

  • Do not suggest using a constructor that isn't accessible
  • Suggest the appropriate syntax (()/{} as appropriate)
  • Add note when trying to use Self as a ctor

CC #22488, fix #47085.

@rust-highfive
Copy link
Collaborator

r? @michaelwoerister

(rust_highfive has picked a reviewer for you, use r? to override)

@kennytm kennytm added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 2, 2018
| ^^^^ did you mean `Self { /* fields */ }`?
| ^^^^ not a function
|
= note: can't instantiate `Self`, you must use the implemented struct directly
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This message would be better if it mentioned the struct S. It'd be even better if it checked that it was a suggestion:

  = help: can't instantiate `Self`, you must use the struct `S` directly:
   |
16 |         let s = S(0, 1); //~ ERROR expected function
   |                 ^

@petrochenkov
Copy link
Contributor

@estebank
Sorry for the delay, I'll review all the PRs in a couple of days.

block));
return (err, candidates);
}
(Def::SelfTy(_, _), _) if ns == ValueNS && is_struct_like(def) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

is_struct_like is always true here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: _, _ -> ..

return (err, candidates);
}
(Def::SelfTy(_, _), _) if ns == ValueNS && is_struct_like(def) => {
err.note("can't instantiate `Self`, you must use the implemented struct \
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think "instantiate" is a correct word here.
Maybe "can't use Self as a constructor"?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also the suggestion to use Self { ... } with braces is now missing (but maybe it's okay).

Copy link
Contributor

Choose a reason for hiding this comment

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

Also suggestions to use Foo { ... } with braces are now missing for type aliases.

err.span_label(span, format!("constructor is not visible \
here due to private fields"));
}
(Def::Struct(def_id), _) if ns == ValueNS && is_struct_like(def) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

is_struct_like is always true here.

return (err, candidates);
}
(Def::VariantCtor(_, ctor_kind), _) if ns == ValueNS && is_struct_like(def) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is equivalent to (Def::VariantCtor(_, CtorKind::Fictive), _) if ns == ValueNS

Def::StructCtor(_, CtorKind::Fn) |
Def::VariantCtor(_, CtorKind::Fn) => "(/* fields */)",
Def::StructCtor(_, CtorKind::Fictive) |
Def::VariantCtor(_, CtorKind::Fictive) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Variants ctors are unreachable here and fictive struct ctor is impossible.

Copy link
Contributor

Choose a reason for hiding this comment

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

Could you also add a test for this suggestion?

Copy link
Contributor Author

@estebank estebank Jan 15, 2018

Choose a reason for hiding this comment

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

So I'm is it correct for me to understand that in this case I should be checking for ctor_def being Def::Struct(..) instead of Def::StructCtor(_, Fictive)?

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 13, 2018
@estebank estebank force-pushed the non-accessible-ctor branch 2 times, most recently from 07181b0 to 27d2559 Compare January 15, 2018 19:19
@estebank estebank added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 15, 2018
- Properly address Variant Ctors
- Show signature if span of trait method without `self` is not available
if is_expected(ctor_def) && !accessible_ctor {
err.span_label(span, format!("constructor is not visible \
here due to private fields"));
} else if accessible_ctor {
Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, now I don't think this condition is reachable at all.
With ns == Value we would find the constructor before Def::Struct if it were really accessible.

path_str));
return (err, candidates);
}
(Def::VariantCtor(_, CtorKind::Fictive), _) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

These three VariantCtor arms look like they give tautological recommendations, I don't think they are ever useful (at least in realistic code).

return (err, candidates);
}
(Def::SelfTy(..), _) if ns == ValueNS => {
err.note("can't use `Self` as a constructor, you must use the \
implemented struct");
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing return (err, candidates);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added.

implemented struct");
}
(Def::TyAlias(_def_id), _) => {
err.note("can't use a type alias as a constructor");
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing return (err, candidates);, missing if ns == ValueNS, unused _def_id.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added.

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 15, 2018
@petrochenkov
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Jan 16, 2018

📌 Commit 282f75a has been approved by petrochenkov

@petrochenkov petrochenkov added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 16, 2018
@estebank
Copy link
Contributor Author

@bors r-, there're some diagnostic output I need to fix by re-adding VariantCtor match arms. Is it ok if I "r" it after doing that or should I change the output to match the current code?

implemented struct");
return (err, candidates);
}
(Def::TyAlias(_), _) if ns == ValueNS => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Def::AssociatedTy(..) can be added here.

err.span_label(span, format!("constructor is not visible \
here due to private fields"));
}
(Def::Struct(def_id), _) if ns == ValueNS => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Def::Union(..), Def::Variant(..) and Def::VariantCtor(_, CtorKind::Fictive) can be added here to restore what the previous code did (they are equivalent to structs without accessible constructors in this context).

Copy link
Contributor

Choose a reason for hiding this comment

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

Only CtorKind::Fictive is relevant here because for others #47116 (comment) still holds.

@petrochenkov
Copy link
Contributor

Some other cases beside VariantCtor are missing too.

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jan 16, 2018
@kennytm
Copy link
Member

kennytm commented Jan 17, 2018

@bors r-

The command in #47116 (comment) is ignored since homu treats r-, as a single word 😒

@petrochenkov
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Jan 20, 2018

📌 Commit 0674050 has been approved by petrochenkov

@petrochenkov petrochenkov added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 20, 2018
@bors
Copy link
Contributor

bors commented Jan 21, 2018

⌛ Testing commit 0674050 with merge 97520cc...

bors added a commit that referenced this pull request Jan 21, 2018
Tweaks to invalid ctor messages

 - Do not suggest using a constructor that isn't accessible
 - Suggest the appropriate syntax (`()`/`{}` as appropriate)
 - Add note when trying to use `Self` as a ctor

CC #22488, fix #47085.
@bors
Copy link
Contributor

bors commented Jan 21, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: petrochenkov
Pushing 97520cc to master...

@bors bors merged commit 0674050 into rust-lang:master Jan 21, 2018
@estebank estebank deleted the non-accessible-ctor branch November 9, 2023 05:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using Self when initializing a tuple struct
6 participants