about summary refs log tree commit diff
path: root/src/test/ui/cast
AgeCommit message (Collapse)AuthorLines
2023-01-11Move /src/test to /testsAlbert Larsan-1172/+0
2022-11-20Auto merge of #103390 - compiler-errors:metadata-mod-regions, r=eholkbors-0/+17
Check fat pointer metadata compatibility modulo regions Regions don't really mean anything anyways during hir typeck. If this `erase_regions` makes anyone nervous, it's probably equally valid to just equate the types using a type relation, but regardless we should _not_ be using strict type equality while region variables are present. Fixes #103384
2022-10-22Check fat pointer metadata compatibility modulo regionsMichael Goulet-0/+17
2022-10-22Bless testsDeadbeef-1/+1
2022-10-22Stabilize arbitrary_enum_discriminant, take 2Deadbeef-2/+0
2022-10-07make const_err a hard errorRalf Jung-5/+1
2022-07-01Only label place where type is needed if span is meaningfulMichael Goulet-2/+0
2022-06-21Move some tests to more reasonable directoriesCaio-0/+13
2022-06-16diagnostics: fix trailing spaceklensy-1/+1
2022-06-05restore a testRalf Jung-1/+2
2022-05-13Extend ptr::null and null_mut to all thin (including extern) typesSimon Sapin-1/+1
Fixes https://github.com/rust-lang/rust/issues/93959 This change was accepted in https://rust-lang.github.io/rfcs/2580-ptr-meta.html Note that this changes the signature of **stable** functions. The change should be backward-compatible, but it is **insta-stable** since it cannot (easily, at all?) be made available only through a `#![feature(…)]` opt-in. The RFC also proposed the same change for `NonNull::dangling`, which makes sense it terms of its signature but not in terms of its implementation. `dangling` uses `align_of()` as an address. But what `align_of()` should be for extern types or whether it should be allowed at all remains an open question. This commit depends on https://github.com/rust-lang/rust/pull/93977, which is not yet part of the bootstrap compiler. So `#[cfg]` is used to only apply the change in stage 1+. As far a I know bounds cannot be made conditional with `#[cfg]`, so the entire functions are duplicated. This is unfortunate but temporary. Since this duplication makes it less obvious in the diff, the new definitions differ in: * More permissive bounds (`Thin` instead of implied `Sized`) * Different implementation * Having `rustc_allow_const_fn_unstable(ptr_metadata)`
2022-04-16Implementation for 65853Jack Huey-5/+41
This attempts to bring better error messages to invalid method calls, by applying some heuristics to identify common mistakes. The algorithm is inspired by Levenshtein distance and longest common sub-sequence. In essence, we treat the types of the function, and the types of the arguments you provided as two "words" and compute the edits to get from one to the other. We then modify that algorithm to detect 4 cases: - A function input is missing - An extra argument was provided - The type of an argument is straight up invalid - Two arguments have been swapped - A subset of the arguments have been shuffled (We detect the last two as separate cases so that we can detect two swaps, instead of 4 parameters permuted.) It helps to understand this argument by paying special attention to terminology: "inputs" refers to the inputs being *expected* by the function, and "arguments" refers to what has been provided at the call site. The basic sketch of the algorithm is as follows: - Construct a boolean grid, with a row for each argument, and a column for each input. The cell [i, j] is true if the i'th argument could satisfy the j'th input. - If we find an argument that could satisfy no inputs, provided for an input that can't be satisfied by any other argument, we consider this an "invalid type". - Extra arguments are those that can't satisfy any input, provided for an input that *could* be satisfied by another argument. - Missing inputs are inputs that can't be satisfied by any argument, where the provided argument could satisfy another input - Swapped / Permuted arguments are identified with a cycle detection algorithm. As each issue is found, we remove the relevant inputs / arguments and check for more issues. If we find no issues, we match up any "valid" arguments, and start again. Note that there's a lot of extra complexity: - We try to stay efficient on the happy path, only computing the diagonal until we find a problem, and then filling in the rest of the matrix. - Closure arguments are wrapped in a tuple and need to be unwrapped - We need to resolve closure types after the rest, to allow the most specific type constraints - We need to handle imported C functions that might be variadic in their inputs. I tried to document a lot of this in comments in the code and keep the naming clear.
2022-04-10use find_ancestor_inside to get right span in CastCheckMichael Goulet-0/+23
2022-03-09better suggestion for int to wide ptr castMichael Goulet-9/+38
2022-02-11Revert "Auto merge of #92007 - oli-obk:lazy_tait2, r=nikomatsakis"Oli Scherer-13/+5
This reverts commit e7cc3bddbe0d0e374d05e7003e662bba1742dbae, reversing changes made to 734368a200904ef9c21db86c595dc04263c87be0.
2022-02-02Lazily resolve type-alias-impl-trait defining usesOli Scherer-5/+13
by using an opaque type obligation to bubble up comparisons between opaque types and other types Also uses proper obligation causes so that the body id works, because out of some reason nll uses body ids for logic instead of just diagnostics.
2021-12-08Rollup merge of #91245 - cameron1024:suggest-i32-u32-char-cast, r=nagisaMatthias Krüger-0/+62
suggest casting between i/u32 and char As discussed in https://github.com/rust-lang/rust/issues/91063 , this adds a suggestion for converting between i32/u32 <-> char with `as`, and a short explanation for why this is safe
2021-12-01Disallow non-c-like but "fieldless" ADTs from being casted to integer...Gary Guo-0/+22
... if they use arbitrary enum discriminant. Code like ```rust enum Enum { Foo = 1, Bar(), Baz{} } ``` seems to be unintentionally allowed so we couldn't disallow them now, but we could disallow them if arbitrary enum discriminant is used before 1.56 hits stable.
2021-11-28suggest cast char -> intcameron-0/+62
2021-11-20Fixup test outputsMichael Goulet-1/+1
2021-11-14Move some tests to more reasonable directoriesCaio-0/+6
2021-11-06Move some tests to more reasonable directoriesCaio-0/+206
2021-10-04Fix suggestion to borrow when casting from pointer to referenceFabian Wolff-0/+35
2021-08-11Modify structured suggestion outputEsteban Küber-2/+2
* On suggestions that include deletions, use a diff inspired output format * When suggesting addition, use `+` as underline * Color highlight modified span
2021-07-30Auto merge of #85971 - FabianWolff:issue-85586, r=davidtwcobors-0/+21
Use more precise span for E0282 in cast expressions This pull request fixes #85586. The example code given there: ```rust fn main() { let a = [1, 2, 3].iter().sum(); let b = (a + 1) as usize; } ``` currently produces ``` error[E0282]: type annotations needed --> issue-85586.rs:3:13 | 3 | let b = (a + 1) as usize; | ^^^^^^^^^^^^^^^^ cannot infer type | = note: type must be known at this point error: aborting due to previous error ``` even though the type of the entire cast expression quite clearly should be `usize`. The error is in the cast's left-hand side, which is made explicit by the changes in this PR: ``` error[E0282]: type annotations needed --> issue-85586.rs:3:13 | 3 | let b = (a + 1) as usize; | ^^^^^^^ cannot infer type | = note: type must be known at this point error: aborting due to previous error ```
2021-07-10remove const_raw_ptr_to_usize_cast featureRalf Jung-100/+0
2021-07-03Remove the deprecated `core::raw` and `std::raw` module.Charles Lew-13/+4
2021-06-10Use more precise span for E0282 in cast expressionsFabian Wolff-0/+21
2021-05-28Auto merge of #84968 - FabianWolff:master, r=estebankbors-2/+2
Fix incorrect suggestions for E0605 Fixes #84598. Here is a simplified version of the problem presented in issue #84598: ```Rust #![allow(unused_variables)] #![allow(dead_code)] trait T { fn t(&self) -> i32; } unsafe fn foo(t: *mut dyn T) { (t as &dyn T).t(); } fn main() {} ``` The current output is: ``` error[E0605]: non-primitive cast: `*mut (dyn T + 'static)` as `&dyn T` --> src/main.rs:7:5 | 7 | (t as &dyn T).t(); | ^^^^^^^^^^^^^ invalid cast | help: borrow the value for the cast to be valid | 7 | (&t as &dyn T).t(); | ^ ``` This is incorrect, though: The cast will _not_ be valid when writing `&t` instead of `t`: ``` error[E0277]: the trait bound `*mut (dyn T + 'static): T` is not satisfied --> t4.rs:7:6 | 7 | (&t as &dyn T).t(); | ^^ the trait `T` is not implemented for `*mut (dyn T + 'static)` | = note: required for the cast to the object type `dyn T` ``` The correct suggestion is `&*t`, which I have implemented in this pull request. Of course, this suggestion will always require an unsafe block, but arguably, that's what the user really wants if they're trying to cast a pointer to a reference. In any case, claiming that the cast will be valid after implementing the suggestion is overly optimistic, as the coercion logic doesn't seem to resolve all nested obligations, i.e. the cast may still be invalid after implementing the suggestion. I have therefore rephrased the suggestion slightly ("consider borrowing the value" instead of "borrow the value for the cast to be valid"). Additionally, I have fixed another incorrect suggestion not mentioned in #84598, which relates to casting immutable references to mutable ones: ```rust fn main() { let mut x = 0; let m = &x as &mut i32; } ``` currently leads to ``` error[E0605]: non-primitive cast: `&i32` as `&mut i32` --> t5.rs:3:13 | 3 | let m = &x as &mut i32; | ^^^^^^^^^^^^^^ invalid cast | help: borrow the value for the cast to be valid | 3 | let m = &mut &x as &mut i32; | ^^^^ ``` which is obviously incorrect: ``` error[E0596]: cannot borrow data in a `&` reference as mutable --> t5.rs:3:13 | 3 | let m = &mut &x as &mut i32; | ^^^^^^^ cannot borrow as mutable ``` I've changed the suggestion to a note explaining the problem: ``` error[E0605]: non-primitive cast: `&i32` as `&mut i32` --> t5.rs:3:13 | 3 | let m = &x as &mut i32; | ^^^^^^^^^^^^^^ invalid cast | note: this reference is immutable --> t5.rs:3:13 | 3 | let m = &x as &mut i32; | ^^ note: trying to cast to a mutable reference type --> t5.rs:3:19 | 3 | let m = &x as &mut i32; | ^^^^^^^^ ``` In this example, it would have been even nicer to suggest replacing `&x` with `&mut x`, but this would be much more complex because we would have to take apart the expression to be cast (currently, we only look at its type), and `&x` could be stored in a variable, where such a suggestion would not even be directly applicable: ```rust fn main() { let mut x = 0; let r = &x; let m = r as &mut i32; } ``` My solution covers this case, too.
2021-05-24Remove stray .stderr filesLeSeulArtichaut-58/+0
2021-05-21Check for ptr-to-int casts in const functions in THIR unsafeckLeSeulArtichaut-13/+88
2021-05-15Warn about unused pub fields in non-pub structsFabian Wolff-2/+4
2021-05-06Fix incorrect suggestions for E0605Fabian Wolff-2/+2
2021-04-25make sure raw ptr casts in 'const' context are unsafeRalf Jung-25/+76
2021-04-18Auto merge of #84207 - SimonSapin:deprecate-core-raw, r=dtolnaybors-1/+3
Deprecate the core::raw / std::raw module It only contains the `TraitObject` struct which exposes components of wide pointer. Pointer metadata APIs are designed to replace this: https://github.com/rust-lang/rust/issues/81513
2021-04-15Add regression testGiacomo Stevanato-0/+55
2021-04-15Allow use of deprecated std::raw in a test for that featureSimon Sapin-1/+3
2021-01-28move a bunch of testsBastian Kauschke-0/+164
2020-12-31Move cast-related testsYuki Okushi-0/+361
2020-09-02pretty: trim paths of unique symbolsDan Aloni-2/+2
If a symbol name can only be imported from one place for a type, and as long as it was not glob-imported anywhere in the current crate, we can trim its printed path and print only the name. This has wide implications on error messages with types, for example, shortening `std::vec::Vec` to just `Vec`, as long as there is no other `Vec` importable anywhere. This adds a new '-Z trim-diagnostic-paths=false' option to control this feature. On the good path, with no diagnosis printed, we should try to avoid issuing this query, so we need to prevent trimmed_def_paths query on several cases. This change also relies on a previous commit that differentiates between `Debug` and `Display` on various rustc types, where the latter is trimmed and presented to the user and the former is not.
2020-06-15Tweak "non-primitive cast" errorEsteban Küber-13/+5
- Suggest borrowing expression if it would allow cast to work. - Suggest using `<Type>::from(<expr>)` when appropriate. - Minor tweak to `;` typo suggestion. Partily address #47136.
2020-02-09--bless --compare-mode=nllMatthias Prechtl-2/+2
2019-12-18Start generating AddressOf rvalues in MIRMatthew Jasper-4/+5
`hir::BorrowKind::Raw` borrows and casting a reference to a raw pointer no longer do a reborrow followed by a cast. Instead we dereference and take the address.
2019-07-09normalize use of backticks in compiler messages for libsyntax/feature_gateSamy Kacimi-2/+2
https://github.com/rust-lang/rust/issues/60532
2019-05-29Update ui test suite to use dynmemoryruins-8/+8
2019-04-11Reword tracking issue noteEsteban Küber-2/+2
2019-04-10Tweak unstable diagnostic outputEsteban Küber-2/+4
2019-03-11Update testsVadim Petrochenkov-8/+8
2019-01-21Declare some unconst operations as unsafe in const fnOliver Scherer-8/+12
2019-01-09provide suggestion for invalid boolean castAndy Russell-9/+22
Also, don't suggest comparing to zero for non-numeric expressions.