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

Account for trait alias when looking for defid #4430

Merged
merged 2 commits into from
Aug 26, 2019

Conversation

tesuji
Copy link
Contributor

@tesuji tesuji commented Aug 22, 2019

I hit the crash on the expect call when running clippy on rustc libcore.
Hopefully this will fix it.
changelog: none

@tesuji tesuji force-pushed the defid_trait_alias branch 2 times, most recently from cec16d6 to 89b888d Compare August 23, 2019 11:25
@phansch phansch added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Aug 24, 2019
@phansch
Copy link
Member

phansch commented Aug 24, 2019

Do you think it would be a lot of work to extract a minimal test case? It would be nice to have a test for this

@tesuji
Copy link
Contributor Author

tesuji commented Aug 25, 2019

Do clippy have a way to log the file it is processing? I think this would help extract a testcase.
Or just merge this and test again on libcore I think.

@ghost
Copy link

ghost commented Aug 25, 2019

Just apply the changes locally and test with your local copy.

cd "<project_dir>"
cargo build --manifest-path "<local_clippy>/Cargo.toml"
cargo run --manifest-path "<local_clippy>/Cargo.toml" --bin cargo-clippy

@tesuji tesuji force-pushed the defid_trait_alias branch from 89b888d to dac2509 Compare August 25, 2019 13:41
@tesuji
Copy link
Contributor Author

tesuji commented Aug 25, 2019

Thanks @mikerite. I cannot manage to find a testcase for this but binary
buillt with this patch fixes the crash when running on libcore.

@ghost
Copy link

ghost commented Aug 25, 2019

I can confirm that the bug is where we look up the AsRef or AsMut traits. libcore is probably one of the few crates where these don't exist. It wouldn't be easy to make a test case for this. You'd have to build something without using core.

--

Edit: Realized my fix was the same as this pull request.

@flip1995
Copy link
Member

flip1995 commented Aug 26, 2019

#![no_core] isn't a thing yet, is it?

EDIT: rust-lang/rust#29639 Nope It is! 🎉

// ignore-windows
// ignore-macos

#![feature(no_core, lang_items, start)]
#![no_core]

#[link(name = "c")]
extern {}

#[lang = "sized"]
pub trait Sized {}
#[lang = "copy"]
pub trait Copy {}
#[lang = "freeze"]
pub unsafe trait Freeze {}

#[lang = "start"]
#[start]
fn start(_argc: isize, _argv: *const *const u8) -> isize {
    0
}

This compiles and does not have libcore. Can you try to create a test case with this?

@flip1995
Copy link
Member

flip1995 commented Aug 26, 2019

The example code, that I posted, already passes the test, without your change. You have to adapt this code, so that it matches

if_chain! {
if let hir::ImplItemKind::Method(ref sig, id) = impl_item.node;
if let Some(first_arg) = iter_input_pats(&sig.decl, cx.tcx.hir().body(id)).next();
if let hir::ItemKind::Impl(_, _, _, _, None, _, _) = item.node;
let method_def_id = cx.tcx.hir().local_def_id(impl_item.hir_id);
let method_sig = cx.tcx.fn_sig(method_def_id);
let method_sig = cx.tcx.erase_late_bound_regions(&method_sig);
let first_arg_ty = &method_sig.inputs().iter().next();
// check conventions w.r.t. conversion method names and predicates
if let Some(first_arg_ty) = first_arg_ty;

if let Some((ref conv, self_kinds)) = &CONVENTIONS
.iter()
.find(|(ref conv, _)| conv.check(&name))
{
if !self_kinds.iter().any(|k| k.matches(cx, ty, first_arg_ty)) {

Where the last line in the second snippet should fail at the matches call


IIUC This should just be an impl of e.g. a to_ method, taking &self as it's first argument.

@flip1995
Copy link
Member

I added it myself. Thanks for your fix!

@bors r+

@bors
Copy link
Contributor

bors commented Aug 26, 2019

📌 Commit 475a142 has been approved by flip1995

bors added a commit that referenced this pull request Aug 26, 2019
Account for trait alias when looking for defid

I hit the crash on the `expect` call when running clippy on rustc libcore.
Hopefully this will fix it.
changelog: none
@bors
Copy link
Contributor

bors commented Aug 26, 2019

⌛ Testing commit 475a142 with merge 9c148c8...

LL | pub fn as_ref(self) -> &'static str {
| ^^^^
|
= note: `-D clippy::wrong-self-convention` implied by `-D warnings`
Copy link
Member

@flip1995 flip1995 Aug 26, 2019

Choose a reason for hiding this comment

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

Fun fact: If you add the & to self in a no_core crate, rustc complains that

error[E0658]: `&A` cannot be used as the type of `self` without the `arbitrary_self_types` feature
  --> tests/ui/def_id_nocore.rs:26:19
   |
26 |     pub fn as_ref(&self) -> &'static str {
   |                   ^^^^^
   |
   = note: for more information, see https://github.com/rust-lang/rust/issues/44874
   = help: add `#![feature(arbitrary_self_types)]` to the crate attributes to enable
   = help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)

I don't think, that we need to adapt the warning though 😄

@bors
Copy link
Contributor

bors commented Aug 26, 2019

💔 Test failed - status-appveyor

@flip1995
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Aug 26, 2019

📌 Commit c222e7e has been approved by flip1995

@bors
Copy link
Contributor

bors commented Aug 26, 2019

⌛ Testing commit c222e7e with merge f760088...

bors added a commit that referenced this pull request Aug 26, 2019
Account for trait alias when looking for defid

I hit the crash on the `expect` call when running clippy on rustc libcore.
Hopefully this will fix it.
changelog: none
@bors
Copy link
Contributor

bors commented Aug 26, 2019

☀️ Test successful - checks-travis, status-appveyor
Approved by: flip1995
Pushing f760088 to master...

@bors bors merged commit c222e7e into rust-lang:master Aug 26, 2019
@tesuji tesuji deleted the defid_trait_alias branch August 26, 2019 13:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants