Skip to content

Commit 5aa89d8

Browse files
committed
Remove the Option and bool impls for carrier and add a dummy impl
The dummy impl should ensure the same type checking behaviour as having other (real) Carrier impls.
1 parent 683bcc0 commit 5aa89d8

File tree

3 files changed

+34
-77
lines changed

3 files changed

+34
-77
lines changed

src/libcore/ops.rs

+7-60
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ use cmp::PartialOrd;
7676
use fmt;
7777
use marker::{Sized, Unsize};
7878
use result::Result::{self, Ok, Err};
79-
use option::Option::{self, Some, None};
8079

8180
/// The `Drop` trait is used to run some code when a value goes out of scope.
8281
/// This is sometimes called a 'destructor'.
@@ -2203,75 +2202,23 @@ impl<U, V> Carrier for Result<U, V> {
22032202
}
22042203
}
22052204

2206-
#[unstable(feature = "question_mark_carrier", issue = "31436")]
2207-
impl<U> Carrier for Option<U> {
2208-
type Success = U;
2209-
type Error = ();
2210-
2211-
fn from_success(u: U) -> Option<U> {
2212-
Some(u)
2213-
}
2205+
struct _DummyErrorType;
22142206

2215-
fn from_error(_: ()) -> Option<U> {
2216-
None
2217-
}
2218-
2219-
fn translate<T>(self) -> T
2220-
where T: Carrier<Success=U, Error=()>
2221-
{
2222-
match self {
2223-
Some(u) => T::from_success(u),
2224-
None => T::from_error(()),
2225-
}
2226-
}
2227-
}
2228-
2229-
// Implementing Carrier for bools means it's easy to write short-circuiting
2230-
// functions. E.g.,
2231-
// ```
2232-
// fn foo() -> bool {
2233-
// if !(f() || g()) {
2234-
// return false;
2235-
// }
2236-
//
2237-
// some_computation();
2238-
// if h() {
2239-
// return false;
2240-
// }
2241-
//
2242-
// more_computation();
2243-
// i()
2244-
// }
2245-
// ```
2246-
// becomes
2247-
// ```
2248-
// fn foo() -> bool {
2249-
// (f() || g())?;
2250-
// some_computation();
2251-
// (!h())?;
2252-
// more_computation();
2253-
// i()
2254-
// }
2255-
// ```
2256-
#[unstable(feature = "question_mark_carrier", issue = "31436")]
2257-
impl Carrier for bool {
2207+
impl Carrier for _DummyErrorType {
22582208
type Success = ();
22592209
type Error = ();
22602210

2261-
fn from_success(_: ()) -> bool {
2262-
true
2211+
fn from_success(_: ()) -> _DummyErrorType {
2212+
_DummyErrorType
22632213
}
22642214

2265-
fn from_error(_: ()) -> bool {
2266-
false
2215+
fn from_error(_: ()) -> _DummyErrorType {
2216+
_DummyErrorType
22672217
}
22682218

22692219
fn translate<T>(self) -> T
22702220
where T: Carrier<Success=(), Error=()>
22712221
{
2272-
match self {
2273-
true => T::from_success(()),
2274-
false => T::from_error(()),
2275-
}
2222+
T::from_success(())
22762223
}
22772224
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(question_mark, question_mark_carrier)]
12+
13+
// Test that type inference fails where there are multiple possible return types
14+
// for the `?` operator.
15+
16+
fn f(x: &i32) -> Result<i32, ()> {
17+
Ok(*x)
18+
}
19+
20+
fn g() -> Result<Vec<i32>, ()> {
21+
let l = [1, 2, 3, 4];
22+
l.iter().map(f).collect()? //~ ERROR type annotations required: cannot resolve
23+
}
24+
25+
fn main() {
26+
g();
27+
}

src/test/run-pass/try-operator.rs

-17
Original file line numberDiff line numberDiff line change
@@ -144,23 +144,6 @@ fn merge_error() -> Result<i32, Error> {
144144
Ok(s.parse::<i32>()? + 1)
145145
}
146146

147-
fn option() -> Option<i32> {
148-
let x = Some(42);
149-
let y = x?;
150-
Some(y + 2)
151-
}
152-
153-
fn bool() -> bool {
154-
let x = true;
155-
let y = false;
156-
let z = true;
157-
158-
(x || y)?;
159-
let a: () = z?;
160-
x?;
161-
true
162-
}
163-
164147
fn main() {
165148
assert_eq!(Ok(3), on_method());
166149

0 commit comments

Comments
 (0)