Skip to content

Commit 249df3b

Browse files
authored
Merge pull request #624 from dtolnay/identlife
Parse lifetimes on Ident type names
2 parents 5a56582 + 679b15d commit 249df3b

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

syntax/check.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::syntax::atom::Atom::{self, *};
22
use crate::syntax::report::Errors;
33
use crate::syntax::{
44
error, ident, trivial, Api, Array, Enum, ExternFn, ExternType, Impl, Lang, Receiver, Ref,
5-
Signature, SliceRef, Struct, Trait, Ty1, Type, TypeAlias, Types,
5+
RustName, Signature, SliceRef, Struct, Trait, Ty1, Type, TypeAlias, Types,
66
};
77
use proc_macro2::{Delimiter, Group, Ident, TokenStream};
88
use quote::{quote, ToTokens};
@@ -27,7 +27,7 @@ fn do_typecheck(cx: &mut Check) {
2727

2828
for ty in cx.types {
2929
match ty {
30-
Type::Ident(ident) => check_type_ident(cx, &ident.rust),
30+
Type::Ident(ident) => check_type_ident(cx, ident),
3131
Type::RustBox(ptr) => check_type_box(cx, ptr),
3232
Type::RustVec(ty) => check_type_rust_vec(cx, ty),
3333
Type::UniquePtr(ptr) => check_type_unique_ptr(cx, ptr),
@@ -61,7 +61,8 @@ impl Check<'_> {
6161
}
6262
}
6363

64-
fn check_type_ident(cx: &mut Check, ident: &Ident) {
64+
fn check_type_ident(cx: &mut Check, name: &RustName) {
65+
let ident = &name.rust;
6566
if Atom::from(ident).is_none()
6667
&& !cx.types.structs.contains_key(ident)
6768
&& !cx.types.enums.contains_key(ident)
@@ -70,6 +71,11 @@ fn check_type_ident(cx: &mut Check, ident: &Ident) {
7071
{
7172
let msg = format!("unsupported type: {}", ident);
7273
cx.error(ident, &msg);
74+
return;
75+
}
76+
77+
if !name.generics.lifetimes.is_empty() {
78+
cx.error(name, "type with lifetime parameter is not supported yet");
7379
}
7480
}
7581

syntax/parse.rs

+25
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,31 @@ fn parse_type_path(ty: &TypePath) -> Result<Type> {
10561056
return Ok(Type::Ref(inner));
10571057
}
10581058
}
1059+
} else {
1060+
let mut lifetimes = Punctuated::new();
1061+
let mut only_lifetimes = true;
1062+
for pair in generic.args.pairs() {
1063+
let (param, punct) = pair.into_tuple();
1064+
if let GenericArgument::Lifetime(param) = param {
1065+
lifetimes.push_value(param.clone());
1066+
if let Some(punct) = punct {
1067+
lifetimes.push_punct(*punct);
1068+
}
1069+
} else {
1070+
only_lifetimes = false;
1071+
break;
1072+
}
1073+
}
1074+
if only_lifetimes {
1075+
return Ok(Type::Ident(RustName {
1076+
rust: ident,
1077+
generics: Lifetimes {
1078+
lt_token: Some(generic.lt_token),
1079+
lifetimes,
1080+
gt_token: Some(generic.gt_token),
1081+
},
1082+
}));
1083+
}
10591084
}
10601085
}
10611086
PathArguments::Parenthesized(_) => {}

syntax/tokens.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,9 @@ impl ToTokens for Signature {
256256

257257
impl ToTokens for RustName {
258258
fn to_tokens(&self, tokens: &mut TokenStream) {
259-
let RustName { rust, generics: _ } = self;
259+
let RustName { rust, generics } = self;
260260
rust.to_tokens(tokens);
261+
generics.to_tokens(tokens);
261262
}
262263
}
263264

0 commit comments

Comments
 (0)