auto merge of #12572 : lifthrasiir/rust/owned-ptr-static-bound, r=alexcrichton
This is inspired by the [question](http://www.reddit.com/r/rust/comments/1yy57k/unsolved_question_from_irc/) (re-)posted to /r/rust. The error message in this question correctly states that one should add `'static` to the trait bounds, but does not state which trait bounds. This PR makes that explicit by appending two words. This also renames `check_durable` to `check_static` and removes the outdated comment as a cleanup.
This commit is contained in:
commit
b4d923852e
2 changed files with 36 additions and 6 deletions
|
|
@ -315,7 +315,7 @@ pub fn check_expr(cx: &mut Context, e: &Expr) {
|
|||
match e.node {
|
||||
ExprUnary(UnBox, interior) => {
|
||||
let interior_type = ty::expr_ty(cx.tcx, interior);
|
||||
let _ = check_durable(cx.tcx, interior_type, interior.span);
|
||||
let _ = check_static(cx.tcx, interior_type, interior.span);
|
||||
}
|
||||
ExprCast(source, _) => {
|
||||
let source_ty = ty::expr_ty(cx.tcx, source);
|
||||
|
|
@ -474,13 +474,13 @@ pub fn check_send(cx: &Context, ty: ty::t, sp: Span) -> bool {
|
|||
}
|
||||
}
|
||||
|
||||
// note: also used from middle::typeck::regionck!
|
||||
pub fn check_durable(tcx: ty::ctxt, ty: ty::t, sp: Span) -> bool {
|
||||
pub fn check_static(tcx: ty::ctxt, ty: ty::t, sp: Span) -> bool {
|
||||
if !ty::type_is_static(tcx, ty) {
|
||||
match ty::get(ty).sty {
|
||||
ty::ty_param(..) => {
|
||||
tcx.sess.span_err(sp, "value may contain references; \
|
||||
add `'static` bound");
|
||||
tcx.sess.span_err(sp,
|
||||
format!("value may contain references; \
|
||||
add `'static` bound to `{}`", ty_to_str(tcx, ty)));
|
||||
}
|
||||
_ => {
|
||||
tcx.sess.span_err(sp, "value may contain references");
|
||||
|
|
@ -578,7 +578,7 @@ pub fn check_cast_for_escaping_regions(
|
|||
if target_params.iter().any(|x| x == &source_param) {
|
||||
/* case (2) */
|
||||
} else {
|
||||
check_durable(cx.tcx, ty, source_span); /* case (3) */
|
||||
check_static(cx.tcx, ty, source_span); /* case (3) */
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
|
|
|
|||
30
src/test/compile-fail/owned-ptr-static-bound.rs
Normal file
30
src/test/compile-fail/owned-ptr-static-bound.rs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
// Copyright 2014 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.
|
||||
|
||||
trait A<T> {}
|
||||
struct B<'a, T>(&'a A<T>);
|
||||
|
||||
trait X {}
|
||||
impl<'a, T> X for B<'a, T> {}
|
||||
|
||||
fn f<'a, T, U>(v: ~A<T>) -> ~X: {
|
||||
~B(v) as ~X: //~ ERROR value may contain references; add `'static` bound to `T`
|
||||
}
|
||||
|
||||
fn g<'a, T, U>(v: ~A<U>) -> ~X: {
|
||||
~B(v) as ~X: //~ ERROR value may contain references; add `'static` bound to `U`
|
||||
}
|
||||
|
||||
fn h<'a, T: 'static>(v: ~A<T>) -> ~X: {
|
||||
~B(v) as ~X: // ok
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue