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

in which we decline to suggest the anonymous lifetime in declarations #61679

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ pub fn lower_crate(
enum ParamMode {
/// Any path in a type context.
Explicit,
/// Path in a type definition, where the anonymous lifetime `'_` is not allowed.
ExplicitNamed,
/// The `module::Type` in `module::Type::method` in an expression.
Optional,
}
Expand Down Expand Up @@ -1489,6 +1491,23 @@ impl<'a> LoweringContext<'a> {
P(self.lower_ty_direct(t, itctx))
}

fn lower_path_ty(
&mut self,
t: &Ty,
qself: &Option<QSelf>,
path: &Path,
param_mode: ParamMode,
itctx: ImplTraitContext<'_>
) -> hir::Ty {
let id = self.lower_node_id(t.id);
let qpath = self.lower_qpath(t.id, qself, path, param_mode, itctx);
let ty = self.ty_path(id, t.span, qpath);
if let hir::TyKind::TraitObject(..) = ty.node {
self.maybe_lint_bare_trait(t.span, t.id, qself.is_none() && path.is_global());
}
ty
}

fn lower_ty_direct(&mut self, t: &Ty, mut itctx: ImplTraitContext<'_>) -> hir::Ty {
let kind = match t.node {
TyKind::Infer => hir::TyKind::Infer,
Expand Down Expand Up @@ -1534,13 +1553,7 @@ impl<'a> LoweringContext<'a> {
return self.lower_ty_direct(ty, itctx);
}
TyKind::Path(ref qself, ref path) => {
let id = self.lower_node_id(t.id);
let qpath = self.lower_qpath(t.id, qself, path, ParamMode::Explicit, itctx);
let ty = self.ty_path(id, t.span, qpath);
if let hir::TyKind::TraitObject(..) = ty.node {
self.maybe_lint_bare_trait(t.span, t.id, qself.is_none() && path.is_global());
}
return ty;
return self.lower_path_ty(t, qself, path, ParamMode::Explicit, itctx);
}
TyKind::ImplicitSelf => {
let res = self.expect_full_res(t.id);
Expand Down Expand Up @@ -3086,6 +3099,18 @@ impl<'a> LoweringContext<'a> {
}

fn lower_struct_field(&mut self, (index, f): (usize, &StructField)) -> hir::StructField {
let ty = if let TyKind::Path(ref qself, ref path) = f.ty.node {
let t = self.lower_path_ty(
&f.ty,
qself,
path,
ParamMode::ExplicitNamed, // no `'_` in declarations (Issue #61124)
ImplTraitContext::disallowed()
);
P(t)
} else {
self.lower_ty(&f.ty, ImplTraitContext::disallowed())
Copy link
Contributor

Choose a reason for hiding this comment

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

alternatively you could add a ParamMode argument to lower_ty, not sure if that is better though

Copy link
Member Author

Choose a reason for hiding this comment

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

That was my first idea, but I decided against it, because it seemed inelegant to change the signature when most of the TyKind match arm bodies don't concern themselves with ParamMode.

};
hir::StructField {
span: f.span,
hir_id: self.lower_node_id(f.id),
Expand All @@ -3095,7 +3120,7 @@ impl<'a> LoweringContext<'a> {
None => Ident::new(sym::integer(index), f.span),
},
vis: self.lower_visibility(&f.vis, None),
ty: self.lower_ty(&f.ty, ImplTraitContext::disallowed()),
ty,
attrs: self.lower_attrs(&f.attrs),
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![deny(elided_lifetimes_in_paths)]

// Previously, the elided-lifetimes-in-path lint would fire, but we don't want
// that, because `'_` isn't legal in struct declarations.

struct Betrayal<'a> { x: &'a u8 }

struct Heartbreak(Betrayal); //~ ERROR missing lifetime specifier

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0106]: missing lifetime specifier
--> $DIR/issue-61124-anon-lifetime-in-struct-declaration.rs:8:19
|
LL | struct Heartbreak(Betrayal);
| ^^^^^^^^ expected lifetime parameter

error: aborting due to previous error

For more information about this error, try `rustc --explain E0106`.