aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_def/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_def/src')
-rw-r--r--crates/hir_def/src/attr.rs8
-rw-r--r--crates/hir_def/src/builtin_attr.rs430
-rw-r--r--crates/hir_def/src/generics.rs69
-rw-r--r--crates/hir_def/src/item_tree.rs7
-rw-r--r--crates/hir_def/src/item_tree/lower.rs20
-rw-r--r--crates/hir_def/src/lib.rs8
-rw-r--r--crates/hir_def/src/path.rs4
-rw-r--r--crates/hir_def/src/path/lower.rs12
-rw-r--r--crates/hir_def/src/type_ref.rs50
9 files changed, 557 insertions, 51 deletions
diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs
index 228d706db..c64b78445 100644
--- a/crates/hir_def/src/attr.rs
+++ b/crates/hir_def/src/attr.rs
@@ -23,7 +23,7 @@ use crate::{
23}; 23};
24 24
25/// Holds documentation 25/// Holds documentation
26#[derive(Debug, Clone, PartialEq, Eq)] 26#[derive(Debug, Clone, PartialEq, Eq, Hash)]
27pub struct Documentation(String); 27pub struct Documentation(String);
28 28
29impl Documentation { 29impl Documentation {
@@ -32,9 +32,9 @@ impl Documentation {
32 } 32 }
33} 33}
34 34
35impl Into<String> for Documentation { 35impl From<Documentation> for String {
36 fn into(self) -> String { 36 fn from(Documentation(string): Documentation) -> Self {
37 self.0 37 string
38 } 38 }
39} 39}
40 40
diff --git a/crates/hir_def/src/builtin_attr.rs b/crates/hir_def/src/builtin_attr.rs
new file mode 100644
index 000000000..2e989c504
--- /dev/null
+++ b/crates/hir_def/src/builtin_attr.rs
@@ -0,0 +1,430 @@
1//! Builtin attributes resolved by nameres.
2//!
3//! The actual definitions were copied from rustc's `compiler/rustc_feature/src/builtin_attrs.rs`.
4//!
5//! It was last synchronized with upstream commit 2225ee1b62ff089917434aefd9b2bf509cfa087f.
6//!
7//! The macros were adjusted to only expand to the attribute name, since that is all we need to do
8//! name resolution, and `BUILTIN_ATTRIBUTES` is almost entirely unchanged from the original, to
9//! ease updating.
10
11/// Ignored attribute namespaces used by tools.
12pub const TOOL_MODULES: &[&str] = &["rustfmt", "clippy"];
13
14type BuiltinAttribute = &'static str;
15
16macro_rules! ungated {
17 ($attr:ident, $typ:expr, $tpl:expr $(,)?) => {
18 stringify!($attr)
19 };
20}
21
22macro_rules! gated {
23 ($attr:ident $($rest:tt)*) => {
24 stringify!($attr)
25 };
26}
27
28macro_rules! rustc_attr {
29 (TEST, $attr:ident $($rest:tt)*) => {
30 stringify!($attr)
31 };
32 ($attr:ident $($rest:tt)*) => {
33 stringify!($attr)
34 };
35}
36
37/// Attributes that have a special meaning to rustc or rustdoc.
38#[rustfmt::skip]
39pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
40 // ==========================================================================
41 // Stable attributes:
42 // ==========================================================================
43
44 // Conditional compilation:
45 ungated!(cfg, Normal, template!(List: "predicate")),
46 ungated!(cfg_attr, Normal, template!(List: "predicate, attr1, attr2, ...")),
47
48 // Testing:
49 ungated!(ignore, Normal, template!(Word, NameValueStr: "reason")),
50 ungated!(
51 should_panic, Normal,
52 template!(Word, List: r#"expected = "reason"#, NameValueStr: "reason"),
53 ),
54 // FIXME(Centril): This can be used on stable but shouldn't.
55 ungated!(reexport_test_harness_main, Normal, template!(NameValueStr: "name")),
56
57 // Macros:
58 ungated!(derive, Normal, template!(List: "Trait1, Trait2, ...")),
59 ungated!(automatically_derived, Normal, template!(Word)),
60 // FIXME(#14407)
61 ungated!(macro_use, Normal, template!(Word, List: "name1, name2, ...")),
62 ungated!(macro_escape, Normal, template!(Word)), // Deprecated synonym for `macro_use`.
63 ungated!(macro_export, Normal, template!(Word, List: "local_inner_macros")),
64 ungated!(proc_macro, Normal, template!(Word)),
65 ungated!(
66 proc_macro_derive, Normal,
67 template!(List: "TraitName, /*opt*/ attributes(name1, name2, ...)"),
68 ),
69 ungated!(proc_macro_attribute, Normal, template!(Word)),
70
71 // Lints:
72 ungated!(warn, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#)),
73 ungated!(allow, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#)),
74 ungated!(forbid, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#)),
75 ungated!(deny, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#)),
76 ungated!(must_use, AssumedUsed, template!(Word, NameValueStr: "reason")),
77 // FIXME(#14407)
78 ungated!(
79 deprecated, Normal,
80 template!(
81 Word,
82 List: r#"/*opt*/ since = "version", /*opt*/ note = "reason""#,
83 NameValueStr: "reason"
84 ),
85 ),
86
87 // Crate properties:
88 ungated!(crate_name, CrateLevel, template!(NameValueStr: "name")),
89 ungated!(crate_type, CrateLevel, template!(NameValueStr: "bin|lib|...")),
90 ungated!(crate_id, CrateLevel, template!(NameValueStr: "ignored")),
91
92 // ABI, linking, symbols, and FFI
93 ungated!(
94 link, AssumedUsed,
95 template!(List: r#"name = "...", /*opt*/ kind = "dylib|static|...", /*opt*/ wasm_import_module = "...""#),
96 ),
97 ungated!(link_name, AssumedUsed, template!(NameValueStr: "name")),
98 ungated!(no_link, Normal, template!(Word)),
99 ungated!(repr, Normal, template!(List: "C")),
100 ungated!(export_name, AssumedUsed, template!(NameValueStr: "name")),
101 ungated!(link_section, AssumedUsed, template!(NameValueStr: "name")),
102 ungated!(no_mangle, AssumedUsed, template!(Word)),
103 ungated!(used, AssumedUsed, template!(Word)),
104
105 // Limits:
106 ungated!(recursion_limit, CrateLevel, template!(NameValueStr: "N")),
107 ungated!(type_length_limit, CrateLevel, template!(NameValueStr: "N")),
108 gated!(
109 const_eval_limit, CrateLevel, template!(NameValueStr: "N"), const_eval_limit,
110 experimental!(const_eval_limit)
111 ),
112
113 // Entry point:
114 ungated!(main, Normal, template!(Word)),
115 ungated!(start, Normal, template!(Word)),
116 ungated!(no_start, CrateLevel, template!(Word)),
117 ungated!(no_main, CrateLevel, template!(Word)),
118
119 // Modules, prelude, and resolution:
120 ungated!(path, Normal, template!(NameValueStr: "file")),
121 ungated!(no_std, CrateLevel, template!(Word)),
122 ungated!(no_implicit_prelude, Normal, template!(Word)),
123 ungated!(non_exhaustive, AssumedUsed, template!(Word)),
124
125 // Runtime
126 ungated!(windows_subsystem, AssumedUsed, template!(NameValueStr: "windows|console")),
127 ungated!(panic_handler, Normal, template!(Word)), // RFC 2070
128
129 // Code generation:
130 ungated!(inline, AssumedUsed, template!(Word, List: "always|never")),
131 ungated!(cold, AssumedUsed, template!(Word)),
132 ungated!(no_builtins, AssumedUsed, template!(Word)),
133 ungated!(target_feature, AssumedUsed, template!(List: r#"enable = "name""#)),
134 ungated!(track_caller, AssumedUsed, template!(Word)),
135 gated!(
136 no_sanitize, AssumedUsed,
137 template!(List: "address, memory, thread"),
138 experimental!(no_sanitize)
139 ),
140
141 // FIXME: #14408 assume docs are used since rustdoc looks at them.
142 ungated!(doc, AssumedUsed, template!(List: "hidden|inline|...", NameValueStr: "string")),
143
144 // ==========================================================================
145 // Unstable attributes:
146 // ==========================================================================
147
148 // Linking:
149 gated!(naked, AssumedUsed, template!(Word), naked_functions, experimental!(naked)),
150 gated!(
151 link_args, Normal, template!(NameValueStr: "args"),
152 "the `link_args` attribute is experimental and not portable across platforms, \
153 it is recommended to use `#[link(name = \"foo\")] instead",
154 ),
155 gated!(
156 link_ordinal, AssumedUsed, template!(List: "ordinal"), raw_dylib,
157 experimental!(link_ordinal)
158 ),
159
160 // Plugins:
161 // XXX Modified for use in rust-analyzer
162 gated!(plugin_registrar),
163 gated!(plugin),
164
165 // Testing:
166 gated!(allow_fail, Normal, template!(Word), experimental!(allow_fail)),
167 gated!(
168 test_runner, CrateLevel, template!(List: "path"), custom_test_frameworks,
169 "custom test frameworks are an unstable feature",
170 ),
171 // RFC #1268
172 gated!(marker, Normal, template!(Word), marker_trait_attr, experimental!(marker)),
173 gated!(
174 thread_local, AssumedUsed, template!(Word),
175 "`#[thread_local]` is an experimental feature, and does not currently handle destructors",
176 ),
177 gated!(no_core, CrateLevel, template!(Word), experimental!(no_core)),
178 // RFC 2412
179 gated!(
180 optimize, AssumedUsed, template!(List: "size|speed"), optimize_attribute,
181 experimental!(optimize),
182 ),
183 // RFC 2867
184 gated!(instruction_set, AssumedUsed, template!(List: "set"), isa_attribute, experimental!(instruction_set)),
185
186 gated!(ffi_returns_twice, AssumedUsed, template!(Word), experimental!(ffi_returns_twice)),
187 gated!(ffi_pure, AssumedUsed, template!(Word), experimental!(ffi_pure)),
188 gated!(ffi_const, AssumedUsed, template!(Word), experimental!(ffi_const)),
189 gated!(
190 register_attr, CrateLevel, template!(List: "attr1, attr2, ..."),
191 experimental!(register_attr),
192 ),
193 gated!(
194 register_tool, CrateLevel, template!(List: "tool1, tool2, ..."),
195 experimental!(register_tool),
196 ),
197
198 gated!(cmse_nonsecure_entry, AssumedUsed, template!(Word), experimental!(cmse_nonsecure_entry)),
199
200 // ==========================================================================
201 // Internal attributes: Stability, deprecation, and unsafe:
202 // ==========================================================================
203
204 ungated!(feature, CrateLevel, template!(List: "name1, name1, ...")),
205 // FIXME(#14407) -- only looked at on-demand so we can't
206 // guarantee they'll have already been checked.
207 ungated!(
208 rustc_deprecated, AssumedUsed,
209 template!(List: r#"since = "version", reason = "...""#)
210 ),
211 // FIXME(#14407)
212 ungated!(stable, AssumedUsed, template!(List: r#"feature = "name", since = "version""#)),
213 // FIXME(#14407)
214 ungated!(
215 unstable, AssumedUsed,
216 template!(List: r#"feature = "name", reason = "...", issue = "N""#),
217 ),
218 // FIXME(#14407)
219 ungated!(rustc_const_unstable, AssumedUsed, template!(List: r#"feature = "name""#)),
220 // FIXME(#14407)
221 ungated!(rustc_const_stable, AssumedUsed, template!(List: r#"feature = "name""#)),
222 gated!(
223 allow_internal_unstable, AssumedUsed, template!(Word, List: "feat1, feat2, ..."),
224 "allow_internal_unstable side-steps feature gating and stability checks",
225 ),
226 gated!(
227 rustc_allow_const_fn_unstable, AssumedUsed, template!(Word, List: "feat1, feat2, ..."),
228 "rustc_allow_const_fn_unstable side-steps feature gating and stability checks"
229 ),
230 gated!(
231 allow_internal_unsafe, Normal, template!(Word),
232 "allow_internal_unsafe side-steps the unsafe_code lint",
233 ),
234
235 // ==========================================================================
236 // Internal attributes: Type system related:
237 // ==========================================================================
238
239 gated!(fundamental, AssumedUsed, template!(Word), experimental!(fundamental)),
240 gated!(
241 may_dangle, Normal, template!(Word), dropck_eyepatch,
242 "`may_dangle` has unstable semantics and may be removed in the future",
243 ),
244
245 // ==========================================================================
246 // Internal attributes: Runtime related:
247 // ==========================================================================
248
249 rustc_attr!(rustc_allocator, AssumedUsed, template!(Word), IMPL_DETAIL),
250 rustc_attr!(rustc_allocator_nounwind, AssumedUsed, template!(Word), IMPL_DETAIL),
251 gated!(alloc_error_handler, Normal, template!(Word), experimental!(alloc_error_handler)),
252 gated!(
253 default_lib_allocator, AssumedUsed, template!(Word), allocator_internals,
254 experimental!(default_lib_allocator),
255 ),
256 gated!(
257 needs_allocator, Normal, template!(Word), allocator_internals,
258 experimental!(needs_allocator),
259 ),
260 gated!(panic_runtime, AssumedUsed, template!(Word), experimental!(panic_runtime)),
261 gated!(needs_panic_runtime, AssumedUsed, template!(Word), experimental!(needs_panic_runtime)),
262 gated!(
263 unwind, AssumedUsed, template!(List: "allowed|aborts"), unwind_attributes,
264 experimental!(unwind),
265 ),
266 gated!(
267 compiler_builtins, AssumedUsed, template!(Word),
268 "the `#[compiler_builtins]` attribute is used to identify the `compiler_builtins` crate \
269 which contains compiler-rt intrinsics and will never be stable",
270 ),
271 gated!(
272 profiler_runtime, AssumedUsed, template!(Word),
273 "the `#[profiler_runtime]` attribute is used to identify the `profiler_builtins` crate \
274 which contains the profiler runtime and will never be stable",
275 ),
276
277 // ==========================================================================
278 // Internal attributes, Linkage:
279 // ==========================================================================
280
281 gated!(
282 linkage, AssumedUsed, template!(NameValueStr: "external|internal|..."),
283 "the `linkage` attribute is experimental and not portable across platforms",
284 ),
285 rustc_attr!(rustc_std_internal_symbol, AssumedUsed, template!(Word), INTERNAL_UNSTABLE),
286
287 // ==========================================================================
288 // Internal attributes, Macro related:
289 // ==========================================================================
290
291 rustc_attr!(rustc_builtin_macro, AssumedUsed, template!(Word), IMPL_DETAIL),
292 rustc_attr!(rustc_proc_macro_decls, Normal, template!(Word), INTERNAL_UNSTABLE),
293 rustc_attr!(
294 rustc_macro_transparency, AssumedUsed,
295 template!(NameValueStr: "transparent|semitransparent|opaque"),
296 "used internally for testing macro hygiene",
297 ),
298
299 // ==========================================================================
300 // Internal attributes, Diagnostics related:
301 // ==========================================================================
302
303 rustc_attr!(
304 rustc_on_unimplemented, AssumedUsed,
305 template!(
306 List: r#"/*opt*/ message = "...", /*opt*/ label = "...", /*opt*/ note = "...""#,
307 NameValueStr: "message"
308 ),
309 INTERNAL_UNSTABLE
310 ),
311 // Enumerates "identity-like" conversion methods to suggest on type mismatch.
312 rustc_attr!(rustc_conversion_suggestion, AssumedUsed, template!(Word), INTERNAL_UNSTABLE),
313
314 // ==========================================================================
315 // Internal attributes, Const related:
316 // ==========================================================================
317
318 rustc_attr!(rustc_promotable, AssumedUsed, template!(Word), IMPL_DETAIL),
319 rustc_attr!(rustc_args_required_const, AssumedUsed, template!(List: "N"), INTERNAL_UNSTABLE),
320
321 // ==========================================================================
322 // Internal attributes, Layout related:
323 // ==========================================================================
324
325 rustc_attr!(
326 rustc_layout_scalar_valid_range_start, AssumedUsed, template!(List: "value"),
327 "the `#[rustc_layout_scalar_valid_range_start]` attribute is just used to enable \
328 niche optimizations in libcore and will never be stable",
329 ),
330 rustc_attr!(
331 rustc_layout_scalar_valid_range_end, AssumedUsed, template!(List: "value"),
332 "the `#[rustc_layout_scalar_valid_range_end]` attribute is just used to enable \
333 niche optimizations in libcore and will never be stable",
334 ),
335 rustc_attr!(
336 rustc_nonnull_optimization_guaranteed, AssumedUsed, template!(Word),
337 "the `#[rustc_nonnull_optimization_guaranteed]` attribute is just used to enable \
338 niche optimizations in libcore and will never be stable",
339 ),
340
341 // ==========================================================================
342 // Internal attributes, Misc:
343 // ==========================================================================
344 gated!(
345 lang, Normal, template!(NameValueStr: "name"), lang_items,
346 "language items are subject to change",
347 ),
348 gated!(rustc_diagnostic_item), // XXX modified in rust-analyzer
349 gated!(
350 // Used in resolve:
351 prelude_import, AssumedUsed, template!(Word),
352 "`#[prelude_import]` is for use by rustc only",
353 ),
354 gated!(
355 rustc_paren_sugar, Normal, template!(Word), unboxed_closures,
356 "unboxed_closures are still evolving",
357 ),
358 rustc_attr!(
359 rustc_inherit_overflow_checks, AssumedUsed, template!(Word),
360 "the `#[rustc_inherit_overflow_checks]` attribute is just used to control \
361 overflow checking behavior of several libcore functions that are inlined \
362 across crates and will never be stable",
363 ),
364 rustc_attr!(rustc_reservation_impl, Normal, template!(NameValueStr: "reservation message"),
365 "the `#[rustc_reservation_impl]` attribute is internally used \
366 for reserving for `for<T> From<!> for T` impl"
367 ),
368 rustc_attr!(
369 rustc_test_marker, Normal, template!(Word),
370 "the `#[rustc_test_marker]` attribute is used internally to track tests",
371 ),
372 rustc_attr!(
373 rustc_unsafe_specialization_marker, Normal, template!(Word),
374 "the `#[rustc_unsafe_specialization_marker]` attribute is used to check specializations"
375 ),
376 rustc_attr!(
377 rustc_specialization_trait, Normal, template!(Word),
378 "the `#[rustc_specialization_trait]` attribute is used to check specializations"
379 ),
380
381 // ==========================================================================
382 // Internal attributes, Testing:
383 // ==========================================================================
384
385 rustc_attr!(TEST, rustc_outlives, Normal, template!(Word)),
386 rustc_attr!(TEST, rustc_capture_analysis, Normal, template!(Word)),
387 rustc_attr!(TEST, rustc_variance, Normal, template!(Word)),
388 rustc_attr!(TEST, rustc_layout, Normal, template!(List: "field1, field2, ...")),
389 rustc_attr!(TEST, rustc_regions, Normal, template!(Word)),
390 rustc_attr!(
391 TEST, rustc_error, AssumedUsed,
392 template!(Word, List: "delay_span_bug_from_inside_query")
393 ),
394 rustc_attr!(TEST, rustc_dump_user_substs, AssumedUsed, template!(Word)),
395 rustc_attr!(TEST, rustc_if_this_changed, AssumedUsed, template!(Word, List: "DepNode")),
396 rustc_attr!(TEST, rustc_then_this_would_need, AssumedUsed, template!(List: "DepNode")),
397 rustc_attr!(
398 TEST, rustc_dirty, AssumedUsed,
399 template!(List: r#"cfg = "...", /*opt*/ label = "...", /*opt*/ except = "...""#),
400 ),
401 rustc_attr!(
402 TEST, rustc_clean, AssumedUsed,
403 template!(List: r#"cfg = "...", /*opt*/ label = "...", /*opt*/ except = "...""#),
404 ),
405 rustc_attr!(
406 TEST, rustc_partition_reused, AssumedUsed,
407 template!(List: r#"cfg = "...", module = "...""#),
408 ),
409 rustc_attr!(
410 TEST, rustc_partition_codegened, AssumedUsed,
411 template!(List: r#"cfg = "...", module = "...""#),
412 ),
413 rustc_attr!(
414 TEST, rustc_expected_cgu_reuse, AssumedUsed,
415 template!(List: r#"cfg = "...", module = "...", kind = "...""#),
416 ),
417 rustc_attr!(TEST, rustc_synthetic, AssumedUsed, template!(Word)),
418 rustc_attr!(TEST, rustc_symbol_name, AssumedUsed, template!(Word)),
419 rustc_attr!(TEST, rustc_polymorphize_error, AssumedUsed, template!(Word)),
420 rustc_attr!(TEST, rustc_def_path, AssumedUsed, template!(Word)),
421 rustc_attr!(TEST, rustc_mir, AssumedUsed, template!(List: "arg1, arg2, ...")),
422 rustc_attr!(TEST, rustc_dump_program_clauses, AssumedUsed, template!(Word)),
423 rustc_attr!(TEST, rustc_dump_env_program_clauses, AssumedUsed, template!(Word)),
424 rustc_attr!(TEST, rustc_object_lifetime_default, AssumedUsed, template!(Word)),
425 rustc_attr!(TEST, rustc_dummy, Normal, template!(Word /* doesn't matter*/)),
426 gated!(
427 omit_gdb_pretty_printer_section, AssumedUsed, template!(Word),
428 "the `#[omit_gdb_pretty_printer_section]` attribute is just used for the Rust test suite",
429 ),
430];
diff --git a/crates/hir_def/src/generics.rs b/crates/hir_def/src/generics.rs
index 835fe3fbd..5189c7e9f 100644
--- a/crates/hir_def/src/generics.rs
+++ b/crates/hir_def/src/generics.rs
@@ -21,7 +21,7 @@ use crate::{
21 keys, 21 keys,
22 src::HasChildSource, 22 src::HasChildSource,
23 src::HasSource, 23 src::HasSource,
24 type_ref::{TypeBound, TypeRef}, 24 type_ref::{LifetimeRef, TypeBound, TypeRef},
25 AdtId, GenericDefId, LocalTypeParamId, Lookup, TypeParamId, 25 AdtId, GenericDefId, LocalTypeParamId, Lookup, TypeParamId,
26}; 26};
27 27
@@ -33,6 +33,12 @@ pub struct TypeParamData {
33 pub provenance: TypeParamProvenance, 33 pub provenance: TypeParamProvenance,
34} 34}
35 35
36/// Data about a generic parameter (to a function, struct, impl, ...).
37#[derive(Clone, PartialEq, Eq, Debug)]
38pub struct LifetimeParamData {
39 pub name: Name,
40}
41
36#[derive(Copy, Clone, PartialEq, Eq, Debug)] 42#[derive(Copy, Clone, PartialEq, Eq, Debug)]
37pub enum TypeParamProvenance { 43pub enum TypeParamProvenance {
38 TypeParamList, 44 TypeParamList,
@@ -44,7 +50,7 @@ pub enum TypeParamProvenance {
44#[derive(Clone, PartialEq, Eq, Debug, Default)] 50#[derive(Clone, PartialEq, Eq, Debug, Default)]
45pub struct GenericParams { 51pub struct GenericParams {
46 pub types: Arena<TypeParamData>, 52 pub types: Arena<TypeParamData>,
47 // lifetimes: Arena<LocalLifetimeParamId, LifetimeParamData>, 53 pub lifetimes: Arena<LifetimeParamData>,
48 pub where_predicates: Vec<WherePredicate>, 54 pub where_predicates: Vec<WherePredicate>,
49} 55}
50 56
@@ -53,16 +59,17 @@ pub struct GenericParams {
53/// It might still result in multiple actual predicates though, because of 59/// It might still result in multiple actual predicates though, because of
54/// associated type bindings like `Iterator<Item = u32>`. 60/// associated type bindings like `Iterator<Item = u32>`.
55#[derive(Clone, PartialEq, Eq, Debug)] 61#[derive(Clone, PartialEq, Eq, Debug)]
56pub struct WherePredicate { 62pub enum WherePredicate {
57 pub target: WherePredicateTarget, 63 TypeBound { target: WherePredicateTypeTarget, bound: TypeBound },
58 pub bound: TypeBound, 64 Lifetime { target: LifetimeRef, bound: LifetimeRef },
59} 65}
60 66
61#[derive(Clone, PartialEq, Eq, Debug)] 67#[derive(Clone, PartialEq, Eq, Debug)]
62pub enum WherePredicateTarget { 68pub enum WherePredicateTypeTarget {
63 TypeRef(TypeRef), 69 TypeRef(TypeRef),
64 /// For desugared where predicates that can directly refer to a type param. 70 /// For desugared where predicates that can directly refer to a type param.
65 TypeParam(LocalTypeParamId), 71 TypeParam(LocalTypeParamId),
72 // FIXME: ForLifetime(Vec<LifetimeParamId>, TypeRef)
66} 73}
67 74
68type SourceMap = ArenaMap<LocalTypeParamId, Either<ast::Trait, ast::TypeParam>>; 75type SourceMap = ArenaMap<LocalTypeParamId, Either<ast::Trait, ast::TypeParam>>;
@@ -123,7 +130,7 @@ impl GenericParams {
123 } 130 }
124 131
125 fn new(db: &dyn DefDatabase, def: GenericDefId) -> (GenericParams, InFile<SourceMap>) { 132 fn new(db: &dyn DefDatabase, def: GenericDefId) -> (GenericParams, InFile<SourceMap>) {
126 let mut generics = GenericParams { types: Arena::default(), where_predicates: Vec::new() }; 133 let mut generics = GenericParams::default();
127 let mut sm = ArenaMap::default(); 134 let mut sm = ArenaMap::default();
128 135
129 // FIXME: add `: Sized` bound for everything except for `Self` in traits 136 // FIXME: add `: Sized` bound for everything except for `Self` in traits
@@ -171,7 +178,7 @@ impl GenericParams {
171 // add super traits as bounds on Self 178 // add super traits as bounds on Self
172 // i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar 179 // i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar
173 let self_param = TypeRef::Path(name![Self].into()); 180 let self_param = TypeRef::Path(name![Self].into());
174 generics.fill_bounds(&lower_ctx, &src.value, self_param); 181 generics.fill_bounds(&lower_ctx, &src.value, Either::Left(self_param));
175 182
176 generics.fill(&lower_ctx, &mut sm, &src.value); 183 generics.fill(&lower_ctx, &mut sm, &src.value);
177 src.file_id 184 src.file_id
@@ -218,12 +225,12 @@ impl GenericParams {
218 &mut self, 225 &mut self,
219 lower_ctx: &LowerCtx, 226 lower_ctx: &LowerCtx,
220 node: &dyn ast::TypeBoundsOwner, 227 node: &dyn ast::TypeBoundsOwner,
221 type_ref: TypeRef, 228 target: Either<TypeRef, LifetimeRef>,
222 ) { 229 ) {
223 for bound in 230 for bound in
224 node.type_bound_list().iter().flat_map(|type_bound_list| type_bound_list.bounds()) 231 node.type_bound_list().iter().flat_map(|type_bound_list| type_bound_list.bounds())
225 { 232 {
226 self.add_where_predicate_from_bound(lower_ctx, bound, type_ref.clone()); 233 self.add_where_predicate_from_bound(lower_ctx, bound, target.clone());
227 } 234 }
228 } 235 }
229 236
@@ -246,19 +253,30 @@ impl GenericParams {
246 sm.insert(param_id, Either::Right(type_param.clone())); 253 sm.insert(param_id, Either::Right(type_param.clone()));
247 254
248 let type_ref = TypeRef::Path(name.into()); 255 let type_ref = TypeRef::Path(name.into());
249 self.fill_bounds(&lower_ctx, &type_param, type_ref); 256 self.fill_bounds(&lower_ctx, &type_param, Either::Left(type_ref));
257 }
258 for lifetime_param in params.lifetime_params() {
259 let name = lifetime_param
260 .lifetime_token()
261 .map_or_else(Name::missing, |tok| Name::new_lifetime(&tok));
262 let param = LifetimeParamData { name: name.clone() };
263 let _param_id = self.lifetimes.alloc(param);
264 let lifetime_ref = LifetimeRef::new_name(name);
265 self.fill_bounds(&lower_ctx, &lifetime_param, Either::Right(lifetime_ref));
250 } 266 }
251 } 267 }
252 268
253 fn fill_where_predicates(&mut self, lower_ctx: &LowerCtx, where_clause: ast::WhereClause) { 269 fn fill_where_predicates(&mut self, lower_ctx: &LowerCtx, where_clause: ast::WhereClause) {
254 for pred in where_clause.predicates() { 270 for pred in where_clause.predicates() {
255 let type_ref = match pred.ty() { 271 let target = if let Some(type_ref) = pred.ty() {
256 Some(type_ref) => type_ref, 272 Either::Left(TypeRef::from_ast(lower_ctx, type_ref))
257 None => continue, 273 } else if let Some(lifetime_tok) = pred.lifetime_token() {
274 Either::Right(LifetimeRef::from_token(lifetime_tok))
275 } else {
276 continue;
258 }; 277 };
259 let type_ref = TypeRef::from_ast(lower_ctx, type_ref);
260 for bound in pred.type_bound_list().iter().flat_map(|l| l.bounds()) { 278 for bound in pred.type_bound_list().iter().flat_map(|l| l.bounds()) {
261 self.add_where_predicate_from_bound(lower_ctx, bound, type_ref.clone()); 279 self.add_where_predicate_from_bound(lower_ctx, bound, target.clone());
262 } 280 }
263 } 281 }
264 } 282 }
@@ -267,15 +285,24 @@ impl GenericParams {
267 &mut self, 285 &mut self,
268 lower_ctx: &LowerCtx, 286 lower_ctx: &LowerCtx,
269 bound: ast::TypeBound, 287 bound: ast::TypeBound,
270 type_ref: TypeRef, 288 target: Either<TypeRef, LifetimeRef>,
271 ) { 289 ) {
272 if bound.question_mark_token().is_some() { 290 if bound.question_mark_token().is_some() {
273 // FIXME: remove this bound 291 // FIXME: remove this bound
274 return; 292 return;
275 } 293 }
276 let bound = TypeBound::from_ast(lower_ctx, bound); 294 let bound = TypeBound::from_ast(lower_ctx, bound);
277 self.where_predicates 295 let predicate = match (target, bound) {
278 .push(WherePredicate { target: WherePredicateTarget::TypeRef(type_ref), bound }); 296 (Either::Left(type_ref), bound) => WherePredicate::TypeBound {
297 target: WherePredicateTypeTarget::TypeRef(type_ref),
298 bound,
299 },
300 (Either::Right(lifetime), TypeBound::Lifetime(bound)) => {
301 WherePredicate::Lifetime { target: lifetime, bound }
302 }
303 _ => return,
304 };
305 self.where_predicates.push(predicate);
279 } 306 }
280 307
281 pub(crate) fn fill_implicit_impl_trait_args(&mut self, type_ref: &TypeRef) { 308 pub(crate) fn fill_implicit_impl_trait_args(&mut self, type_ref: &TypeRef) {
@@ -288,8 +315,8 @@ impl GenericParams {
288 }; 315 };
289 let param_id = self.types.alloc(param); 316 let param_id = self.types.alloc(param);
290 for bound in bounds { 317 for bound in bounds {
291 self.where_predicates.push(WherePredicate { 318 self.where_predicates.push(WherePredicate::TypeBound {
292 target: WherePredicateTarget::TypeParam(param_id), 319 target: WherePredicateTypeTarget::TypeParam(param_id),
293 bound: bound.clone(), 320 bound: bound.clone(),
294 }); 321 });
295 } 322 }
diff --git a/crates/hir_def/src/item_tree.rs b/crates/hir_def/src/item_tree.rs
index c017b352d..b08167281 100644
--- a/crates/hir_def/src/item_tree.rs
+++ b/crates/hir_def/src/item_tree.rs
@@ -246,7 +246,10 @@ struct GenericParamsStorage {
246 246
247impl GenericParamsStorage { 247impl GenericParamsStorage {
248 fn alloc(&mut self, params: GenericParams) -> GenericParamsId { 248 fn alloc(&mut self, params: GenericParams) -> GenericParamsId {
249 if params.types.is_empty() && params.where_predicates.is_empty() { 249 if params.types.is_empty()
250 && params.lifetimes.is_empty()
251 && params.where_predicates.is_empty()
252 {
250 return GenericParamsId::EMPTY; 253 return GenericParamsId::EMPTY;
251 } 254 }
252 255
@@ -255,7 +258,7 @@ impl GenericParamsStorage {
255} 258}
256 259
257static EMPTY_GENERICS: GenericParams = 260static EMPTY_GENERICS: GenericParams =
258 GenericParams { types: Arena::new(), where_predicates: Vec::new() }; 261 GenericParams { types: Arena::new(), lifetimes: Arena::new(), where_predicates: Vec::new() };
259 262
260#[derive(Default, Debug, Eq, PartialEq)] 263#[derive(Default, Debug, Eq, PartialEq)]
261struct ItemTreeData { 264struct ItemTreeData {
diff --git a/crates/hir_def/src/item_tree/lower.rs b/crates/hir_def/src/item_tree/lower.rs
index 63b2826f8..f7ce2e26d 100644
--- a/crates/hir_def/src/item_tree/lower.rs
+++ b/crates/hir_def/src/item_tree/lower.rs
@@ -13,6 +13,7 @@ use syntax::{
13use crate::{ 13use crate::{
14 attr::Attrs, 14 attr::Attrs,
15 generics::{GenericParams, TypeParamData, TypeParamProvenance}, 15 generics::{GenericParams, TypeParamData, TypeParamProvenance},
16 type_ref::LifetimeRef,
16}; 17};
17 18
18use super::*; 19use super::*;
@@ -292,12 +293,16 @@ impl Ctx {
292 let self_type = TypeRef::Path(name![Self].into()); 293 let self_type = TypeRef::Path(name![Self].into());
293 match self_param.kind() { 294 match self_param.kind() {
294 ast::SelfParamKind::Owned => self_type, 295 ast::SelfParamKind::Owned => self_type,
295 ast::SelfParamKind::Ref => { 296 ast::SelfParamKind::Ref => TypeRef::Reference(
296 TypeRef::Reference(Box::new(self_type), Mutability::Shared) 297 Box::new(self_type),
297 } 298 self_param.lifetime_token().map(LifetimeRef::from_token),
298 ast::SelfParamKind::MutRef => { 299 Mutability::Shared,
299 TypeRef::Reference(Box::new(self_type), Mutability::Mut) 300 ),
300 } 301 ast::SelfParamKind::MutRef => TypeRef::Reference(
302 Box::new(self_type),
303 self_param.lifetime_token().map(LifetimeRef::from_token),
304 Mutability::Mut,
305 ),
301 } 306 }
302 } 307 }
303 }; 308 };
@@ -629,8 +634,7 @@ impl Ctx {
629 // add super traits as bounds on Self 634 // add super traits as bounds on Self
630 // i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar 635 // i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar
631 let self_param = TypeRef::Path(name![Self].into()); 636 let self_param = TypeRef::Path(name![Self].into());
632 generics.fill_bounds(&self.body_ctx, trait_def, self_param); 637 generics.fill_bounds(&self.body_ctx, trait_def, Either::Left(self_param));
633
634 generics.fill(&self.body_ctx, &mut sm, node); 638 generics.fill(&self.body_ctx, &mut sm, node);
635 } 639 }
636 GenericsOwner::Impl => { 640 GenericsOwner::Impl => {
diff --git a/crates/hir_def/src/lib.rs b/crates/hir_def/src/lib.rs
index 02ed30e4d..7e2199a9c 100644
--- a/crates/hir_def/src/lib.rs
+++ b/crates/hir_def/src/lib.rs
@@ -18,6 +18,7 @@ pub mod attr;
18pub mod path; 18pub mod path;
19pub mod type_ref; 19pub mod type_ref;
20pub mod builtin_type; 20pub mod builtin_type;
21pub mod builtin_attr;
21pub mod diagnostics; 22pub mod diagnostics;
22pub mod per_ns; 23pub mod per_ns;
23pub mod item_scope; 24pub mod item_scope;
@@ -224,6 +225,13 @@ pub struct TypeParamId {
224pub type LocalTypeParamId = Idx<generics::TypeParamData>; 225pub type LocalTypeParamId = Idx<generics::TypeParamData>;
225 226
226#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 227#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
228pub struct LifetimeParamId {
229 pub parent: GenericDefId,
230 pub local_id: LocalLifetimeParamId,
231}
232pub type LocalLifetimeParamId = Idx<generics::LifetimeParamData>;
233
234#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
227pub enum ContainerId { 235pub enum ContainerId {
228 ModuleId(ModuleId), 236 ModuleId(ModuleId),
229 DefWithBodyId(DefWithBodyId), 237 DefWithBodyId(DefWithBodyId),
diff --git a/crates/hir_def/src/path.rs b/crates/hir_def/src/path.rs
index 5b8c1e449..00a69a8a6 100644
--- a/crates/hir_def/src/path.rs
+++ b/crates/hir_def/src/path.rs
@@ -7,7 +7,7 @@ use std::{
7 sync::Arc, 7 sync::Arc,
8}; 8};
9 9
10use crate::body::LowerCtx; 10use crate::{body::LowerCtx, type_ref::LifetimeRef};
11use base_db::CrateId; 11use base_db::CrateId;
12use hir_expand::{ 12use hir_expand::{
13 hygiene::Hygiene, 13 hygiene::Hygiene,
@@ -145,7 +145,7 @@ pub struct AssociatedTypeBinding {
145#[derive(Debug, Clone, PartialEq, Eq, Hash)] 145#[derive(Debug, Clone, PartialEq, Eq, Hash)]
146pub enum GenericArg { 146pub enum GenericArg {
147 Type(TypeRef), 147 Type(TypeRef),
148 // or lifetime... 148 Lifetime(LifetimeRef),
149} 149}
150 150
151impl Path { 151impl Path {
diff --git a/crates/hir_def/src/path/lower.rs b/crates/hir_def/src/path/lower.rs
index 07b9723ce..60fa7646b 100644
--- a/crates/hir_def/src/path/lower.rs
+++ b/crates/hir_def/src/path/lower.rs
@@ -15,7 +15,7 @@ use super::AssociatedTypeBinding;
15use crate::{ 15use crate::{
16 body::LowerCtx, 16 body::LowerCtx,
17 path::{GenericArg, GenericArgs, ModPath, Path, PathKind}, 17 path::{GenericArg, GenericArgs, ModPath, Path, PathKind},
18 type_ref::{TypeBound, TypeRef}, 18 type_ref::{LifetimeRef, TypeBound, TypeRef},
19}; 19};
20 20
21pub(super) use lower_use::lower_use_tree; 21pub(super) use lower_use::lower_use_tree;
@@ -170,8 +170,14 @@ pub(super) fn lower_generic_args(
170 bindings.push(AssociatedTypeBinding { name, type_ref, bounds }); 170 bindings.push(AssociatedTypeBinding { name, type_ref, bounds });
171 } 171 }
172 } 172 }
173 // Lifetimes and constants are ignored for now. 173 ast::GenericArg::LifetimeArg(lifetime_arg) => {
174 ast::GenericArg::LifetimeArg(_) | ast::GenericArg::ConstArg(_) => (), 174 if let Some(lifetime) = lifetime_arg.lifetime_token() {
175 let lifetime_ref = LifetimeRef::from_token(lifetime);
176 args.push(GenericArg::Lifetime(lifetime_ref))
177 }
178 }
179 // constants are ignored for now.
180 ast::GenericArg::ConstArg(_) => (),
175 } 181 }
176 } 182 }
177 183
diff --git a/crates/hir_def/src/type_ref.rs b/crates/hir_def/src/type_ref.rs
index 1a78c1444..347ceabb9 100644
--- a/crates/hir_def/src/type_ref.rs
+++ b/crates/hir_def/src/type_ref.rs
@@ -1,6 +1,7 @@
1//! HIR for references to types. Paths in these are not yet resolved. They can 1//! HIR for references to types. Paths in these are not yet resolved. They can
2//! be directly created from an ast::TypeRef, without further queries. 2//! be directly created from an ast::TypeRef, without further queries.
3use syntax::ast::{self}; 3use hir_expand::name::Name;
4use syntax::{ast, SyntaxToken};
4 5
5use crate::{body::LowerCtx, path::Path}; 6use crate::{body::LowerCtx, path::Path};
6 7
@@ -58,7 +59,7 @@ pub enum TypeRef {
58 Tuple(Vec<TypeRef>), 59 Tuple(Vec<TypeRef>),
59 Path(Path), 60 Path(Path),
60 RawPtr(Box<TypeRef>, Mutability), 61 RawPtr(Box<TypeRef>, Mutability),
61 Reference(Box<TypeRef>, Mutability), 62 Reference(Box<TypeRef>, Option<LifetimeRef>, Mutability),
62 Array(Box<TypeRef> /*, Expr*/), 63 Array(Box<TypeRef> /*, Expr*/),
63 Slice(Box<TypeRef>), 64 Slice(Box<TypeRef>),
64 /// A fn pointer. Last element of the vector is the return type. 65 /// A fn pointer. Last element of the vector is the return type.
@@ -70,10 +71,29 @@ pub enum TypeRef {
70} 71}
71 72
72#[derive(Clone, PartialEq, Eq, Hash, Debug)] 73#[derive(Clone, PartialEq, Eq, Hash, Debug)]
74pub struct LifetimeRef {
75 pub name: Name,
76}
77
78impl LifetimeRef {
79 pub(crate) fn new_name(name: Name) -> Self {
80 LifetimeRef { name }
81 }
82
83 pub(crate) fn from_token(token: SyntaxToken) -> Self {
84 LifetimeRef { name: Name::new_lifetime(&token) }
85 }
86
87 pub fn missing() -> LifetimeRef {
88 LifetimeRef { name: Name::missing() }
89 }
90}
91
92#[derive(Clone, PartialEq, Eq, Hash, Debug)]
73pub enum TypeBound { 93pub enum TypeBound {
74 Path(Path), 94 Path(Path),
75 // also for<> bounds 95 // ForLifetime(Vec<LifetimeRef>, Path), FIXME ForLifetime
76 // also Lifetimes 96 Lifetime(LifetimeRef),
77 Error, 97 Error,
78} 98}
79 99
@@ -107,8 +127,9 @@ impl TypeRef {
107 } 127 }
108 ast::Type::RefType(inner) => { 128 ast::Type::RefType(inner) => {
109 let inner_ty = TypeRef::from_ast_opt(&ctx, inner.ty()); 129 let inner_ty = TypeRef::from_ast_opt(&ctx, inner.ty());
130 let lifetime = inner.lifetime_token().map(|t| LifetimeRef::from_token(t));
110 let mutability = Mutability::from_mutable(inner.mut_token().is_some()); 131 let mutability = Mutability::from_mutable(inner.mut_token().is_some());
111 TypeRef::Reference(Box::new(inner_ty), mutability) 132 TypeRef::Reference(Box::new(inner_ty), lifetime, mutability)
112 } 133 }
113 ast::Type::InferType(_inner) => TypeRef::Placeholder, 134 ast::Type::InferType(_inner) => TypeRef::Placeholder,
114 ast::Type::FnPtrType(inner) => { 135 ast::Type::FnPtrType(inner) => {
@@ -163,14 +184,14 @@ impl TypeRef {
163 types.iter().for_each(|t| go(t, f)) 184 types.iter().for_each(|t| go(t, f))
164 } 185 }
165 TypeRef::RawPtr(type_ref, _) 186 TypeRef::RawPtr(type_ref, _)
166 | TypeRef::Reference(type_ref, _) 187 | TypeRef::Reference(type_ref, ..)
167 | TypeRef::Array(type_ref) 188 | TypeRef::Array(type_ref)
168 | TypeRef::Slice(type_ref) => go(&type_ref, f), 189 | TypeRef::Slice(type_ref) => go(&type_ref, f),
169 TypeRef::ImplTrait(bounds) | TypeRef::DynTrait(bounds) => { 190 TypeRef::ImplTrait(bounds) | TypeRef::DynTrait(bounds) => {
170 for bound in bounds { 191 for bound in bounds {
171 match bound { 192 match bound {
172 TypeBound::Path(path) => go_path(path, f), 193 TypeBound::Path(path) => go_path(path, f),
173 TypeBound::Error => (), 194 TypeBound::Lifetime(_) | TypeBound::Error => (),
174 } 195 }
175 } 196 }
176 } 197 }
@@ -186,8 +207,12 @@ impl TypeRef {
186 for segment in path.segments().iter() { 207 for segment in path.segments().iter() {
187 if let Some(args_and_bindings) = segment.args_and_bindings { 208 if let Some(args_and_bindings) = segment.args_and_bindings {
188 for arg in &args_and_bindings.args { 209 for arg in &args_and_bindings.args {
189 let crate::path::GenericArg::Type(type_ref) = arg; 210 match arg {
190 go(type_ref, f); 211 crate::path::GenericArg::Type(type_ref) => {
212 go(type_ref, f);
213 }
214 crate::path::GenericArg::Lifetime(_) => {}
215 }
191 } 216 }
192 for binding in &args_and_bindings.bindings { 217 for binding in &args_and_bindings.bindings {
193 if let Some(type_ref) = &binding.type_ref { 218 if let Some(type_ref) = &binding.type_ref {
@@ -196,7 +221,7 @@ impl TypeRef {
196 for bound in &binding.bounds { 221 for bound in &binding.bounds {
197 match bound { 222 match bound {
198 TypeBound::Path(path) => go_path(path, f), 223 TypeBound::Path(path) => go_path(path, f),
199 TypeBound::Error => (), 224 TypeBound::Lifetime(_) | TypeBound::Error => (),
200 } 225 }
201 } 226 }
202 } 227 }
@@ -232,7 +257,10 @@ impl TypeBound {
232 }; 257 };
233 TypeBound::Path(path) 258 TypeBound::Path(path)
234 } 259 }
235 ast::TypeBoundKind::ForType(_) | ast::TypeBoundKind::Lifetime(_) => TypeBound::Error, 260 ast::TypeBoundKind::ForType(_) => TypeBound::Error, // FIXME ForType
261 ast::TypeBoundKind::Lifetime(lifetime) => {
262 TypeBound::Lifetime(LifetimeRef::from_token(lifetime))
263 }
236 } 264 }
237 } 265 }
238 266