Struct rustc_typeck::check::coercion::CoerceMany [−][src]
pub struct CoerceMany<'gcx, 'tcx, 'exprs, E> where
'gcx: 'tcx,
E: 'exprs + AsCoercionSite, { expected_ty: Ty<'tcx>, final_ty: Option<Ty<'tcx>>, expressions: Expressions<'gcx, 'exprs, E>, pushed: usize, }
🔬 This is a nightly-only experimental API. (rustc_private
)
this crate is being loaded from the sysroot, an unstable location; did you mean to load this crate from crates.io via Cargo.toml
instead?
CoerceMany encapsulates the pattern you should use when you have
many expressions that are all getting coerced to a common
type. This arises, for example, when you have a match (the result
of each arm is coerced to a common type). It also arises in less
obvious places, such as when you have many break foo
expressions
that target the same loop, or the various return
expressions in
a function.
The basic protocol is as follows:
- Instantiate the
CoerceMany
with an initialexpected_ty
. This will also serve as the "starting LUB". The expectation is that this type is something which all of the expressions must be coercible to. Use a fresh type variable if needed. - For each expression whose result is to be coerced, invoke
coerce()
with.- In some cases we wish to coerce "non-expressions" whose types are implicitly
unit. This happens for example if you have a
break
with no expression, or anif
with noelse
. In that case, invokecoerce_forced_unit()
. coerce()
andcoerce_forced_unit()
may report errors. They hide this from you so that you don't have to worry your pretty head about it. But if an error is reported, the final type will beerr
.- Invoking
coerce()
may cause us to go and adjust the "adjustments" on previously coerced expressions.
- In some cases we wish to coerce "non-expressions" whose types are implicitly
unit. This happens for example if you have a
- When all done, invoke
complete()
. This will return the LUB of all your expressions.- WARNING: I don't believe this final type is guaranteed to be
related to your initial
expected_ty
in any particular way, although it will typically be a subtype, so you should check it. - Invoking
complete()
may cause us to go and adjust the "adjustments" on previously coerced expressions.
- WARNING: I don't believe this final type is guaranteed to be
related to your initial
Example:
let mut coerce = CoerceMany::new(expected_ty); for expr in exprs { let expr_ty = fcx.check_expr_with_expectation(expr, expected); coerce.coerce(fcx, &cause, expr, expr_ty); } let final_ty = coerce.complete(fcx);
Fields
expected_ty: Ty<'tcx>
🔬 This is a nightly-only experimental API. (rustc_private
)
this crate is being loaded from the sysroot, an unstable location; did you mean to load this crate from crates.io via Cargo.toml
instead?
final_ty: Option<Ty<'tcx>>
🔬 This is a nightly-only experimental API. (rustc_private
)
this crate is being loaded from the sysroot, an unstable location; did you mean to load this crate from crates.io via Cargo.toml
instead?
expressions: Expressions<'gcx, 'exprs, E>
🔬 This is a nightly-only experimental API. (rustc_private
)
this crate is being loaded from the sysroot, an unstable location; did you mean to load this crate from crates.io via Cargo.toml
instead?
pushed: usize
🔬 This is a nightly-only experimental API. (rustc_private
)
this crate is being loaded from the sysroot, an unstable location; did you mean to load this crate from crates.io via Cargo.toml
instead?
Methods
impl<'gcx, 'tcx, 'exprs, E> CoerceMany<'gcx, 'tcx, 'exprs, E> where
'gcx: 'tcx,
E: 'exprs + AsCoercionSite,
[src]
impl<'gcx, 'tcx, 'exprs, E> CoerceMany<'gcx, 'tcx, 'exprs, E> where
'gcx: 'tcx,
E: 'exprs + AsCoercionSite,
pub fn new(expected_ty: Ty<'tcx>) -> Self
[src]
pub fn new(expected_ty: Ty<'tcx>) -> Self
🔬 This is a nightly-only experimental API. (rustc_private
)
this crate is being loaded from the sysroot, an unstable location; did you mean to load this crate from crates.io via Cargo.toml
instead?
The usual case; collect the set of expressions dynamically.
If the full set of coercion sites is known before hand,
consider with_coercion_sites()
instead to avoid allocation.
pub fn with_coercion_sites(
expected_ty: Ty<'tcx>,
coercion_sites: &'exprs [E]
) -> Self
[src]
pub fn with_coercion_sites(
expected_ty: Ty<'tcx>,
coercion_sites: &'exprs [E]
) -> Self
🔬 This is a nightly-only experimental API. (rustc_private
)
this crate is being loaded from the sysroot, an unstable location; did you mean to load this crate from crates.io via Cargo.toml
instead?
As an optimization, you can create a CoerceMany
with a
pre-existing slice of expressions. In this case, you are
expected to pass each element in the slice to coerce(...)
in
order. This is used with arrays in particular to avoid
needlessly cloning the slice.
fn make(
expected_ty: Ty<'tcx>,
expressions: Expressions<'gcx, 'exprs, E>
) -> Self
[src]
fn make(
expected_ty: Ty<'tcx>,
expressions: Expressions<'gcx, 'exprs, E>
) -> Self
🔬 This is a nightly-only experimental API. (rustc_private
)
this crate is being loaded from the sysroot, an unstable location; did you mean to load this crate from crates.io via Cargo.toml
instead?
pub fn expected_ty(&self) -> Ty<'tcx>
[src]
pub fn expected_ty(&self) -> Ty<'tcx>
🔬 This is a nightly-only experimental API. (rustc_private
)
this crate is being loaded from the sysroot, an unstable location; did you mean to load this crate from crates.io via Cargo.toml
instead?
Return the "expected type" with which this coercion was constructed. This represents the "downward propagated" type that was given to us at the start of typing whatever construct we are typing (e.g., the match expression).
Typically, this is used as the expected type when type-checking each of the alternative expressions whose types we are trying to merge.
pub fn merged_ty(&self) -> Ty<'tcx>
[src]
pub fn merged_ty(&self) -> Ty<'tcx>
🔬 This is a nightly-only experimental API. (rustc_private
)
this crate is being loaded from the sysroot, an unstable location; did you mean to load this crate from crates.io via Cargo.toml
instead?
Returns the current "merged type", representing our best-guess
at the LUB of the expressions we've seen so far (if any). This
isn't final until you call self.final()
, which will return
the merged type.
pub fn coerce<'a>(
&mut self,
fcx: &FnCtxt<'a, 'gcx, 'tcx>,
cause: &ObligationCause<'tcx>,
expression: &'gcx Expr,
expression_ty: Ty<'tcx>
)
[src]
pub fn coerce<'a>(
&mut self,
fcx: &FnCtxt<'a, 'gcx, 'tcx>,
cause: &ObligationCause<'tcx>,
expression: &'gcx Expr,
expression_ty: Ty<'tcx>
)
🔬 This is a nightly-only experimental API. (rustc_private
)
this crate is being loaded from the sysroot, an unstable location; did you mean to load this crate from crates.io via Cargo.toml
instead?
Indicates that the value generated by expression
, which is
of type expression_ty
, is one of the possibility that we
could coerce from. This will record expression
and later
calls to coerce
may come back and add adjustments and things
if necessary.
pub fn coerce_forced_unit<'a>(
&mut self,
fcx: &FnCtxt<'a, 'gcx, 'tcx>,
cause: &ObligationCause<'tcx>,
augment_error: &mut FnMut(&mut DiagnosticBuilder),
label_unit_as_expected: bool
)
[src]
pub fn coerce_forced_unit<'a>(
&mut self,
fcx: &FnCtxt<'a, 'gcx, 'tcx>,
cause: &ObligationCause<'tcx>,
augment_error: &mut FnMut(&mut DiagnosticBuilder),
label_unit_as_expected: bool
)
🔬 This is a nightly-only experimental API. (rustc_private
)
this crate is being loaded from the sysroot, an unstable location; did you mean to load this crate from crates.io via Cargo.toml
instead?
Indicates that one of the inputs is a "forced unit". This
occurs in a case like if foo { ... };
, where the missing else
generates a "forced unit". Another example is a loop { break; }
, where the break
has no argument expression. We treat
these cases slightly differently for error-reporting
purposes. Note that these tend to correspond to cases where
the ()
expression is implicit in the source, and hence we do
not take an expression argument.
The augment_error
gives you a chance to extend the error
message, in case any results (e.g., we use this to suggest
removing a ;
).
fn coerce_inner<'a>(
&mut self,
fcx: &FnCtxt<'a, 'gcx, 'tcx>,
cause: &ObligationCause<'tcx>,
expression: Option<&'gcx Expr>,
expression_ty: Ty<'tcx>,
augment_error: Option<&mut FnMut(&mut DiagnosticBuilder)>,
label_expression_as_expected: bool
)
[src]
fn coerce_inner<'a>(
&mut self,
fcx: &FnCtxt<'a, 'gcx, 'tcx>,
cause: &ObligationCause<'tcx>,
expression: Option<&'gcx Expr>,
expression_ty: Ty<'tcx>,
augment_error: Option<&mut FnMut(&mut DiagnosticBuilder)>,
label_expression_as_expected: bool
)
🔬 This is a nightly-only experimental API. (rustc_private
)
this crate is being loaded from the sysroot, an unstable location; did you mean to load this crate from crates.io via Cargo.toml
instead?
The inner coercion "engine". If expression
is None
, this
is a forced-unit case, and hence expression_ty
must be
Nil
.
pub fn complete<'a>(self, fcx: &FnCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx>
[src]
pub fn complete<'a>(self, fcx: &FnCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx>
🔬 This is a nightly-only experimental API. (rustc_private
)
this crate is being loaded from the sysroot, an unstable location; did you mean to load this crate from crates.io via Cargo.toml
instead?
Auto Trait Implementations
impl<'gcx, 'tcx, 'exprs, E> !Send for CoerceMany<'gcx, 'tcx, 'exprs, E>
impl<'gcx, 'tcx, 'exprs, E> !Send for CoerceMany<'gcx, 'tcx, 'exprs, E>
impl<'gcx, 'tcx, 'exprs, E> !Sync for CoerceMany<'gcx, 'tcx, 'exprs, E>
impl<'gcx, 'tcx, 'exprs, E> !Sync for CoerceMany<'gcx, 'tcx, 'exprs, E>