Skip to content

Commit

Permalink
Simplify and type_known_to_meet_builtin_bound and make it more correc…
Browse files Browse the repository at this point in the history
…t when

associated types are involved.
  • Loading branch information
arielb1 authored and Ariel Ben-Yehuda committed Jun 18, 2015
1 parent 7a13b93 commit 56d765d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 59 deletions.
75 changes: 16 additions & 59 deletions src/librustc/middle/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,12 @@ pub fn predicates_for_generics<'tcx>(tcx: &ty::ctxt<'tcx>,
/// `bound` or is not known to meet bound (note that this is
/// conservative towards *no impl*, which is the opposite of the
/// `evaluate` methods).
pub fn evaluate_builtin_bound<'a,'tcx>(infcx: &InferCtxt<'a,'tcx>,
typer: &ty::ClosureTyper<'tcx>,
ty: Ty<'tcx>,
bound: ty::BuiltinBound,
span: Span)
-> SelectionResult<'tcx, ()>
pub fn type_known_to_meet_builtin_bound<'a,'tcx>(infcx: &InferCtxt<'a,'tcx>,
typer: &ty::ClosureTyper<'tcx>,
ty: Ty<'tcx>,
bound: ty::BuiltinBound,
span: Span)
-> bool
{
debug!("type_known_to_meet_builtin_bound(ty={}, bound={:?})",
ty.repr(infcx.tcx),
Expand All @@ -327,61 +327,18 @@ pub fn evaluate_builtin_bound<'a,'tcx>(infcx: &InferCtxt<'a,'tcx>,
// Note: we only assume something is `Copy` if we can
// *definitively* show that it implements `Copy`. Otherwise,
// assume it is move; linear is always ok.
let result = match fulfill_cx.select_all_or_error(infcx, typer) {
Ok(()) => Ok(Some(())), // Success, we know it implements Copy.
Err(errors) => {
// If there were any hard errors, propagate an arbitrary
// one of those. If no hard errors at all, report
// ambiguity.
let sel_error =
errors.iter()
.filter_map(|err| {
match err.code {
CodeAmbiguity => None,
CodeSelectionError(ref e) => Some(e.clone()),
CodeProjectionError(_) => {
infcx.tcx.sess.span_bug(
span,
"projection error while selecting?")
}
}
})
.next();
match sel_error {
None => { Ok(None) }
Some(e) => { Err(e) }
}
}
};

debug!("type_known_to_meet_builtin_bound: ty={} bound={:?} result={:?}",
ty.repr(infcx.tcx),
bound,
result);

result
}

pub fn type_known_to_meet_builtin_bound<'a,'tcx>(infcx: &InferCtxt<'a,'tcx>,
typer: &ty::ClosureTyper<'tcx>,
ty: Ty<'tcx>,
bound: ty::BuiltinBound,
span: Span)
-> bool
{
match evaluate_builtin_bound(infcx, typer, ty, bound, span) {
Ok(Some(())) => {
// definitely impl'd
match fulfill_cx.select_all_or_error(infcx, typer) {
Ok(()) => {
debug!("type_known_to_meet_builtin_bound: ty={} bound={:?} success",
ty.repr(infcx.tcx),
bound);
true
}
Ok(None) => {
// ambiguous: if coherence check was successful, shouldn't
// happen, but we might have reported an error and been
// soldering on, so just treat this like not implemented
false
}
Err(_) => {
// errors: not implemented.
Err(e) => {
debug!("type_known_to_meet_builtin_bound: ty={} bound={:?} errors={}",
ty.repr(infcx.tcx),
bound,
e.repr(infcx.tcx));
false
}
}
Expand Down
24 changes: 24 additions & 0 deletions src/test/compile-fail/issue-25700.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2015 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.

struct S<T: 'static>(Option<&'static T>);

trait Tr { type Out; }
impl<T> Tr for T { type Out = T; }

impl<T: 'static> Copy for S<T> where S<T>: Tr<Out=T> {}
impl<T: 'static> Clone for S<T> where S<T>: Tr<Out=T> {
fn clone(&self) -> Self { *self }
}
fn main() {
let t = S::<()>(None);
drop(t);
drop(t); //~ ERROR use of moved value
}
22 changes: 22 additions & 0 deletions src/test/run-pass/issue-25700-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2015 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.

struct S<T: 'static>(Option<&'static T>);

trait Tr { type Out; }
impl<T> Tr for T { type Out = T; }

impl<T: 'static> Copy for S<T> where S<T>: Tr<Out=T> {}
impl<T: 'static> Clone for S<T> where S<T>: Tr<Out=T> {
fn clone(&self) -> Self { *self }
}
fn main() {
S::<()>(None);
}

0 comments on commit 56d765d

Please sign in to comment.