2013-12-08 01:55:27 -06:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-10-27 17:37:07 -05:00
|
|
|
#![allow(unused_variables)]
|
2014-03-21 20:05:05 -05:00
|
|
|
#![allow(non_camel_case_types)]
|
|
|
|
#![deny(dead_code)]
|
2015-01-22 20:22:03 -06:00
|
|
|
#![feature(libc)]
|
2013-12-08 01:55:27 -06:00
|
|
|
|
2014-03-21 20:05:05 -05:00
|
|
|
#![crate_type="lib"]
|
2013-12-08 01:55:27 -06:00
|
|
|
|
2014-02-26 11:58:41 -06:00
|
|
|
extern crate libc;
|
|
|
|
|
2014-08-12 21:25:05 -05:00
|
|
|
pub use extern_foo as x;
|
2014-07-21 00:11:43 -05:00
|
|
|
extern {
|
2015-03-15 15:00:41 -05:00
|
|
|
pub fn extern_foo();
|
2014-07-21 00:11:43 -05:00
|
|
|
}
|
|
|
|
|
2014-09-20 07:08:10 -05:00
|
|
|
struct Foo; //~ ERROR: struct is never used
|
2013-12-08 01:55:27 -06:00
|
|
|
impl Foo {
|
2014-09-20 07:08:10 -05:00
|
|
|
fn foo(&self) { //~ ERROR: method is never used
|
2013-12-08 01:55:27 -06:00
|
|
|
bar()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-20 07:08:10 -05:00
|
|
|
fn bar() { //~ ERROR: function is never used
|
|
|
|
fn baz() {} //~ ERROR: function is never used
|
2013-12-08 01:55:27 -06:00
|
|
|
|
|
|
|
Foo.foo();
|
|
|
|
baz();
|
|
|
|
}
|
|
|
|
|
|
|
|
// no warning
|
|
|
|
struct Foo2;
|
|
|
|
impl Foo2 { fn foo2(&self) { bar2() } }
|
|
|
|
fn bar2() {
|
|
|
|
fn baz2() {}
|
|
|
|
|
|
|
|
Foo2.foo2();
|
|
|
|
baz2();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn pub_fn() {
|
|
|
|
let foo2_struct = Foo2;
|
|
|
|
foo2_struct.foo2();
|
2013-12-13 23:08:26 -06:00
|
|
|
|
|
|
|
blah::baz();
|
2013-12-08 01:55:27 -06:00
|
|
|
}
|
|
|
|
|
2013-12-13 23:08:26 -06:00
|
|
|
mod blah {
|
2014-02-26 11:58:41 -06:00
|
|
|
use libc::size_t;
|
2013-12-13 23:08:26 -06:00
|
|
|
// not warned because it's used in the parameter of `free` and return of
|
|
|
|
// `malloc` below, which are also used.
|
|
|
|
enum c_void {}
|
|
|
|
|
|
|
|
extern {
|
2014-06-25 14:47:34 -05:00
|
|
|
fn free(p: *const c_void);
|
|
|
|
fn malloc(size: size_t) -> *const c_void;
|
2013-12-13 23:08:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn baz() {
|
|
|
|
unsafe { free(malloc(4)); }
|
|
|
|
}
|
|
|
|
}
|
2013-12-08 01:55:27 -06:00
|
|
|
|
2014-09-20 07:08:10 -05:00
|
|
|
enum c_void {} //~ ERROR: enum is never used
|
2013-12-08 01:55:27 -06:00
|
|
|
extern {
|
2014-09-20 07:08:10 -05:00
|
|
|
fn free(p: *const c_void); //~ ERROR: foreign function is never used
|
2013-12-08 01:55:27 -06:00
|
|
|
}
|
2013-12-31 15:19:57 -06:00
|
|
|
|
|
|
|
// Check provided method
|
|
|
|
mod inner {
|
|
|
|
pub trait Trait {
|
|
|
|
fn f(&self) { f(); }
|
|
|
|
}
|
|
|
|
|
2015-01-08 04:54:35 -06:00
|
|
|
impl Trait for isize {}
|
2013-12-31 15:19:57 -06:00
|
|
|
|
|
|
|
fn f() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn foo() {
|
Add trivial cast lints.
This permits all coercions to be performed in casts, but adds lints to warn in those cases.
Part of this patch moves cast checking to a later stage of type checking. We acquire obligations to check casts as part of type checking where we previously checked them. Once we have type checked a function or module, then we check any cast obligations which have been acquired. That means we have more type information available to check casts (this was crucial to making coercions work properly in place of some casts), but it means that casts cannot feed input into type inference.
[breaking change]
* Adds two new lints for trivial casts and trivial numeric casts, these are warn by default, but can cause errors if you build with warnings as errors. Previously, trivial numeric casts and casts to trait objects were allowed.
* The unused casts lint has gone.
* Interactions between casting and type inference have changed in subtle ways. Two ways this might manifest are:
- You may need to 'direct' casts more with extra type information, for example, in some cases where `foo as _ as T` succeeded, you may now need to specify the type for `_`
- Casts do not influence inference of integer types. E.g., the following used to type check:
```
let x = 42;
let y = &x as *const u32;
```
Because the cast would inform inference that `x` must have type `u32`. This no longer applies and the compiler will fallback to `i32` for `x` and thus there will be a type error in the cast. The solution is to add more type information:
```
let x: u32 = 42;
let y = &x as *const u32;
```
2015-03-19 23:15:27 -05:00
|
|
|
let a: &inner::Trait = &1_isize;
|
2013-12-31 15:19:57 -06:00
|
|
|
a.f();
|
|
|
|
}
|