Function rustc::middle::kind::check_cast_for_escaping_regions[src]

pub fn check_cast_for_escaping_regions(cx: &Context, source_ty: t, target_ty: t, source_span: Span)

This is rather subtle. When we are casting a value to an instantiated trait like a as trait<'r>, regionck already ensures that any references that appear in the type of a are bounded by 'r (ed.: rem FIXME(#5723)). However, it is possible that there are type parameters in the type of a, and those type parameters may have references within them. We have to guarantee that the regions which appear in those type parameters are not obscured.

Therefore, we ensure that one of three conditions holds:

(1) The trait instance cannot escape the current fn. This is guaranteed if the region bound &r is some scope within the fn itself. This case is safe because whatever references are found within the type parameter, they must enclose the fn body itself.

(2) The type parameter appears in the type of the trait. For example, if the type parameter is T and the trait type is deque<T>, then whatever references may appear in T also appear in deque<T>.

(3) The type parameter is sendable (and therefore does not contain references).

FIXME(#5723)---This code should probably move into regionck.