-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
9040c5d
to
957d6ef
Compare
957d6ef
to
35ea817
Compare
| ^^^^ did you mean `Self { /* fields */ }`? | ||
| ^^^^ not a function | ||
| | ||
= note: can't instantiate `Self`, you must use the implemented struct directly |
There was a problem hiding this comment.
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
| ^
@estebank |
src/librustc_resolve/lib.rs
Outdated
block)); | ||
return (err, candidates); | ||
} | ||
(Def::SelfTy(_, _), _) if ns == ValueNS && is_struct_like(def) => { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: _, _
-> ..
src/librustc_resolve/lib.rs
Outdated
return (err, candidates); | ||
} | ||
(Def::SelfTy(_, _), _) if ns == ValueNS && is_struct_like(def) => { | ||
err.note("can't instantiate `Self`, you must use the implemented struct \ |
There was a problem hiding this comment.
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"?
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
src/librustc_resolve/lib.rs
Outdated
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) => { |
There was a problem hiding this comment.
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.
src/librustc_resolve/lib.rs
Outdated
return (err, candidates); | ||
} | ||
(Def::VariantCtor(_, ctor_kind), _) if ns == ValueNS && is_struct_like(def) => { |
There was a problem hiding this comment.
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
src/librustc_resolve/lib.rs
Outdated
Def::StructCtor(_, CtorKind::Fn) | | ||
Def::VariantCtor(_, CtorKind::Fn) => "(/* fields */)", | ||
Def::StructCtor(_, CtorKind::Fictive) | | ||
Def::VariantCtor(_, CtorKind::Fictive) => { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)
?
07181b0
to
27d2559
Compare
- Properly address Variant Ctors - Show signature if span of trait method without `self` is not available
27d2559
to
4a76916
Compare
src/librustc_resolve/lib.rs
Outdated
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 { |
There was a problem hiding this comment.
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.
src/librustc_resolve/lib.rs
Outdated
path_str)); | ||
return (err, candidates); | ||
} | ||
(Def::VariantCtor(_, CtorKind::Fictive), _) => { |
There was a problem hiding this comment.
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).
src/librustc_resolve/lib.rs
Outdated
return (err, candidates); | ||
} | ||
(Def::SelfTy(..), _) if ns == ValueNS => { | ||
err.note("can't use `Self` as a constructor, you must use the \ | ||
implemented struct"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing return (err, candidates);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added.
src/librustc_resolve/lib.rs
Outdated
implemented struct"); | ||
} | ||
(Def::TyAlias(_def_id), _) => { | ||
err.note("can't use a type alias as a constructor"); |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added.
40cf9dc
to
282f75a
Compare
@bors r+ |
📌 Commit 282f75a has been approved by |
@bors r-, there're some diagnostic output I need to fix by re-adding |
src/librustc_resolve/lib.rs
Outdated
implemented struct"); | ||
return (err, candidates); | ||
} | ||
(Def::TyAlias(_), _) if ns == ValueNS => { |
There was a problem hiding this comment.
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 => { |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
Some other cases beside |
@bors r- The command in #47116 (comment) is ignored since homu treats |
@bors r+ |
📌 Commit 0674050 has been approved by |
☀️ Test successful - status-appveyor, status-travis |
()
/{}
as appropriate)Self
as a ctorCC #22488, fix #47085.