-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Erroneous compilation failure with associated constant #54822
Comments
Okay, so I came here from the TWIR CFP post, and I thought I would take a stab at this. This is the first time I have ever dug deeply into the internals of rustc itself, so take everything I'm about to say with a grain of salt. I don't have a real understanding of most of the rustc internals yet, so I approached this bug purely experimentally. It may be that what I'm saying is very obvious to somebody who already understands how typechecking and trait resolution work, so I apologize if I'm wasting everyone's time. So I started by playing with the failing example, and there are lots of ways here to get some obviously wrong output from rustc:
So I started by looking into why in the world the compiler would output After some experimentation, I was able to make all of the examples here compile by changing:
to
to put the where clause for
Through experimentation I'm able to fix So I don't think I'm actually fixing the bug here, because I don't understand things well enough yet to know what the right thing to do is, and there's a huge chance I have a lot of this wrong. I'm including all of this information simply in case it helps somebody more knowledgable actually fix it. I hope this is helpful, if it's not helpful then feel free to ignore. |
I'm no expert either, but just from the test failures it seems that @kyren's fix is actually correct. Test failures after applying the fix: https://gist.github.com/vickenty/1ea9ea56c97e5e4902f6158a5f2410f1
In trait Anything<'a: 'b, 'b> {
const AC: Option<&'b str>;
}
struct FailStruct2 { }
impl<'a: 'b, 'b> Anything<'a, 'b> for FailStruct2 {
const AC: Option<&'a str> = None;
//~^ ERROR: mismatched types
} The error message appears similar to the one on this issue: Since impls can use more generic lifetimes (as was recently discussed), it would make sense if this code was allowed as well. |
Oh, I should have paid more attention to what the tests actually did, thank you for looking at that! |
Fix rust-lang#54822 and associated faulty tests Type checking associated constants can require trait bounds, but an empty parameter environment was provided to the trait solver. Providing an appropriate parameter environment seems to fix rust-lang#54822 and also make one of the cases in src/test/ui/nll/trait-associated-constant.rs that should compile successfully do so. It also (slightly) improves the error message in src/test/ui/associated-const/associated-const-generic-obligations.rs
Rollup of 16 pull requests Successful merges: - #58829 (librustc_interface: Update scoped-tls to 1.0) - #58876 (Parse lifetimes that start with a number and give specific error) - #58908 (Update rand version) - #58998 (Fix documentation of from_ne_bytes and from_le_bytes) - #59056 (Use lifetime contravariance to elide more lifetimes in core+alloc+std) - #59057 (Standardize `Range*` documentation) - #59080 (Fix incorrect links in librustc_codegen_llvm documentation) - #59083 (Fix #54822 and associated faulty tests) - #59093 (Remove precompute_in_scope_traits_hashes) - #59101 (Reduces Code Repetitions like `!n >> amt`) - #59121 (impl FromIterator for Result: Use assert_eq! instead of assert!) - #59124 (Replace assert with assert_eq) - #59129 (Visit impl Trait for dead_code lint) - #59130 (Note that NonNull does not launder shared references for mutation) - #59132 (ignore higher-ranked object bound conditions created by WF) - #59138 (Simplify Iterator::{min, max}) Failed merges: r? @ghost
@kyren I saw your PR got merged; thanks again for your work digging into this and for the fix! |
No problem, glad to help 👍 It was more straightforward than I thought it was going to be. |
EDIT: To anyone coming to this from the This Week in Rust CFP: This is currently an unsolved issue, so the first step to tackling this is to figure out where the bug is! That alone would be a huge help. If you want to implement a fix, that'd be great too :)
=====
The following program should compile on stable, but doesn't:
It fails to compile with the following error:
Note that if we amend as either of the following, it works (credit to @ezrosent for figuring this out):
The text was updated successfully, but these errors were encountered: