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

Allow eliding GATs in expression position #92918

Merged
merged 1 commit into from
Jan 31, 2022
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
2 changes: 1 addition & 1 deletion compiler/rustc_typeck/src/astconv/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
let named_type_param_count =
param_counts.types - has_self as usize - synth_type_param_count;
let infer_lifetimes =
gen_pos != GenericArgPosition::Type && !gen_args.has_lifetime_params();
(gen_pos != GenericArgPosition::Type || infer_args) && !gen_args.has_lifetime_params();

if gen_pos != GenericArgPosition::Type && !gen_args.bindings.is_empty() {
Self::prohibit_assoc_ty_binding(tcx, gen_args.bindings[0].span);
Expand Down
15 changes: 14 additions & 1 deletion compiler/rustc_typeck/src/astconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,20 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
) -> subst::GenericArg<'tcx> {
let tcx = self.astconv.tcx();
match param.kind {
GenericParamDefKind::Lifetime => tcx.lifetimes.re_static.into(),
GenericParamDefKind::Lifetime => self
.astconv
.re_infer(Some(param), self.span)
.unwrap_or_else(|| {
debug!(?param, "unelided lifetime in signature");

// This indicates an illegal lifetime in a non-assoc-trait position
tcx.sess.delay_span_bug(self.span, "unelided lifetime in signature");

// Supply some dummy value. We don't have an
Copy link
Member Author

Choose a reason for hiding this comment

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

copy-pasta'ed this from elsewhere in astconv.

// `re_error`, annoyingly, so use `'static`.
tcx.lifetimes.re_static
})
.into(),
GenericParamDefKind::Type { has_default, .. } => {
if !infer_args && has_default {
// No type parameter provided, but a default exists.
Expand Down
38 changes: 38 additions & 0 deletions src/test/ui/generic-associated-types/elided-in-expr-position.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#![feature(generic_associated_types)]
#![allow(unused)]

pub trait Trait {
type Assoc<'a> where Self: 'a;

fn f(&self) -> Self::Assoc<'_>;

// Disallow elision in return position, for now
fn g(&self) -> Self::Assoc;
//~^ ERROR missing generics for associated type `Trait::Assoc`
}

pub struct Struct {
item: f32
}

pub struct GenericStruct<'a> {
ref_item: &'a f32
}

impl Trait for Struct {
type Assoc<'a> = GenericStruct<'a>;

fn f(&self) -> Self::Assoc<'_> {
Self::Assoc {
ref_item: &self.item
}
}

// Disallow elision in return position, for now
fn g(&self) -> Self::Assoc {
//~^ ERROR missing generics for associated type `Trait::Assoc`
todo!()
}
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
error[E0107]: missing generics for associated type `Trait::Assoc`
--> $DIR/elided-in-expr-position.rs:10:26
|
LL | fn g(&self) -> Self::Assoc;
| ^^^^^ expected 1 lifetime argument
|
note: associated type defined here, with 1 lifetime parameter: `'a`
--> $DIR/elided-in-expr-position.rs:5:10
|
LL | type Assoc<'a> where Self: 'a;
| ^^^^^ --
help: add missing lifetime argument
|
LL | fn g(&self) -> Self::Assoc<'_>;
| ~~~~~~~~~

error[E0107]: missing generics for associated type `Trait::Assoc`
--> $DIR/elided-in-expr-position.rs:32:26
|
LL | fn g(&self) -> Self::Assoc {
| ^^^^^ expected 1 lifetime argument
|
note: associated type defined here, with 1 lifetime parameter: `'a`
--> $DIR/elided-in-expr-position.rs:5:10
|
LL | type Assoc<'a> where Self: 'a;
| ^^^^^ --
help: add missing lifetime argument
|
LL | fn g(&self) -> Self::Assoc<'_> {
| ~~~~~~~~~

error: aborting due to 2 previous errors

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