Skip to content

rustc: fix ICE regarding struct variants in enums #15786

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

Closed
wants to merge 1 commit into from

Conversation

yorkie
Copy link
Contributor

@yorkie yorkie commented Jul 18, 2014

Hi, this is inspired by the output of previous rustc output:

note: the compiler hit an unexpected failure path. this is a bug.
note: we would appreciate a bug report: http://doc.rust-lang.org/complement-bugreport.html
note: run with `RUST_BACKTRACE=1` for a backtrace
task 'rustc' failed at 'assertion failed: fields.len() > 0', /Users/rustbuild/src/rust-buildbot/slave/nightly-mac/build/src/librustc/middle/ty.rs:3657

This unexpected failure will scare off someone IMO, right, then such that a better error print would be better :p

@yorkie
Copy link
Contributor Author

yorkie commented Jul 18, 2014

and someone wrote this:

enum {
  Foo {}
}

really maybe a usual case :(

assert!(fields.len() > 0);

if fields.len() == 0 {
cx.sess.fatal("you don't specify any field, use simple defination")
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you mean "definition" instead of "defination"?

Copy link
Contributor

Choose a reason for hiding this comment

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

By the way... I think it would be more informative for the reader if you followed the style of the error a couple of lines below (enum_variants: all fields in struct must have a name). What about enum_variants: struct variant must have at least one field?

EDIT: ignore this comment and read what huonw wrote below

Copy link
Contributor

Choose a reason for hiding this comment

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

Additionaly, I think you will need to use cx.sess.span_err(ast_variant.span, "error message") instead of cx.sess.fatal.

EDIT: ignore this comment and read what huonw wrote below

Copy link
Member

Choose a reason for hiding this comment

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

I recommend cx.sess.span_fatal(ast_variant.span, "struct variants must have at least one field");.

I think it would be invalid to continue compilation at this point, so one must use fatal rather than err (which just prints an error message, and causes compilation to stop at some future point, but not immediately). Also, the enum_variants: ... warning in the error message below is only appropriate there, since that one is a bug (the parser shouldn't let through any UnnamedFields in a struct variant), while this is a user-facing message, and users don't care about the compiler-internals function which emits the error message.

@aochagavia
Copy link
Contributor

For anyone reviewing this, an example of the ICE is available in the playpen

@yorkie A better title for the pull request would be "Fix ICE regarding struct variants in enums". ICE means internal compiler error.

@huonw
Copy link
Member

huonw commented Jul 19, 2014

Also, this should have a test, add a file to src/test/compile-fail, e.g. struct-variant-no-fields.rs:

// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

enum Foo {
    Bar { } //~ ERROR struct variant must have at least one field
}

fn main() {}

See https://github.com/rust-lang/rust/wiki/Note-testsuite#language-and-compiler-tests for more details.

@@ -3665,8 +3665,10 @@ impl VariantInfo {
ast::StructVariantKind(ref struct_def) => {

let fields: &[StructField] = struct_def.fields.as_slice();

assert!(fields.len() > 0);

Copy link
Member

Choose a reason for hiding this comment

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

The travis build failed in make tidy because this line has trailing whitespace, can you remove the whitespace?

@aochagavia
Copy link
Contributor

@huonw don't forget the #![feature(struct_variant)]... In the playpen it doesn't compile without it.

@yorkie
Copy link
Contributor Author

yorkie commented Jul 19, 2014

thanks guys so much, @aochagavia and @huonw.
plz review my new commit :p

@yorkie yorkie changed the title rustc: better error prints rustc: fix ICE regarding struct variants in enums Jul 19, 2014
@yorkie
Copy link
Contributor Author

yorkie commented Jul 19, 2014

and plus, i have changed the title of my pr/commit to: rustc: fix ICE regarding struct variants in enums

@sfackler
Copy link
Member

There's a tidy issue: https://travis-ci.org/rust-lang/rust/jobs/30350054

/home/travis/build/rust-lang/rust/src/librustc/middle/ty.rs:3728: line longer than 100 chars

@yorkie
Copy link
Contributor Author

yorkie commented Jul 19, 2014

done :p

#![feature(struct_variant)]

enum Foo {
Bar { } //~ ERROR struct variant must have at least one field
Copy link
Member

Choose a reason for hiding this comment

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

The error above has "struct variants must have" whereas this error says "struct variant must have"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi, @alexcrichton, i have changed this in ty.rs, now LGTY?

@yorkie
Copy link
Contributor Author

yorkie commented Jul 21, 2014

hi, the travis is green :)

@luqmana
Copy link
Member

luqmana commented Jul 21, 2014

@yorkie I don't think this should be done in librustc but rather libsyntax. What's the point of accepting it in the parser if we're just gonna error out later. The relevant bit in libsyntax would be parse_enum_def in libsyntax/parse/parser.rs. I think the check would make more sense there.

@yorkie
Copy link
Contributor Author

yorkie commented Jul 25, 2014

i dont 100% agreed on this, because this error breaks here, do we really need to move this patch to the parsing phase? i need more from u

@alexcrichton
Copy link
Member

Closing due to inactivity, but feel free to reopen with comments addressed!

bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 13, 2023
fix: Fix VS Code detection for Insiders version

Addresses rust-lang/rust-analyzer#15784.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants