-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Fix cycle error with existential types #62423
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8ba9b10
Fix cycle error with existential types
Aaron1011 ec62699
Fix bug when opaque type was nested in another type.
Aaron1011 2f05160
Remove unecessary doc comment
Aaron1011 2ab8d61
s/consts/`const` items/
Aaron1011 3600832
Address review comments
Aaron1011 66b2b97
Fix test stderr
Aaron1011 2f41962
Add explanation to 'existential_type_const' test
Aaron1011 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1253,17 +1253,38 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { | |
&anon_ty, | ||
locations.span(body), | ||
)); | ||
|
||
let revealed_ty_is_opaque = revealed_ty.is_impl_trait(); | ||
|
||
debug!( | ||
"eq_opaque_type_and_type: \ | ||
instantiated output_ty={:?} \ | ||
opaque_type_map={:#?} \ | ||
revealed_ty={:?}", | ||
output_ty, opaque_type_map, revealed_ty | ||
revealed_ty={:?} \ | ||
revealed_ty_is_opaque={}", | ||
output_ty, opaque_type_map, revealed_ty, revealed_ty_is_opaque | ||
); | ||
obligations.add(infcx | ||
.at(&ObligationCause::dummy(), param_env) | ||
.eq(output_ty, revealed_ty)?); | ||
|
||
// This is 'true' when we're using an existential | ||
// type without 'revelaing' it. For example, code like this: | ||
Centril marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// | ||
// existential type Foo: Debug; | ||
// fn foo1() -> Foo { ... } | ||
// fn foo2() -> Foo { foo1() } | ||
// | ||
// In 'foo2', we're not revealing the type of 'Foo' - we're | ||
Centril marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// just treating it as the opaque type. All of the constraints | ||
// in our 'opaque_type_map' apply to the concrete type, | ||
Centril marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// not to the opaque type itself. Therefore, it's enough | ||
// to simply equate the output and opque 'revealed_type', | ||
Centril marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// as we do above | ||
Centril marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if revealed_ty_is_opaque { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just testing that the revealed type is opaque doesn't seem like enough |
||
return Ok(InferOk { value: None, obligations: obligations.into_vec() }); | ||
} | ||
|
||
for (&opaque_def_id, opaque_decl) in &opaque_type_map { | ||
let opaque_defn_ty = tcx.type_of(opaque_def_id); | ||
let opaque_defn_ty = opaque_defn_ty.subst(tcx, opaque_decl.substs); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#![feature(existential_type)] | ||
Centril marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#![feature(impl_trait_in_bindings)] | ||
Centril marked this conversation as resolved.
Show resolved
Hide resolved
|
||
//~^ WARN the feature `impl_trait_in_bindings` is incomplete and may cause the compiler to crash | ||
|
||
// Ensures that consts can constrain an existential type | ||
Aaron1011 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
use std::fmt::Debug; | ||
|
||
// Type `Foo` refers to a type that implements the `Debug` trait. | ||
// The concrete type to which `Foo` refers is inferred from this module, | ||
// and this concrete type is hidden from outer modules (but not submodules). | ||
pub existential type Foo: Debug; | ||
|
||
const _FOO: Foo = 5; | ||
|
||
fn main() { | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
warning: the feature `impl_trait_in_bindings` is incomplete and may cause the compiler to crash | ||
--> $DIR/existential_type_const.rs:2:12 | ||
| | ||
LL | #![feature(impl_trait_in_bindings)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^ | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#![feature(existential_type)] | ||
|
||
// Regression test for issue #61863 | ||
|
||
pub trait MyTrait {} | ||
|
||
#[derive(Debug)] | ||
pub struct MyStruct { | ||
v: u64 | ||
} | ||
|
||
impl MyTrait for MyStruct {} | ||
|
||
pub fn bla() -> TE { | ||
return MyStruct {v:1} | ||
} | ||
|
||
pub fn bla2() -> TE { | ||
bla() | ||
} | ||
|
||
|
||
existential type TE: MyTrait; | ||
|
||
fn main() {} |
2 changes: 1 addition & 1 deletion
2
src/test/ui/existential_types/existential-types-with-cycle-error.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 1 addition & 23 deletions
24
src/test/ui/existential_types/existential-types-with-cycle-error.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,8 @@ | ||
error[E0391]: cycle detected when processing `Foo` | ||
error: could not find defining uses | ||
--> $DIR/existential-types-with-cycle-error.rs:3:1 | ||
| | ||
LL | existential type Foo: Fn() -> Foo; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
note: ...which requires processing `crash`... | ||
--> $DIR/existential-types-with-cycle-error.rs:6:25 | ||
| | ||
LL | fn crash(x: Foo) -> Foo { | ||
| _________________________^ | ||
LL | | x | ||
LL | | } | ||
| |_^ | ||
= note: ...which again requires processing `Foo`, completing the cycle | ||
note: cycle used when collecting item types in top-level module | ||
--> $DIR/existential-types-with-cycle-error.rs:1:1 | ||
| | ||
LL | / #![feature(existential_type)] | ||
LL | | | ||
LL | | existential type Foo: Fn() -> Foo; | ||
LL | | | ||
... | | ||
LL | | | ||
LL | | } | ||
| |_^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0391`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 1 addition & 23 deletions
24
src/test/ui/existential_types/existential-types-with-cycle-error2.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,8 @@ | ||
error[E0391]: cycle detected when processing `Foo` | ||
error: could not find defining uses | ||
--> $DIR/existential-types-with-cycle-error2.rs:7:1 | ||
| | ||
LL | existential type Foo: Bar<Foo, Item = Foo>; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
note: ...which requires processing `crash`... | ||
--> $DIR/existential-types-with-cycle-error2.rs:10:25 | ||
| | ||
LL | fn crash(x: Foo) -> Foo { | ||
| _________________________^ | ||
LL | | x | ||
LL | | } | ||
| |_^ | ||
= note: ...which again requires processing `Foo`, completing the cycle | ||
note: cycle used when collecting item types in top-level module | ||
--> $DIR/existential-types-with-cycle-error2.rs:1:1 | ||
| | ||
LL | / #![feature(existential_type)] | ||
LL | | | ||
LL | | pub trait Bar<T> { | ||
LL | | type Item; | ||
... | | ||
LL | | | ||
LL | | } | ||
| |_^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0391`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 1 addition & 20 deletions
21
src/test/ui/existential_types/no_inferrable_concrete_type.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,8 @@ | ||
error[E0391]: cycle detected when processing `Foo` | ||
error: could not find defining uses | ||
--> $DIR/no_inferrable_concrete_type.rs:6:1 | ||
| | ||
LL | existential type Foo: Copy; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
note: ...which requires processing `bar`... | ||
--> $DIR/no_inferrable_concrete_type.rs:9:23 | ||
| | ||
LL | fn bar(x: Foo) -> Foo { x } | ||
| ^^^^^ | ||
= note: ...which again requires processing `Foo`, completing the cycle | ||
note: cycle used when collecting item types in top-level module | ||
--> $DIR/no_inferrable_concrete_type.rs:4:1 | ||
| | ||
LL | / #![feature(existential_type)] | ||
LL | | | ||
LL | | existential type Foo: Copy; | ||
LL | | | ||
... | | ||
LL | | let _: Foo = std::mem::transmute(0u8); | ||
LL | | } | ||
| |_^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0391`. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.