Skip to content

Commit d671948

Browse files
Allow eliding GATs in expr position
1 parent ad46af2 commit d671948

File tree

4 files changed

+88
-2
lines changed

4 files changed

+88
-2
lines changed

compiler/rustc_typeck/src/astconv/generics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
445445
let named_type_param_count =
446446
param_counts.types - has_self as usize - synth_type_param_count;
447447
let infer_lifetimes =
448-
gen_pos != GenericArgPosition::Type && !gen_args.has_lifetime_params();
448+
(gen_pos != GenericArgPosition::Type || infer_args) && !gen_args.has_lifetime_params();
449449

450450
if gen_pos != GenericArgPosition::Type && !gen_args.bindings.is_empty() {
451451
Self::prohibit_assoc_ty_binding(tcx, gen_args.bindings[0].span);

compiler/rustc_typeck/src/astconv/mod.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,20 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
482482
) -> subst::GenericArg<'tcx> {
483483
let tcx = self.astconv.tcx();
484484
match param.kind {
485-
GenericParamDefKind::Lifetime => tcx.lifetimes.re_static.into(),
485+
GenericParamDefKind::Lifetime => self
486+
.astconv
487+
.re_infer(Some(param), self.span)
488+
.unwrap_or_else(|| {
489+
debug!(?param, "unelided lifetime in signature");
490+
491+
// This indicates an illegal lifetime in a non-assoc-trait position
492+
tcx.sess.delay_span_bug(self.span, "unelided lifetime in signature");
493+
494+
// Supply some dummy value. We don't have an
495+
// `re_error`, annoyingly, so use `'static`.
496+
tcx.lifetimes.re_static
497+
})
498+
.into(),
486499
GenericParamDefKind::Type { has_default, .. } => {
487500
if !infer_args && has_default {
488501
// No type parameter provided, but a default exists.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#![feature(generic_associated_types)]
2+
#![allow(unused)]
3+
4+
pub trait Trait {
5+
type Assoc<'a> where Self: 'a;
6+
7+
fn f(&self) -> Self::Assoc<'_>;
8+
9+
// Disallow elision in return position, for now
10+
fn g(&self) -> Self::Assoc;
11+
//~^ ERROR missing generics for associated type `Trait::Assoc`
12+
}
13+
14+
pub struct Struct {
15+
item: f32
16+
}
17+
18+
pub struct GenericStruct<'a> {
19+
ref_item: &'a f32
20+
}
21+
22+
impl Trait for Struct {
23+
type Assoc<'a> = GenericStruct<'a>;
24+
25+
fn f(&self) -> Self::Assoc<'_> {
26+
Self::Assoc {
27+
ref_item: &self.item
28+
}
29+
}
30+
31+
// Disallow elision in return position, for now
32+
fn g(&self) -> Self::Assoc {
33+
//~^ ERROR missing generics for associated type `Trait::Assoc`
34+
todo!()
35+
}
36+
}
37+
38+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
error[E0107]: missing generics for associated type `Trait::Assoc`
2+
--> $DIR/elided-in-expr-position.rs:10:26
3+
|
4+
LL | fn g(&self) -> Self::Assoc;
5+
| ^^^^^ expected 1 lifetime argument
6+
|
7+
note: associated type defined here, with 1 lifetime parameter: `'a`
8+
--> $DIR/elided-in-expr-position.rs:5:10
9+
|
10+
LL | type Assoc<'a> where Self: 'a;
11+
| ^^^^^ --
12+
help: add missing lifetime argument
13+
|
14+
LL | fn g(&self) -> Self::Assoc<'_>;
15+
| ~~~~~~~~~
16+
17+
error[E0107]: missing generics for associated type `Trait::Assoc`
18+
--> $DIR/elided-in-expr-position.rs:32:26
19+
|
20+
LL | fn g(&self) -> Self::Assoc {
21+
| ^^^^^ expected 1 lifetime argument
22+
|
23+
note: associated type defined here, with 1 lifetime parameter: `'a`
24+
--> $DIR/elided-in-expr-position.rs:5:10
25+
|
26+
LL | type Assoc<'a> where Self: 'a;
27+
| ^^^^^ --
28+
help: add missing lifetime argument
29+
|
30+
LL | fn g(&self) -> Self::Assoc<'_> {
31+
| ~~~~~~~~~
32+
33+
error: aborting due to 2 previous errors
34+
35+
For more information about this error, try `rustc --explain E0107`.

0 commit comments

Comments
 (0)