about summary refs log tree commit diff
path: root/src/librustc/traits
AgeCommit message (Collapse)AuthorLines
2018-11-03Added support for trait aliases as bounds.Alexander Regueiro-57/+172
2018-11-03Auto merge of #54383 - mikeyhew:custom-receivers-object-safety, r=nikomatsakisbors-35/+263
Take 2: Implement object-safety and dynamic dispatch for arbitrary_self_types This replaces #50173. Over the months that that PR was open, we made a lot of changes to the way this was going to be implemented, and the long, meandering comment thread and commit history would have been confusing to people reading it in the future. So I decided to package everything up with new, straighforward commits and open a new PR. Here are the main points. Please read the commit messages for details. - To simplify codegen, we only support receivers that have the ABI of a pointer. That means they are builtin pointer types, or newtypes thereof. - We introduce a new trait: `DispatchFromDyn<T>`, similar to `CoerceUnsized<T>`. `DispatchFromDyn` has extra requirements that `CoerceUnsized` does not: when you implement `DispatchFromDyn` for a struct, there cannot be any extra fields besides the field being coerced and `PhantomData` fields. This ensures that the struct's ABI is the same as a pointer. - For a method's receiver (e.g. `self: Rc<Self>`) to be object-safe, it needs to have the following property: - let `DynReceiver` be the receiver when `Self = dyn Trait` - let `ConcreteReceiver` be the receiver when `Self = T`, where `T` is some unknown `Sized` type that implements `Trait`, and is the erased type of the trait object. - `ConcreteReceiver` must implement `DispatchFromDyn<DynReceiver>` In the case of `Rc<Self>`, this requires `Rc<T>: DispatchFromDyn<Rc<dyn Trait>>` These rules are explained more thoroughly in the doc comment on `receiver_is_dispatchable` in object_safety.rs. r? @nikomatsakis and @eddyb cc @arielb1 @cramertj @withoutboats Special thanks to @nikomatsakis for getting me un-stuck when implementing the object-safety checks, and @eddyb for helping with the codegen parts. EDIT 2018-11-01: updated because CoerceSized has been replaced with DispatchFromDyn
2018-11-02Auto merge of #55305 - nikomatsakis:universes-refactor-3, r=scalexmbors-31/+25
universes refactor 3 Some more refactorings from my universe branch. These are getting a bit more "invasive" -- they start to plumb the universe information through the canonicalization process. As of yet though I don't **believe** this branch changes our behavior in any notable way, though I'm marking the branch as `WIP` to give myself a chance to verify this. r? @scalexm
2018-11-01Remove this check for object-safety during selection of trait object candidatesMichael Hewson-11/+0
I don't really understand what it's for, but see the comment here: https://github.com/rust-lang/rust/pull/50173#discussion_r204222336 where arielb1 said > Does this check do anything these days? I think `$0: Trait` is always considered ambiguous and nikomatsakis agreed we may be able to get rid of it
2018-11-01add `U: Trait` to the param env during DispatchFromDyn checkMichael Hewson-27/+40
also updated the doc on `receiver_is_dispatchable` to reflect current state of the implementation
2018-11-01Add layout sanity checks in object safetyMichael Hewson-0/+100
If object-safety checks succeed for a receiver type, make sure the receiver’s abi is a) a Scalar, when Self = () b) a ScalarPair, when Self = dyn Trait
2018-11-01Replace UncoeribleReceiver error message with UndispatchableReceiverMichael Hewson-5/+5
2018-11-01Replace CoerceSized trait with DispatchFromDynMichael Hewson-30/+32
Rename `CoerceSized` to `DispatchFromDyn`, and reverse the direction so that, for example, you write ``` impl<T: Unsize<U>, U> DispatchFromDyn<*const U> for *const T {} ``` instead of ``` impl<T: Unsize<U>, U> DispatchFromDyn<*const T> for *const U {} ``` this way the trait is really just a subset of `CoerceUnsized`. The checks in object_safety.rs are updated for the new trait, and some documentation and method names in there are updated for the new trait name — e.g. `receiver_is_coercible` is now called `receiver_is_dispatchable`. Since the trait now works in the opposite direction, some code had to updated here for that too. I did not update the error messages for invalid `CoerceSized` (now `DispatchFromDyn`) implementations, except to find/replace `CoerceSized` with `DispatchFromDyn`. Will ask for suggestions in the PR thread.
2018-11-01Implement the object-safety checks for arbitrary_self_types: part 1Michael Hewson-20/+144
For a trait method to be considered object-safe, the receiver type must satisfy certain properties: first, we need to be able to get the vtable to so we can look up the method, and second, we need to convert the receiver from the version where `Self=dyn Trait`, to the version where `Self=T`, `T` being some unknown, `Sized` type that implements `Trait`. To check that the receiver satisfies those properties, we use the following query: forall (U) { if (Self: Unsize<U>) { Receiver[Self => U]: CoerceSized<Receiver> } } where `Receiver` is the receiver type of the method (e.g. `Rc<Self>`), and `Receiver[Self => U]` is the receiver type where `Self = U`, e.g. `Rc<U>`. forall queries like this aren’t implemented in the trait system yet, so for now we are using a bit of a hack — see the code for explanation.
2018-11-01move some code around to avoid query cyclesMichael Hewson-12/+11
2018-11-01Make all object-safety methods require a global TyCtxtMichael Hewson-3/+4
2018-11-01Rollup merge of #55504 - ljedrz:simpler_repeated_elements, r=oli-obkPietro Albini-5/+1
Use vec![x; n] instead of iter::repeat(x).take(n).collect() It's shorter and possibly easier to optimize.
2018-10-30Use vec![x; n] instead of iter::repeat(x).take(n).collect()ljedrz-5/+1
2018-10-30Make `process_obligations`' computation of `completed` optional.Nicholas Nethercote-3/+4
It's only used in tests. This reduces instruction counts on several benchmarks by 0.5--1%.
2018-10-29Take advantage of impl Iterator in (transitive/elaborate)_boundsljedrz-4/+3
2018-10-28Choose predicates without inference variables over those with themAaron Hill-9/+33
Fixes #54705 When constructing synthetic auto trait impls, we may come across multiple predicates involving the same type, trait, and substitutions. Since we can only display one of these, we pick the one with the 'most strict' lifetime paramters. This ensures that the impl we render the user is actually valid (that is, a struct matching that impl will actually implement the auto trait in question). This commit exapnds the definition of 'more strict' to take into account inference variables. We always choose a predicate without inference variables over a predicate with inference variables.
2018-10-27select.rs: rustfmtNiko Matsakis-7/+9
2018-10-27apply minimum bounds when checking closure signatureNiko Matsakis-0/+6
Required for test expect-fn-supply-fn.rs to pass; otherwise we have unconstrained inference variables that get inferred to `'empty`.
2018-10-27allow canonicalized regions to carry universe and track max-universeNiko Matsakis-25/+11
But.. we don't really use it for anything right now.
2018-10-26add user_ty.projs support to `AscribeUserType`.Felix S. Klock II-4/+7
2018-10-26Auto merge of #55382 - kennytm:rollup, r=kennytmbors-3/+38
Rollup of 21 pull requests Successful merges: - #54816 (Don't try to promote already promoted out temporaries) - #54824 (Cleanup rustdoc tests with `@!has` and `@!matches`) - #54921 (Add line numbers option to rustdoc) - #55167 (Add a "cheap" mode for `compute_missing_ctors`.) - #55258 (Fix Rustdoc ICE when checking blanket impls) - #55264 (Compile the libstd we distribute with -Ccodegen-unit=1) - #55271 (Unimplement ExactSizeIterator for MIR traversing iterators) - #55292 (Macro diagnostics tweaks) - #55298 (Point at macro definition when no rules expect token) - #55301 (List allowed tokens after macro fragments) - #55302 (Extend the impl_stable_hash_for! macro for miri.) - #55325 (Fix link to macros chapter) - #55343 (rustbuild: fix remap-debuginfo when building a release) - #55346 (Shrink `Statement`.) - #55358 (Remove redundant clone (2)) - #55370 (Update mailmap for estebank) - #55375 (Typo fixes in configure_cmake comments) - #55378 (rustbuild: use configured linker to build boostrap) - #55379 (validity: assert that unions are non-empty) - #55383 (Use `SmallVec` for the queue in `coerce_unsized`.) - #55391 (bootstrap: clean up a few clippy findings)
2018-10-26Rollup merge of #55358 - sinkuu:redundant_clone2, r=estebankkennytm-3/+3
Remove redundant clone (2)
2018-10-26Auto merge of #53821 - oli-obk:sanity_query, r=RalfJungbors-27/+11
Report const eval error inside the query Functional changes: We no longer warn about bad constants embedded in unused types. This relied on being able to report just a warning, not a hard error on that case, which we cannot do any more now that error reporting is consistently centralized. r? @RalfJung fixes #53561
2018-10-26Rollup merge of #55258 - Aaron1011:fix/rustdoc-blanket, r=GuillaumeGomezkennytm-0/+35
Fix Rustdoc ICE when checking blanket impls Fixes #55001, #54744 Previously, SelectionContext would unconditionally cache the selection result for an obligation. This worked fine for most users of SelectionContext, but it caused an issue when used by Rustdoc's blanket impl finder. The issue occured when SelectionContext chose a ParamCandidate which contained inference variables. Since inference variables can change between calls to select(), it's not safe to cache the selection result - the chosen candidate might not be applicable for future results, leading to an ICE when we try to run confirmation. This commit prevents SelectionContext from caching any ParamCandidate that contains inference variables. This should always be completely safe, as trait selection should never depend on a particular result being cached. I've also added some extra debug!() statements, which I found helpful in tracking down this bug.
2018-10-26Remove redundant cloneShotaro Yamada-3/+3
2018-10-25Fix tidy errorAaron Hill-1/+3
2018-10-25Check for negative impls when finding auto traitsAaron Hill-1/+14
Fixes #55321 When AutoTraitFinder begins examining a type, it checks for an explicit negative impl. However, it wasn't checking for negative impls found when calling 'select' on predicates found from nested obligations. This commit makes AutoTraitFinder check for negative impls whenever it makes a call to 'select'. If a negative impl is found, it immediately bails out. Normal users of SelectioContext don't need to worry about this, since they stop as soon as an Unimplemented error is encountered. However, we add predicates to our ParamEnv when we encounter this error, so we need to handle negative impls specially (so that we don't try adding them to our ParamEnv).
2018-10-25preserve const eval error information through trait error systemRalf Jung-7/+10
2018-10-25Report const eval error inside the queryOliver Schneider-28/+9
2018-10-25Auto merge of #55347 - pietroalbini:rollup, r=pietroalbinibors-2/+2
Rollup of 22 pull requests Successful merges: - #53507 (Add doc for impl From for Waker) - #53931 (Gradually expanding libstd's keyword documentation) - #54965 (update tcp stream documentation) - #54977 (Accept `Option<Box<$t:ty>>` in macro argument) - #55138 (in which unused-parens suggestions heed what the user actually wrote) - #55173 (Suggest appropriate syntax on missing lifetime specifier in return type) - #55200 (Documents `From` implementations for `Stdio`) - #55245 (submodules: update clippy from 5afdf8b7 to b1d03437) - #55247 (Clarified code example in char primitive doc) - #55251 (Fix a typo in the documentation of RangeInclusive) - #55253 (only issue "variant of the expected type" suggestion for enums) - #55254 (Correct trailing ellipsis in name_from_pat) - #55269 (fix typos in various places) - #55282 (Remove redundant clone) - #55285 (Do some copy editing on the release notes) - #55291 (Update stdsimd submodule) - #55296 (Set RUST_BACKTRACE=0 for rustdoc-ui/failed-doctest-output.rs) - #55306 (Regression test for #54478.) - #55328 (Fix doc for new copysign functions) - #55340 (Operands no longer appear in places) - #55345 (Remove is_null) - #55348 (Update RELEASES.md after destabilization of non_modrs_mods) Failed merges: r? @ghost
2018-10-25Rollup merge of #55282 - sinkuu:redundant_clone, r=estebankPietro Albini-1/+1
Remove redundant clone
2018-10-24port the relate-types code from NLL type-check into a type-opNiko Matsakis-7/+10
Add regression tests for #55219 and #55241 Also another test where a duplicate-like error appears to have been suppressed; I'm not 100% sure why this output changes, though I could imagine that some duplicate suppression is enabled by this PR.
2018-10-24introduce (but do not use) `ascribe_user_type` goalNiko Matsakis-0/+78
Lots of annoying boilerplate.
2018-10-23fix typos in various placesMatthias Krüger-1/+1
2018-10-23Remove redundant cloneShotaro Yamada-1/+1
2018-10-22Fix Rustdoc ICE when checking blanket implsAaron Hill-0/+35
Fixes #55001, #54744 Previously, SelectionContext would unconditionally cache the selection result for an obligation. This worked fine for most users of SelectionContext, but it caused an issue when used by Rustdoc's blanket impl finder. The issue occured when SelectionContext chose a ParamCandidate which contained inference variables. Since inference variables can change between calls to select(), it's not safe to cache the selection result - the chosen candidate might not be applicable for future results, leading to an ICE when we try to run confirmation. This commit prevents SelectionContext from caching any ParamCandidate that contains inference variables. This should always be completely safe, as trait selection should never depend on a particular result being cached. I've also added some extra debug!() statements, which I found helpful in tracking down this bug.
2018-10-20Rename InferTy::CanonicalTy to BoundTy and add DebruijnIndex to variant typeFabian Drinck-3/+3
2018-10-20Auto merge of #55014 - ljedrz:lazyboye_unwraps, r=matthewjasperbors-4/+4
Prefer unwrap_or_else to unwrap_or in case of function calls/allocations The contents of `unwrap_or` are evaluated eagerly, so it's not a good pick in case of function calls and allocations. This PR also changes a few `unwrap_or`s with `unwrap_or_default`. An added bonus is that in some cases this change also reveals if the object it's called on is an `Option` or a `Result` (based on whether the closure takes an argument).
2018-10-19Free some memory instead of just dropping elementsOliver Scherer-2/+4
2018-10-19Prefer `Default::default` over `FxHash*::default` in struct constructorsOliver Scherer-46/+9
2018-10-19Deprecate the `FxHashMap()` and `FxHashSet()` constructor function hackOliver Scherer-13/+13
2018-10-19Prefer unwrap_or_else to unwrap_or in case of function calls/allocationsljedrz-4/+4
2018-10-19Auto merge of #55040 - scalexm:param-env, r=nikomatsakisbors-3/+95
Replace `ParamEnv` with a new type in chalk context. I left a few FIXMEs. r? @nikomatsakis
2018-10-18Auto merge of #54979 - estebank:path-unsized, r=nikomatsakisbors-0/+3
Custom E0277 diagnostic for `Path` r? @nikomatsakis we have a way to target `Path` exclusively, we need to identify the correct text to show to consider #23286 fixed.
2018-10-17Categorize chalk clausesscalexm-3/+28
2018-10-17Implement the `environment` queryscalexm-6/+7
2018-10-17Use `Environment` instead of `ty::ParamEnv` in chalk contextscalexm-1/+67
2018-10-17Auto merge of #54946 - estebank:iterator, r=varkorbors-3/+34
Add filtering option to `rustc_on_unimplemented` and reword `Iterator` E0277 errors - Add more targetting filters for arrays to `rustc_on_unimplemented` (Fix #53766) - Detect one element array of `Range` type, which is potentially a typo: `for _ in [0..10] {}` where iterating between `0` and `10` was intended. (Fix #23141) - Suggest `.bytes()` and `.chars()` for `String`. - Suggest borrowing or `.iter()` on arrays (Fix #36391) - Suggest using range literal when iterating on integers (Fix #34353) - Do not suggest `.iter()` by default (Fix #50773, fix #46806) - Add regression test (Fix #22872)
2018-10-16Change Scalar to numeric castEsteban Küber-1/+2
2018-10-16Auto merge of #55067 - ljedrz:generic_iterator_related_improvements, ↵bors-7/+4
r=petrochenkov A few iterator-related improvements - typeck: don't collect into a vector when unnecessary - create only one vector when winnowing candidates - change a cloning map to `into_iter`