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

Add a crate keyword and allow it to link external crates #12017

Merged
merged 3 commits into from
Feb 14, 2014

Conversation

flaper87
Copy link
Contributor

@flaper87 flaper87 commented Feb 3, 2014

The first setp for #9880 is to add a new crate keyword. This PR does exactly that. I took a chance to refactor parse_item_foreign_mod and I broke it down into 2 separate methods to isolate each feature.

The next step will be to push a new stage0 snapshot and then get rid of all extern mod around the code.

@flaper87
Copy link
Contributor Author

flaper87 commented Feb 3, 2014

@huonw r?

This is a first iteration for this change! It seems pretty complete, although I would appreciate a detailed review on both methods since the re-factor could have introduced some regressions.

@thestinger
Copy link
Contributor

Was it decided to go with crate in a meeting? I'm opposed to changing this and there hasn't been a response to my comment on the issue. The prevailing opinion may be that extern mod is confusing but it is not the only opinion and no one has stated why calling it crate (Rust-specific term unknown to outsiders) will be better than calling it an externally defined module.

A C library is certainly not considered a crate, and the plan for supporting libclang-based symbol resolution for native libraries was to add an attribute to an extern mod statement providing the header name.

@alexcrichton
Copy link
Member

@thestinger
Copy link
Contributor

Ah, I wasn't aware it had been discussed - nevermind then.

@@ -4608,12 +4587,33 @@ impl Parser {
visibility,
maybe_append(attrs, extra_attrs));
return IoviItem(item);
} else {
} else if self.eat_keyword(keywords::Crate) {
Copy link
Member

Choose a reason for hiding this comment

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

I think this might be better to put this before .parse_opt_abis. i.e.

if self.is_keyword(keywords::Extern) { 
    let next_is_mod = self.eat_keyword(keywords::Mod);
    if self.eat_keyword(keywords::Crate) || next_is_mod { 
        if next_is_mod { self.span_note(...) }
        self.parse_item_extern_crate(lo, visibility, attrs);
    }

    let opt_abis = self.parse_opt_abis();
    if self.eat_keyword(keywords::Fn) { ... }
    ...
}

This means that extern "ABI" crate blah; is an "unexpected token crate" error, which seems reasonable. Although the keeping it the way it is now seems ok-ish.

Also, as I hinted in then code above above, wouldn't this be better as (something equivalent to) self.eat_keyword(keywords::Crate) || self.eat_keyword(keywords::Mod) rather than duplicating the logic below?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As it is now, it still raises the "ABI" error but I think we should get rid of that. I'll refactor the code based on your suggestions.

@flaper87
Copy link
Contributor Author

flaper87 commented Feb 5, 2014

This requiers to change all usages of crate in the code, I'll push a new version of this patch.

return self.parse_item_foreign_mod(lo, opt_abis, visibility, attrs);
}

if opt_abis.is_some() {
Copy link
Member

Choose a reason for hiding this comment

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

Don't we need the "expected fnor{" error here? (i.e. replace this if with just self.span_fatal(self.span, format!("expected ... but found {}", self.this_token_to_str()).)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The else if self.token should handle extern { I'll add an else clause with the error you suggested or re-write this if entirely.

Copy link
Member

Choose a reason for hiding this comment

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

No, I'm talking about the case when it's not a { or fn (i.e. neither case is triggered above): test it with extern blah;. It should emit an error, but it won't.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And yet I again, I should stop doing so many things at the same time. I was talking about the else if self.token not this specific if statement. I agree it should go away, in fact, I already removed it.

@emberian
Copy link
Member

emberian commented Feb 6, 2014

I would like this to be held off until a meeting, where making crate a contextual keyword can be discussed.

@flaper87
Copy link
Contributor Author

flaper87 commented Feb 6, 2014

@cmr I went ahead and worked on this based on @brson comment. The idea is to make it a full keyword for now and then contextualized it later if there's an agreement to do that. #9880 (comment)

@brson
Copy link
Contributor

brson commented Feb 11, 2014

In the meeting today we decided to continue with crate as a full keyword under the assumption that 'crate' is uncommon outside the compiler. We can file followup issues about making crate and in conditional keywords.

Let's please rename uses of crate variables to krate. Sorry this will require a bunch of rewriting.

extern crate is intended to always be used to link to Rust libraries, not C libraries.

@alexcrichton
Copy link
Member

I asked @cmr to remove his r+ because I realized that this still emits a NOTE for extern mod. This needs to emit an error for extern mod. For bootstrapping purposes, it's ok to allow both, but we don't want to litter the build with NOTE everywhere asking us to do something that can't be done just yet.

@flaper87
Copy link
Contributor Author

@alexcrichton Commented the span_err out and I'll un-comment it as soon as the snapshot is up. r?

@alexcrichton
Copy link
Member

Looks like this failed due to a legitimate reason

@flaper87
Copy link
Contributor Author

@alexcrichton damn, I forgot to update the error message. Done: r? p=?

This patch replaces all `crate` usage with `krate` before introducing the
new keyword. This ensures that after introducing the keyword, there
won't be any compilation errors.

krate might not be the most expressive substitution for crate but it's a
very close abbreviation for it. `module` was already used in several
places already.
This patch adds a new keyword `crate` which is intended to replace mod
in the context of `extern mod` as part of the issue rust-lang#9880. The patch
doesn't replace all `extern mod` cases since it is necessary to first
push a new snapshot 0.

The implementation could've been less invasive than this. However I
preferred to take this chance to split the `parse_item_foreign_mod`
method and pull the `extern crate` part out of there, hence the new
method `parse_item_foreign_crate`.
This patch gets rid of ObsoleteExternModAttributesInParens and
ObsoleteNamedExternModule since the replacement of `extern mod` with
`extern crate` avoids those cases and raises different errors. Both have
been around for at least a version which makes this a good moment to get
rid of them.
bors added a commit that referenced this pull request Feb 14, 2014
The first setp for #9880 is to add a new `crate` keyword. This PR does exactly that. I took a chance to refactor `parse_item_foreign_mod` and I broke it down into 2 separate methods to isolate each feature.

The next step will be to push a new stage0 snapshot and then get rid of all `extern mod` around the code.
@bors bors closed this Feb 14, 2014
@bors bors merged commit 5deb3c9 into rust-lang:master Feb 14, 2014
chris-morgan added a commit to chris-morgan/ncurses-rs that referenced this pull request Feb 16, 2014
@flaper87 flaper87 deleted the replace-mod-crate branch March 11, 2014 09:16
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 25, 2022
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.

7 participants