about summary refs log tree commit diff
path: root/src/test/ui/cast
AgeCommit message (Collapse)AuthorLines
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.
2018-12-25Remove licensesMark Rousskov-91/+11
2018-08-14Merged migrated compile-fail tests and ui tests. Fixes #46841.David Wood-0/+234