Skip to content

Forbid casting to/from a pointer of unknown kind - #45735

Merged
bors merged 1 commit into
rust-lang:masterfrom
tirr-c:issue-45730
Nov 8, 2017
Merged

Forbid casting to/from a pointer of unknown kind#45735
bors merged 1 commit into
rust-lang:masterfrom
tirr-c:issue-45730

Conversation

@tirr-c

Copy link
Copy Markdown
Contributor

Fixes#45730.

Before, it ICE'd when pointer_kind encountered TyInfer.

@rust-highfive

Copy link
Copy Markdown
Contributor

r? @pnkfelix

(rust_highfive has picked a reviewer for you, use r? to override)

Comment threadsrc/librustc_typeck/check/cast.rs Outdated

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Does this case occur only by lack of type information? I'm not pretty sure.

@shepmastershepmaster added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 3, 2017
@arielb1

Copy link
Copy Markdown
Contributor

@tirr-c

It's not a good idea to assume things when encountering a TyInfer - trait bounds etc. can make the TyInfer equal to every type, so this could accidentally allow thin-to-fat pointer casts if the _ is later made to be equal fmt::Debug, as in:

use std::fmt;fnmain(){let x:*const_ = 0as*const_;// we don't want to allow this if...let y:Option<*const fmt::Debug> = Some(x)as_;// ^ later, we have x = *const fmt::Debug}fnnot_ok(){let x = 0as*consti32as*const_as*mut_;//~ ERROR ?}

Instead, you should make fn pointer_kind return an Option, and emit an error of we are casting to an unknown pointer kind..

@tirr-ctirr-c changed the title Assume thin pointer when pointer type cannot be inferredForbid casting to/from a pointer of unknown kindNov 6, 2017
@tirr-c

Copy link
Copy Markdown
ContributorAuthor

Added a new error code for casting with unknown pointer kind.

r? @arielb1

@rust-highfiverust-highfive assigned arielb1 and unassigned pnkfelixNov 6, 2017
Comment threadsrc/librustc_typeck/check/cast.rs Outdated

@arielb1arielb1Nov 6, 2017

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This forbids casting an *const _ to an *const u32, please move the check after the following if to avoid breaking user code.

@tirr-c

Copy link
Copy Markdown
ContributorAuthor

Updated, but I couldn't trigger the *const _ as *const u32 case. What would the code look like?

@arielb1

arielb1 commented Nov 6, 2017

Copy link
Copy Markdown
Contributor

@tirr-c

Can't you have 0 as *const u32 as *const _ as *mut u32? Or does that go through a separate code-path?

@arielb1

Copy link
Copy Markdown
Contributor

@bors r+

@bors

bors commented Nov 6, 2017

Copy link
Copy Markdown
Collaborator

📌 Commit 99ada04 has been approved by arielb1

@tirr-c

Copy link
Copy Markdown
ContributorAuthor

It just compiles. I'm not sure when inference occurs, but maybe *const _ there is inferred as *const u32?

@kennytmkennytm added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 7, 2017
@bors

bors commented Nov 8, 2017

Copy link
Copy Markdown
Collaborator

⌛ Testing commit 99ada04 with merge 49bee9d...

bors added a commit that referenced this pull request Nov 8, 2017
Forbid casting to/from a pointer of unknown kind
Fixes#45730.
Before, it ICE'd when `pointer_kind` encountered `TyInfer`.
@bors

bors commented Nov 8, 2017

Copy link
Copy Markdown
Collaborator

☀️ Test successful - status-appveyor, status-travis
Approved by: arielb1
Pushing 49bee9d to master...

@bors
bors merged commit 99ada04 into rust-lang:masterNov 8, 2017
@tirr-c
tirr-c deleted the issue-45730 branch November 19, 2017 10:31
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

@tirr-c@rust-highfive@arielb1@bors@kennytm@pnkfelix@shepmaster