]> git.lizzy.rs Git - rust.git/commitdiff
Make lifetimes lint work with type aliases and non-locally-defined structs
authorFlorian Hartwig <florian.j.hartwig@gmail.com>
Fri, 4 Dec 2015 14:56:25 +0000 (15:56 +0100)
committerFlorian Hartwig <florian.j.hartwig@gmail.com>
Sun, 6 Dec 2015 01:04:13 +0000 (02:04 +0100)
src/lifetimes.rs
tests/compile-fail/lifetimes.rs

index acc7b0140529a4584182558990fe71ab07014231..0003c50d84385c1543f3d2b7e1ef80fc2b3257f3 100644 (file)
@@ -3,7 +3,7 @@
 use rustc::lint::*;
 use syntax::codemap::Span;
 use rustc_front::intravisit::{Visitor, walk_ty, walk_ty_param_bound};
-use rustc::middle::def::Def::{DefTy, DefTrait};
+use rustc::middle::def::Def::{DefTy, DefTrait, DefStruct};
 use std::collections::HashSet;
 
 use utils::{in_external_macro, span_lint};
@@ -187,23 +187,22 @@ fn collect_anonymous_lifetimes(&mut self, path: &Path, ty: &Ty) {
         let last_path_segment = path.segments.last().map(|s| &s.parameters);
         if let Some(&AngleBracketedParameters(ref params)) = last_path_segment {
             if params.lifetimes.is_empty() {
-                let def = self.cx.tcx.def_map.borrow().get(&ty.id).map(|r| r.full_def());
-                match def {
-                    Some(DefTy(def_id, _)) => {
-                        if let Some(ty_def) = self.cx.tcx.adt_defs.borrow().get(&def_id) {
-                            let scheme = ty_def.type_scheme(self.cx.tcx);
-                            for _ in scheme.generics.regions.as_slice() {
+                if let Some(def) = self.cx.tcx.def_map.borrow().get(&ty.id).map(|r| r.full_def()) {
+                    match def {
+                        DefTy(def_id, _) | DefStruct(def_id) => {
+                            let type_scheme = self.cx.tcx.lookup_item_type(def_id);
+                            for _ in type_scheme.generics.regions.as_slice() {
                                 self.record(&None);
                             }
-                        }
-                    }
-                    Some(DefTrait(def_id)) => {
-                        let trait_def = self.cx.tcx.trait_defs.borrow()[&def_id];
-                        for _ in &trait_def.generics.regions {
-                            self.record(&None);
-                        }
+                        },
+                        DefTrait(def_id) => {
+                            let trait_def = self.cx.tcx.trait_defs.borrow()[&def_id];
+                            for _ in &trait_def.generics.regions {
+                                self.record(&None);
+                            }
+                        },
+                        _ => {}
                     }
-                    _ => {}
                 }
             }
         }
index f5d95aacc9a3659786a0ee5046e8e82a90b4a005..040c3554e89907260bfbaaf22c54ca9aecb3760b 100644 (file)
@@ -106,5 +106,18 @@ fn trait_obj_elided<'a>(_arg: &'a WithLifetime) -> &'a str { unimplemented!() }
 // unambiguous if we elided the lifetime
 fn trait_obj_elided2<'a>(_arg: &'a Drop) -> &'a str { unimplemented!() } //~ERROR explicit lifetimes given
 
+type FooAlias<'a> = Foo<'a>;
+
+fn alias_with_lt<'a>(_foo: FooAlias<'a>) -> &'a str { unimplemented!() } //~ERROR explicit lifetimes given
+
+// no warning, two input lifetimes (named on the reference, anonymous on Foo)
+fn alias_with_lt2<'a>(_foo: &'a FooAlias) -> &'a str { unimplemented!() }
+
+// no warning, two input lifetimes (anonymous on the reference, named on Foo)
+fn alias_with_lt3<'a>(_foo: &FooAlias<'a> ) -> &'a str { unimplemented!() }
+
+// no warning, two input lifetimes
+fn alias_with_lt4<'a, 'b>(_foo: &'a FooAlias<'b> ) -> &'a str { unimplemented!() }
+
 fn main() {
 }