Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 'type annotations needed' error with opaque types #66431

Merged
merged 4 commits into from
Nov 19, 2019

Conversation

Aaron1011
Copy link
Member

@Aaron1011 Aaron1011 commented Nov 15, 2019

Related: #66426

This commit adds handling for opaque types during inference variable
fallback. Type variables generated from the instantiation of opaque
types now fallback to the opaque type itself.

Normally, the type variable for an instantiated opaque type is either
unified with the concrete type, or with the opaque type itself (e.g when
a function returns an opaque type by calling another function).

However, it's possible for the type variable to be left completely
unconstrained. This can occur in code like this:

pub type Foo = impl Copy;
fn produce() -> Option<Foo> {
    None
}

Here, we'll instantatiate the Foo in Option<Foo> to a fresh type
variable, but we will never unify it with anything due to the fact
that we return a None.

This results in the error message:

type annotations needed: cannot resolve `_: std::marker::Copy

pointing at pub type Foo = impl Copy.

This message is not only confusing, it's incorrect. When an opaque type
inference variable is completely unconstrained, we can always fall back
to using the opaque type itself. This effectively turns that particular
use of the opaque type into a non-defining use, even if it appears in a
defining scope.

@rust-highfive
Copy link
Collaborator

r? @davidtwco

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 15, 2019
@Centril Centril added the F-type_alias_impl_trait `#[feature(type_alias_impl_trait)]` label Nov 15, 2019
@Centril
Copy link
Contributor

Centril commented Nov 15, 2019

r? @varkor
cc @nikomatsakis

@rust-highfive rust-highfive assigned varkor and unassigned davidtwco Nov 15, 2019
@bors
Copy link
Contributor

bors commented Nov 15, 2019

☔ The latest upstream changes (presumably #66449) made this pull request unmergeable. Please resolve the merge conflicts.

src/librustc_typeck/check/mod.rs Outdated Show resolved Hide resolved
src/test/ui/impl-trait/where-allowed-2.stderr Show resolved Hide resolved
}

fn _produce() -> Wrapper<Foo> {
Wrapper::Second
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this entirely correct. What is the final type of Foo?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Final type is bool as determined by constrained_foo.

Copy link
Contributor

@Centril Centril left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nits

src/librustc_typeck/check/mod.rs Outdated Show resolved Hide resolved
src/librustc_typeck/check/mod.rs Outdated Show resolved Hide resolved
src/librustc/infer/opaque_types/mod.rs Outdated Show resolved Hide resolved
src/librustc_typeck/check/mod.rs Outdated Show resolved Hide resolved
src/librustc_typeck/check/mod.rs Outdated Show resolved Hide resolved
src/librustc_typeck/check/mod.rs Outdated Show resolved Hide resolved
@Aaron1011
Copy link
Member Author

@Centril @estebank: I've addressed your comments.

Copy link
Member

@varkor varkor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy with the implementation once the nits have been addressed and rebased into the main commits.

src/librustc_typeck/check/mod.rs Outdated Show resolved Hide resolved
src/librustc_typeck/check/mod.rs Outdated Show resolved Hide resolved
src/librustc_typeck/check/mod.rs Outdated Show resolved Hide resolved
src/librustc_typeck/check/mod.rs Outdated Show resolved Hide resolved
src/librustc_typeck/check/mod.rs Outdated Show resolved Hide resolved
src/librustc_typeck/check/mod.rs Outdated Show resolved Hide resolved
src/test/ui/impl-trait/where-allowed-2.stderr Show resolved Hide resolved
Related: rust-lang#66426

This commit adds handling for opaque types during inference variable
fallback. Type variables generated from the instantiatino of opaque
types now fallback to the opque type itself.

Normally, the type variable for an instantiated opaque type is either
unified with the concrete type, or with the opaque type itself (e.g when
a function returns an opaque type by calling another function).

However, it's possible for the type variable to be left completely
unconstrained. This can occur in code like this:

```rust
pub type Foo = impl Copy;
fn produce() -> Option<Foo> {
    None
}
```

Here, we'll instantatiate the `Foo` in `Option<Foo>` to a fresh type
variable, but we will never unify it with anything due to the fact
that we return a `None`.

This results in the error message:

`type annotations needed: cannot resolve `_: std::marker::Copy``

pointing at `pub type Foo = impl Copy`.

This message is not only confusing, it's incorrect. When an opaque type
inference variable is completely unconstrained, we can always fall back
to using the opaque type itself. This effectively turns that particular
use of the opaque type into a non-defining use, even if it appears in a
defining scope.
@Aaron1011
Copy link
Member Author

@varkor: Squashed

@varkor
Copy link
Member

varkor commented Nov 18, 2019

@bors r+

@bors
Copy link
Contributor

bors commented Nov 18, 2019

📌 Commit a11abe0 has been approved by varkor

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 18, 2019
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Nov 19, 2019
…varkor

Fix 'type annotations needed' error with opaque types

Related: rust-lang#66426

This commit adds handling for opaque types during inference variable
fallback. Type variables generated from the instantiation of opaque
types now fallback to the opaque type itself.

Normally, the type variable for an instantiated opaque type is either
unified with the concrete type, or with the opaque type itself (e.g when
a function returns an opaque type by calling another function).

However, it's possible for the type variable to be left completely
unconstrained. This can occur in code like this:

```rust
pub type Foo = impl Copy;
fn produce() -> Option<Foo> {
    None
}
```

Here, we'll instantatiate the `Foo` in `Option<Foo>` to a fresh type
variable, but we will never unify it with anything due to the fact
that we return a `None`.

This results in the error message:
```
type annotations needed: cannot resolve `_: std::marker::Copy
```

pointing at `pub type Foo = impl Copy`.

This message is not only confusing, it's incorrect. When an opaque type
inference variable is completely unconstrained, we can always fall back
to using the opaque type itself. This effectively turns that particular
use of the opaque type into a non-defining use, even if it appears in a
defining scope.
bors added a commit that referenced this pull request Nov 19, 2019
Rollup of 13 pull requests

Successful merges:

 - #66090 (Misc CI improvements)
 - #66239 (Suggest calling async closure when needed)
 - #66430 ([doc] Fix the source code highlighting on source comments)
 - #66431 (Fix 'type annotations needed' error with opaque types)
 - #66461 (Add explanation message for E0641)
 - #66468 (Cleanup Miri SIMD intrinsics)
 - #66478 (rustc_plugin: Remove the compatibility shim)
 - #66493 (Add JohnTitor to rustc-guide toolstate notification list)
 - #66511 (std::error::Chain: remove Copy)
 - #66512 (Add unix::process::CommandExt::arg0)
 - #66520 (Disable gdb pretty printer global section on wasm targets)
 - #66529 (resolve: Give derive helpers highest priority during resolution)
 - #66536 (Move the definition of `QueryResult` into `plumbing.rs`.)

Failed merges:

r? @ghost
Centril added a commit to Centril/rust that referenced this pull request Nov 19, 2019
…varkor

Fix 'type annotations needed' error with opaque types

Related: rust-lang#66426

This commit adds handling for opaque types during inference variable
fallback. Type variables generated from the instantiation of opaque
types now fallback to the opaque type itself.

Normally, the type variable for an instantiated opaque type is either
unified with the concrete type, or with the opaque type itself (e.g when
a function returns an opaque type by calling another function).

However, it's possible for the type variable to be left completely
unconstrained. This can occur in code like this:

```rust
pub type Foo = impl Copy;
fn produce() -> Option<Foo> {
    None
}
```

Here, we'll instantatiate the `Foo` in `Option<Foo>` to a fresh type
variable, but we will never unify it with anything due to the fact
that we return a `None`.

This results in the error message:
```
type annotations needed: cannot resolve `_: std::marker::Copy
```

pointing at `pub type Foo = impl Copy`.

This message is not only confusing, it's incorrect. When an opaque type
inference variable is completely unconstrained, we can always fall back
to using the opaque type itself. This effectively turns that particular
use of the opaque type into a non-defining use, even if it appears in a
defining scope.
bors added a commit that referenced this pull request Nov 19, 2019
Rollup of 11 pull requests

Successful merges:

 - #66090 (Misc CI improvements)
 - #66155 (Add long error explanation for E0594)
 - #66239 (Suggest calling async closure when needed)
 - #66430 ([doc] Fix the source code highlighting on source comments)
 - #66431 (Fix 'type annotations needed' error with opaque types)
 - #66461 (Add explanation message for E0641)
 - #66493 (Add JohnTitor to rustc-guide toolstate notification list)
 - #66511 (std::error::Chain: remove Copy)
 - #66529 (resolve: Give derive helpers highest priority during resolution)
 - #66536 (Move the definition of `QueryResult` into `plumbing.rs`.)
 - #66538 (Remove compiler_builtins_lib feature from libstd)

Failed merges:

r? @ghost
@bors bors merged commit a11abe0 into rust-lang:master Nov 19, 2019
@Aaron1011 Aaron1011 deleted the fix/opaque-type-infer branch November 19, 2019 15:24
@bors
Copy link
Contributor

bors commented Nov 19, 2019

☔ The latest upstream changes (presumably #66545) made this pull request unmergeable. Please resolve the merge conflicts.

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Nov 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-type_alias_impl_trait `#[feature(type_alias_impl_trait)]` S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
Development

Successfully merging this pull request may close these issues.

7 participants