diff options
Diffstat (limited to 'crates')
81 files changed, 9472 insertions, 1855 deletions
diff --git a/crates/base_db/src/change.rs b/crates/base_db/src/change.rs index 043e03bba..04e294e41 100644 --- a/crates/base_db/src/change.rs +++ b/crates/base_db/src/change.rs | |||
@@ -19,7 +19,7 @@ pub struct Change { | |||
19 | 19 | ||
20 | impl fmt::Debug for Change { | 20 | impl fmt::Debug for Change { |
21 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | 21 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
22 | let mut d = fmt.debug_struct("AnalysisChange"); | 22 | let mut d = fmt.debug_struct("Change"); |
23 | if let Some(roots) = &self.roots { | 23 | if let Some(roots) = &self.roots { |
24 | d.field("roots", roots); | 24 | d.field("roots", roots); |
25 | } | 25 | } |
diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index b3218833d..fc1a74641 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs | |||
@@ -14,7 +14,7 @@ use hir_def::{ | |||
14 | per_ns::PerNs, | 14 | per_ns::PerNs, |
15 | resolver::{HasResolver, Resolver}, | 15 | resolver::{HasResolver, Resolver}, |
16 | src::HasSource as _, | 16 | src::HasSource as _, |
17 | type_ref::{Mutability, TypeRef}, | 17 | type_ref::TypeRef, |
18 | AdtId, AssocContainerId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId, | 18 | AdtId, AssocContainerId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId, |
19 | DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule, ImplId, LifetimeParamId, | 19 | DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule, ImplId, LifetimeParamId, |
20 | LocalEnumVariantId, LocalFieldId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId, | 20 | LocalEnumVariantId, LocalFieldId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId, |
@@ -31,9 +31,9 @@ use hir_ty::{ | |||
31 | display::{write_bounds_like_dyn_trait_with_prefix, HirDisplayError, HirFormatter}, | 31 | display::{write_bounds_like_dyn_trait_with_prefix, HirDisplayError, HirFormatter}, |
32 | method_resolution, | 32 | method_resolution, |
33 | traits::{FnTrait, Solution, SolutionVariables}, | 33 | traits::{FnTrait, Solution, SolutionVariables}, |
34 | ApplicationTy, BoundVar, CallableDefId, Canonical, DebruijnIndex, FnSig, GenericPredicate, | 34 | AliasTy, BoundVar, CallableDefId, CallableSig, Canonical, DebruijnIndex, GenericPredicate, |
35 | InEnvironment, Obligation, ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, Ty, | 35 | InEnvironment, Mutability, Obligation, ProjectionPredicate, ProjectionTy, Scalar, Substs, |
36 | TyDefId, TyKind, TypeCtor, | 36 | TraitEnvironment, Ty, TyDefId, TyVariableKind, |
37 | }; | 37 | }; |
38 | use rustc_hash::FxHashSet; | 38 | use rustc_hash::FxHashSet; |
39 | use stdx::{format_to, impl_from}; | 39 | use stdx::{format_to, impl_from}; |
@@ -836,7 +836,7 @@ pub enum Access { | |||
836 | impl From<Mutability> for Access { | 836 | impl From<Mutability> for Access { |
837 | fn from(mutability: Mutability) -> Access { | 837 | fn from(mutability: Mutability) -> Access { |
838 | match mutability { | 838 | match mutability { |
839 | Mutability::Shared => Access::Shared, | 839 | Mutability::Not => Access::Shared, |
840 | Mutability::Mut => Access::Exclusive, | 840 | Mutability::Mut => Access::Exclusive, |
841 | } | 841 | } |
842 | } | 842 | } |
@@ -865,7 +865,10 @@ impl SelfParam { | |||
865 | .params | 865 | .params |
866 | .first() | 866 | .first() |
867 | .map(|param| match *param { | 867 | .map(|param| match *param { |
868 | TypeRef::Reference(.., mutability) => mutability.into(), | 868 | TypeRef::Reference(.., mutability) => match mutability { |
869 | hir_def::type_ref::Mutability::Shared => Access::Shared, | ||
870 | hir_def::type_ref::Mutability::Mut => Access::Exclusive, | ||
871 | }, | ||
869 | _ => Access::Owned, | 872 | _ => Access::Owned, |
870 | }) | 873 | }) |
871 | .unwrap_or(Access::Owned) | 874 | .unwrap_or(Access::Owned) |
@@ -1547,25 +1550,19 @@ impl Type { | |||
1547 | } | 1550 | } |
1548 | 1551 | ||
1549 | pub fn is_unit(&self) -> bool { | 1552 | pub fn is_unit(&self) -> bool { |
1550 | matches!( | 1553 | matches!(self.ty.value, Ty::Tuple(0, ..)) |
1551 | self.ty.value, | ||
1552 | Ty::Apply(ApplicationTy { ctor: TypeCtor::Tuple { cardinality: 0 }, .. }) | ||
1553 | ) | ||
1554 | } | 1554 | } |
1555 | pub fn is_bool(&self) -> bool { | 1555 | pub fn is_bool(&self) -> bool { |
1556 | matches!(self.ty.value, Ty::Apply(ApplicationTy { ctor: TypeCtor::Bool, .. })) | 1556 | matches!(self.ty.value, Ty::Scalar(Scalar::Bool)) |
1557 | } | 1557 | } |
1558 | 1558 | ||
1559 | pub fn is_mutable_reference(&self) -> bool { | 1559 | pub fn is_mutable_reference(&self) -> bool { |
1560 | matches!( | 1560 | matches!(self.ty.value, Ty::Ref(Mutability::Mut, ..)) |
1561 | self.ty.value, | ||
1562 | Ty::Apply(ApplicationTy { ctor: TypeCtor::Ref(Mutability::Mut), .. }) | ||
1563 | ) | ||
1564 | } | 1561 | } |
1565 | 1562 | ||
1566 | pub fn remove_ref(&self) -> Option<Type> { | 1563 | pub fn remove_ref(&self) -> Option<Type> { |
1567 | if let Ty::Apply(ApplicationTy { ctor: TypeCtor::Ref(_), .. }) = self.ty.value { | 1564 | if let Ty::Ref(.., substs) = &self.ty.value { |
1568 | self.ty.value.substs().map(|substs| self.derived(substs[0].clone())) | 1565 | Some(self.derived(substs[0].clone())) |
1569 | } else { | 1566 | } else { |
1570 | None | 1567 | None |
1571 | } | 1568 | } |
@@ -1654,14 +1651,14 @@ impl Type { | |||
1654 | .build(); | 1651 | .build(); |
1655 | let predicate = ProjectionPredicate { | 1652 | let predicate = ProjectionPredicate { |
1656 | projection_ty: ProjectionTy { associated_ty: alias.id, parameters: subst }, | 1653 | projection_ty: ProjectionTy { associated_ty: alias.id, parameters: subst }, |
1657 | ty: Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0)), | 1654 | ty: Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)), |
1658 | }; | 1655 | }; |
1659 | let goal = Canonical { | 1656 | let goal = Canonical { |
1660 | value: InEnvironment::new( | 1657 | value: InEnvironment::new( |
1661 | self.ty.environment.clone(), | 1658 | self.ty.environment.clone(), |
1662 | Obligation::Projection(predicate), | 1659 | Obligation::Projection(predicate), |
1663 | ), | 1660 | ), |
1664 | kinds: Arc::new([TyKind::General]), | 1661 | kinds: Arc::new([TyVariableKind::General]), |
1665 | }; | 1662 | }; |
1666 | 1663 | ||
1667 | match db.trait_solve(self.krate, goal)? { | 1664 | match db.trait_solve(self.krate, goal)? { |
@@ -1685,7 +1682,7 @@ impl Type { | |||
1685 | 1682 | ||
1686 | pub fn as_callable(&self, db: &dyn HirDatabase) -> Option<Callable> { | 1683 | pub fn as_callable(&self, db: &dyn HirDatabase) -> Option<Callable> { |
1687 | let def = match self.ty.value { | 1684 | let def = match self.ty.value { |
1688 | Ty::Apply(ApplicationTy { ctor: TypeCtor::FnDef(def), parameters: _ }) => Some(def), | 1685 | Ty::FnDef(def, _) => Some(def), |
1689 | _ => None, | 1686 | _ => None, |
1690 | }; | 1687 | }; |
1691 | 1688 | ||
@@ -1694,20 +1691,16 @@ impl Type { | |||
1694 | } | 1691 | } |
1695 | 1692 | ||
1696 | pub fn is_closure(&self) -> bool { | 1693 | pub fn is_closure(&self) -> bool { |
1697 | matches!(&self.ty.value, Ty::Apply(ApplicationTy { ctor: TypeCtor::Closure { .. }, .. })) | 1694 | matches!(&self.ty.value, Ty::Closure { .. }) |
1698 | } | 1695 | } |
1699 | 1696 | ||
1700 | pub fn is_fn(&self) -> bool { | 1697 | pub fn is_fn(&self) -> bool { |
1701 | matches!( | 1698 | matches!(&self.ty.value, Ty::FnDef(..) | Ty::Function { .. }) |
1702 | &self.ty.value, | ||
1703 | Ty::Apply(ApplicationTy { ctor: TypeCtor::FnDef(..), .. }) | ||
1704 | | Ty::Apply(ApplicationTy { ctor: TypeCtor::FnPtr { .. }, .. }) | ||
1705 | ) | ||
1706 | } | 1699 | } |
1707 | 1700 | ||
1708 | pub fn is_packed(&self, db: &dyn HirDatabase) -> bool { | 1701 | pub fn is_packed(&self, db: &dyn HirDatabase) -> bool { |
1709 | let adt_id = match self.ty.value { | 1702 | let adt_id = match self.ty.value { |
1710 | Ty::Apply(ApplicationTy { ctor: TypeCtor::Adt(adt_id), .. }) => adt_id, | 1703 | Ty::Adt(adt_id, ..) => adt_id, |
1711 | _ => return false, | 1704 | _ => return false, |
1712 | }; | 1705 | }; |
1713 | 1706 | ||
@@ -1719,7 +1712,7 @@ impl Type { | |||
1719 | } | 1712 | } |
1720 | 1713 | ||
1721 | pub fn is_raw_ptr(&self) -> bool { | 1714 | pub fn is_raw_ptr(&self) -> bool { |
1722 | matches!(&self.ty.value, Ty::Apply(ApplicationTy { ctor: TypeCtor::RawPtr(..), .. })) | 1715 | matches!(&self.ty.value, Ty::Raw(..)) |
1723 | } | 1716 | } |
1724 | 1717 | ||
1725 | pub fn contains_unknown(&self) -> bool { | 1718 | pub fn contains_unknown(&self) -> bool { |
@@ -1728,44 +1721,34 @@ impl Type { | |||
1728 | fn go(ty: &Ty) -> bool { | 1721 | fn go(ty: &Ty) -> bool { |
1729 | match ty { | 1722 | match ty { |
1730 | Ty::Unknown => true, | 1723 | Ty::Unknown => true, |
1731 | Ty::Apply(a_ty) => a_ty.parameters.iter().any(go), | 1724 | _ => ty.substs().map_or(false, |substs| substs.iter().any(go)), |
1732 | _ => false, | ||
1733 | } | 1725 | } |
1734 | } | 1726 | } |
1735 | } | 1727 | } |
1736 | 1728 | ||
1737 | pub fn fields(&self, db: &dyn HirDatabase) -> Vec<(Field, Type)> { | 1729 | pub fn fields(&self, db: &dyn HirDatabase) -> Vec<(Field, Type)> { |
1738 | if let Ty::Apply(a_ty) = &self.ty.value { | 1730 | let (variant_id, substs) = match self.ty.value { |
1739 | let variant_id = match a_ty.ctor { | 1731 | Ty::Adt(AdtId::StructId(s), ref substs) => (s.into(), substs), |
1740 | TypeCtor::Adt(AdtId::StructId(s)) => s.into(), | 1732 | Ty::Adt(AdtId::UnionId(u), ref substs) => (u.into(), substs), |
1741 | TypeCtor::Adt(AdtId::UnionId(u)) => u.into(), | 1733 | _ => return Vec::new(), |
1742 | _ => return Vec::new(), | ||
1743 | }; | ||
1744 | |||
1745 | return db | ||
1746 | .field_types(variant_id) | ||
1747 | .iter() | ||
1748 | .map(|(local_id, ty)| { | ||
1749 | let def = Field { parent: variant_id.into(), id: local_id }; | ||
1750 | let ty = ty.clone().subst(&a_ty.parameters); | ||
1751 | (def, self.derived(ty)) | ||
1752 | }) | ||
1753 | .collect(); | ||
1754 | }; | 1734 | }; |
1755 | Vec::new() | 1735 | |
1736 | db.field_types(variant_id) | ||
1737 | .iter() | ||
1738 | .map(|(local_id, ty)| { | ||
1739 | let def = Field { parent: variant_id.into(), id: local_id }; | ||
1740 | let ty = ty.clone().subst(substs); | ||
1741 | (def, self.derived(ty)) | ||
1742 | }) | ||
1743 | .collect() | ||
1756 | } | 1744 | } |
1757 | 1745 | ||
1758 | pub fn tuple_fields(&self, _db: &dyn HirDatabase) -> Vec<Type> { | 1746 | pub fn tuple_fields(&self, _db: &dyn HirDatabase) -> Vec<Type> { |
1759 | let mut res = Vec::new(); | 1747 | if let Ty::Tuple(_, substs) = &self.ty.value { |
1760 | if let Ty::Apply(a_ty) = &self.ty.value { | 1748 | substs.iter().map(|ty| self.derived(ty.clone())).collect() |
1761 | if let TypeCtor::Tuple { .. } = a_ty.ctor { | 1749 | } else { |
1762 | for ty in a_ty.parameters.iter() { | 1750 | Vec::new() |
1763 | let ty = ty.clone(); | 1751 | } |
1764 | res.push(self.derived(ty)); | ||
1765 | } | ||
1766 | } | ||
1767 | }; | ||
1768 | res | ||
1769 | } | 1752 | } |
1770 | 1753 | ||
1771 | pub fn autoderef<'a>(&'a self, db: &'a dyn HirDatabase) -> impl Iterator<Item = Type> + 'a { | 1754 | pub fn autoderef<'a>(&'a self, db: &'a dyn HirDatabase) -> impl Iterator<Item = Type> + 'a { |
@@ -1802,15 +1785,13 @@ impl Type { | |||
1802 | } | 1785 | } |
1803 | 1786 | ||
1804 | pub fn type_parameters(&self) -> impl Iterator<Item = Type> + '_ { | 1787 | pub fn type_parameters(&self) -> impl Iterator<Item = Type> + '_ { |
1805 | let ty = self.ty.value.strip_references(); | 1788 | self.ty |
1806 | let substs = match ty { | 1789 | .value |
1807 | Ty::Apply(apply_ty) => &apply_ty.parameters, | 1790 | .strip_references() |
1808 | Ty::Opaque(opaque_ty) => &opaque_ty.parameters, | 1791 | .substs() |
1809 | _ => return Either::Left(iter::empty()), | 1792 | .into_iter() |
1810 | }; | 1793 | .flat_map(|substs| substs.iter()) |
1811 | 1794 | .map(move |ty| self.derived(ty.clone())) | |
1812 | let iter = substs.iter().map(move |ty| self.derived(ty.clone())); | ||
1813 | Either::Right(iter) | ||
1814 | } | 1795 | } |
1815 | 1796 | ||
1816 | pub fn iterate_method_candidates<T>( | 1797 | pub fn iterate_method_candidates<T>( |
@@ -1900,17 +1881,8 @@ impl Type { | |||
1900 | 1881 | ||
1901 | // FIXME: provide required accessors such that it becomes implementable from outside. | 1882 | // FIXME: provide required accessors such that it becomes implementable from outside. |
1902 | pub fn is_equal_for_find_impls(&self, other: &Type) -> bool { | 1883 | pub fn is_equal_for_find_impls(&self, other: &Type) -> bool { |
1903 | match (&self.ty.value, &other.ty.value) { | 1884 | let rref = other.remove_ref(); |
1904 | (Ty::Apply(a_original_ty), Ty::Apply(ApplicationTy { ctor, parameters })) => match ctor | 1885 | self.ty.value.equals_ctor(rref.as_ref().map_or(&other.ty.value, |it| &it.ty.value)) |
1905 | { | ||
1906 | TypeCtor::Ref(..) => match parameters.as_single() { | ||
1907 | Ty::Apply(a_ty) => a_original_ty.ctor == a_ty.ctor, | ||
1908 | _ => false, | ||
1909 | }, | ||
1910 | _ => a_original_ty.ctor == *ctor, | ||
1911 | }, | ||
1912 | _ => false, | ||
1913 | } | ||
1914 | } | 1886 | } |
1915 | 1887 | ||
1916 | fn derived(&self, ty: Ty) -> Type { | 1888 | fn derived(&self, ty: Ty) -> Type { |
@@ -1955,28 +1927,20 @@ impl Type { | |||
1955 | fn walk_type(db: &dyn HirDatabase, type_: &Type, cb: &mut impl FnMut(Type)) { | 1927 | fn walk_type(db: &dyn HirDatabase, type_: &Type, cb: &mut impl FnMut(Type)) { |
1956 | let ty = type_.ty.value.strip_references(); | 1928 | let ty = type_.ty.value.strip_references(); |
1957 | match ty { | 1929 | match ty { |
1958 | Ty::Apply(ApplicationTy { ctor, parameters }) => { | 1930 | Ty::Adt(..) => { |
1959 | match ctor { | 1931 | cb(type_.derived(ty.clone())); |
1960 | TypeCtor::Adt(_) => { | 1932 | } |
1961 | cb(type_.derived(ty.clone())); | 1933 | Ty::AssociatedType(..) => { |
1962 | } | 1934 | if let Some(_) = ty.associated_type_parent_trait(db) { |
1963 | TypeCtor::AssociatedType(_) => { | 1935 | cb(type_.derived(ty.clone())); |
1964 | if let Some(_) = ty.associated_type_parent_trait(db) { | ||
1965 | cb(type_.derived(ty.clone())); | ||
1966 | } | ||
1967 | } | ||
1968 | TypeCtor::OpaqueType(..) => { | ||
1969 | if let Some(bounds) = ty.impl_trait_bounds(db) { | ||
1970 | walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); | ||
1971 | } | ||
1972 | } | ||
1973 | _ => (), | ||
1974 | } | 1936 | } |
1975 | |||
1976 | // adt params, tuples, etc... | ||
1977 | walk_substs(db, type_, parameters, cb); | ||
1978 | } | 1937 | } |
1979 | Ty::Opaque(opaque_ty) => { | 1938 | Ty::OpaqueType(..) => { |
1939 | if let Some(bounds) = ty.impl_trait_bounds(db) { | ||
1940 | walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); | ||
1941 | } | ||
1942 | } | ||
1943 | Ty::Alias(AliasTy::Opaque(opaque_ty)) => { | ||
1980 | if let Some(bounds) = ty.impl_trait_bounds(db) { | 1944 | if let Some(bounds) = ty.impl_trait_bounds(db) { |
1981 | walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); | 1945 | walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); |
1982 | } | 1946 | } |
@@ -1992,7 +1956,10 @@ impl Type { | |||
1992 | walk_bounds(db, &type_.derived(ty.clone()), bounds.as_ref(), cb); | 1956 | walk_bounds(db, &type_.derived(ty.clone()), bounds.as_ref(), cb); |
1993 | } | 1957 | } |
1994 | 1958 | ||
1995 | _ => (), | 1959 | _ => {} |
1960 | } | ||
1961 | if let Some(substs) = ty.substs() { | ||
1962 | walk_substs(db, type_, &substs, cb); | ||
1996 | } | 1963 | } |
1997 | } | 1964 | } |
1998 | 1965 | ||
@@ -2010,7 +1977,7 @@ impl HirDisplay for Type { | |||
2010 | #[derive(Debug)] | 1977 | #[derive(Debug)] |
2011 | pub struct Callable { | 1978 | pub struct Callable { |
2012 | ty: Type, | 1979 | ty: Type, |
2013 | sig: FnSig, | 1980 | sig: CallableSig, |
2014 | def: Option<CallableDefId>, | 1981 | def: Option<CallableDefId>, |
2015 | pub(crate) is_bound_method: bool, | 1982 | pub(crate) is_bound_method: bool, |
2016 | } | 1983 | } |
diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs index 5343a036c..b1ebba516 100644 --- a/crates/hir/src/diagnostics.rs +++ b/crates/hir/src/diagnostics.rs | |||
@@ -1,5 +1,7 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | pub use hir_def::diagnostics::{InactiveCode, UnresolvedModule, UnresolvedProcMacro}; | 2 | pub use hir_def::diagnostics::{ |
3 | InactiveCode, UnresolvedMacroCall, UnresolvedModule, UnresolvedProcMacro, | ||
4 | }; | ||
3 | pub use hir_expand::diagnostics::{ | 5 | pub use hir_expand::diagnostics::{ |
4 | Diagnostic, DiagnosticCode, DiagnosticSink, DiagnosticSinkBuilder, | 6 | Diagnostic, DiagnosticCode, DiagnosticSink, DiagnosticSinkBuilder, |
5 | }; | 7 | }; |
diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs index 59292d5a2..144851f83 100644 --- a/crates/hir/src/semantics.rs +++ b/crates/hir/src/semantics.rs | |||
@@ -16,13 +16,12 @@ use rustc_hash::{FxHashMap, FxHashSet}; | |||
16 | use syntax::{ | 16 | use syntax::{ |
17 | algo::find_node_at_offset, | 17 | algo::find_node_at_offset, |
18 | ast::{self, GenericParamsOwner, LoopBodyOwner}, | 18 | ast::{self, GenericParamsOwner, LoopBodyOwner}, |
19 | match_ast, AstNode, SyntaxNode, SyntaxToken, TextSize, | 19 | match_ast, AstNode, SyntaxNode, SyntaxNodePtr, SyntaxToken, TextSize, |
20 | }; | 20 | }; |
21 | 21 | ||
22 | use crate::{ | 22 | use crate::{ |
23 | code_model::Access, | 23 | code_model::Access, |
24 | db::HirDatabase, | 24 | db::HirDatabase, |
25 | diagnostics::Diagnostic, | ||
26 | semantics::source_to_def::{ChildContainer, SourceToDefCache, SourceToDefCtx}, | 25 | semantics::source_to_def::{ChildContainer, SourceToDefCache, SourceToDefCtx}, |
27 | source_analyzer::{resolve_hir_path, SourceAnalyzer}, | 26 | source_analyzer::{resolve_hir_path, SourceAnalyzer}, |
28 | AssocItem, Callable, ConstParam, Crate, Field, Function, HirFileId, Impl, InFile, Label, | 27 | AssocItem, Callable, ConstParam, Crate, Field, Function, HirFileId, Impl, InFile, Label, |
@@ -141,7 +140,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { | |||
141 | self.imp.original_range(node) | 140 | self.imp.original_range(node) |
142 | } | 141 | } |
143 | 142 | ||
144 | pub fn diagnostics_display_range(&self, diagnostics: &dyn Diagnostic) -> FileRange { | 143 | pub fn diagnostics_display_range(&self, diagnostics: InFile<SyntaxNodePtr>) -> FileRange { |
145 | self.imp.diagnostics_display_range(diagnostics) | 144 | self.imp.diagnostics_display_range(diagnostics) |
146 | } | 145 | } |
147 | 146 | ||
@@ -385,8 +384,7 @@ impl<'db> SemanticsImpl<'db> { | |||
385 | node.as_ref().original_file_range(self.db.upcast()) | 384 | node.as_ref().original_file_range(self.db.upcast()) |
386 | } | 385 | } |
387 | 386 | ||
388 | fn diagnostics_display_range(&self, diagnostics: &dyn Diagnostic) -> FileRange { | 387 | fn diagnostics_display_range(&self, src: InFile<SyntaxNodePtr>) -> FileRange { |
389 | let src = diagnostics.display_source(); | ||
390 | let root = self.db.parse_or_expand(src.file_id).unwrap(); | 388 | let root = self.db.parse_or_expand(src.file_id).unwrap(); |
391 | let node = src.value.to_node(&root); | 389 | let node = src.value.to_node(&root); |
392 | self.cache(root, src.file_id); | 390 | self.cache(root, src.file_id); |
diff --git a/crates/hir/src/source_analyzer.rs b/crates/hir/src/source_analyzer.rs index dc21f6051..64ce4add1 100644 --- a/crates/hir/src/source_analyzer.rs +++ b/crates/hir/src/source_analyzer.rs | |||
@@ -20,7 +20,7 @@ use hir_def::{ | |||
20 | use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile}; | 20 | use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile}; |
21 | use hir_ty::{ | 21 | use hir_ty::{ |
22 | diagnostics::{record_literal_missing_fields, record_pattern_missing_fields}, | 22 | diagnostics::{record_literal_missing_fields, record_pattern_missing_fields}, |
23 | InferenceResult, Substs, Ty, | 23 | InferenceResult, Substs, |
24 | }; | 24 | }; |
25 | use syntax::{ | 25 | use syntax::{ |
26 | ast::{self, AstNode}, | 26 | ast::{self, AstNode}, |
@@ -299,14 +299,11 @@ impl SourceAnalyzer { | |||
299 | let infer = self.infer.as_ref()?; | 299 | let infer = self.infer.as_ref()?; |
300 | 300 | ||
301 | let expr_id = self.expr_id(db, &literal.clone().into())?; | 301 | let expr_id = self.expr_id(db, &literal.clone().into())?; |
302 | let substs = match &infer.type_of_expr[expr_id] { | 302 | let substs = infer.type_of_expr[expr_id].substs()?; |
303 | Ty::Apply(a_ty) => &a_ty.parameters, | ||
304 | _ => return None, | ||
305 | }; | ||
306 | 303 | ||
307 | let (variant, missing_fields, _exhaustive) = | 304 | let (variant, missing_fields, _exhaustive) = |
308 | record_literal_missing_fields(db, infer, expr_id, &body[expr_id])?; | 305 | record_literal_missing_fields(db, infer, expr_id, &body[expr_id])?; |
309 | let res = self.missing_fields(db, krate, substs, variant, missing_fields); | 306 | let res = self.missing_fields(db, krate, &substs, variant, missing_fields); |
310 | Some(res) | 307 | Some(res) |
311 | } | 308 | } |
312 | 309 | ||
@@ -320,14 +317,11 @@ impl SourceAnalyzer { | |||
320 | let infer = self.infer.as_ref()?; | 317 | let infer = self.infer.as_ref()?; |
321 | 318 | ||
322 | let pat_id = self.pat_id(&pattern.clone().into())?; | 319 | let pat_id = self.pat_id(&pattern.clone().into())?; |
323 | let substs = match &infer.type_of_pat[pat_id] { | 320 | let substs = infer.type_of_pat[pat_id].substs()?; |
324 | Ty::Apply(a_ty) => &a_ty.parameters, | ||
325 | _ => return None, | ||
326 | }; | ||
327 | 321 | ||
328 | let (variant, missing_fields, _exhaustive) = | 322 | let (variant, missing_fields, _exhaustive) = |
329 | record_pattern_missing_fields(db, infer, pat_id, &body[pat_id])?; | 323 | record_pattern_missing_fields(db, infer, pat_id, &body[pat_id])?; |
330 | let res = self.missing_fields(db, krate, substs, variant, missing_fields); | 324 | let res = self.missing_fields(db, krate, &substs, variant, missing_fields); |
331 | Some(res) | 325 | Some(res) |
332 | } | 326 | } |
333 | 327 | ||
diff --git a/crates/hir_def/src/body.rs b/crates/hir_def/src/body.rs index 9a432f7d1..ff4b4a0cf 100644 --- a/crates/hir_def/src/body.rs +++ b/crates/hir_def/src/body.rs | |||
@@ -123,7 +123,7 @@ impl Expander { | |||
123 | Some(it) => it, | 123 | Some(it) => it, |
124 | None => { | 124 | None => { |
125 | if err.is_none() { | 125 | if err.is_none() { |
126 | eprintln!("no error despite `as_call_id_with_errors` returning `None`"); | 126 | log::warn!("no error despite `as_call_id_with_errors` returning `None`"); |
127 | } | 127 | } |
128 | return ExpandResult { value: None, err }; | 128 | return ExpandResult { value: None, err }; |
129 | } | 129 | } |
diff --git a/crates/hir_def/src/body/lower.rs b/crates/hir_def/src/body/lower.rs index c18001e15..40beb2f7a 100644 --- a/crates/hir_def/src/body/lower.rs +++ b/crates/hir_def/src/body/lower.rs | |||
@@ -24,7 +24,7 @@ use test_utils::mark; | |||
24 | use crate::{ | 24 | use crate::{ |
25 | adt::StructKind, | 25 | adt::StructKind, |
26 | body::{Body, BodySourceMap, Expander, LabelSource, PatPtr, SyntheticSyntax}, | 26 | body::{Body, BodySourceMap, Expander, LabelSource, PatPtr, SyntheticSyntax}, |
27 | builtin_type::{BuiltinFloat, BuiltinInt}, | 27 | builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint}, |
28 | db::DefDatabase, | 28 | db::DefDatabase, |
29 | diagnostics::{InactiveCode, MacroError, UnresolvedProcMacro}, | 29 | diagnostics::{InactiveCode, MacroError, UnresolvedProcMacro}, |
30 | expr::{ | 30 | expr::{ |
@@ -1065,11 +1065,16 @@ impl From<ast::LiteralKind> for Literal { | |||
1065 | fn from(ast_lit_kind: ast::LiteralKind) -> Self { | 1065 | fn from(ast_lit_kind: ast::LiteralKind) -> Self { |
1066 | match ast_lit_kind { | 1066 | match ast_lit_kind { |
1067 | LiteralKind::IntNumber(lit) => { | 1067 | LiteralKind::IntNumber(lit) => { |
1068 | if let Some(float_suffix) = lit.suffix().and_then(BuiltinFloat::from_suffix) { | 1068 | if let builtin @ Some(_) = lit.suffix().and_then(BuiltinFloat::from_suffix) { |
1069 | return Literal::Float(Default::default(), Some(float_suffix)); | 1069 | return Literal::Float(Default::default(), builtin); |
1070 | } else if let builtin @ Some(_) = | ||
1071 | lit.suffix().and_then(|it| BuiltinInt::from_suffix(&it)) | ||
1072 | { | ||
1073 | Literal::Int(Default::default(), builtin) | ||
1074 | } else { | ||
1075 | let builtin = lit.suffix().and_then(|it| BuiltinUint::from_suffix(&it)); | ||
1076 | Literal::Uint(Default::default(), builtin) | ||
1070 | } | 1077 | } |
1071 | let ty = lit.suffix().and_then(|it| BuiltinInt::from_suffix(&it)); | ||
1072 | Literal::Int(Default::default(), ty) | ||
1073 | } | 1078 | } |
1074 | LiteralKind::FloatNumber(lit) => { | 1079 | LiteralKind::FloatNumber(lit) => { |
1075 | let ty = lit.suffix().and_then(|it| BuiltinFloat::from_suffix(&it)); | 1080 | let ty = lit.suffix().and_then(|it| BuiltinFloat::from_suffix(&it)); |
@@ -1077,7 +1082,7 @@ impl From<ast::LiteralKind> for Literal { | |||
1077 | } | 1082 | } |
1078 | LiteralKind::ByteString(_) => Literal::ByteString(Default::default()), | 1083 | LiteralKind::ByteString(_) => Literal::ByteString(Default::default()), |
1079 | LiteralKind::String(_) => Literal::String(Default::default()), | 1084 | LiteralKind::String(_) => Literal::String(Default::default()), |
1080 | LiteralKind::Byte => Literal::Int(Default::default(), Some(BuiltinInt::U8)), | 1085 | LiteralKind::Byte => Literal::Uint(Default::default(), Some(BuiltinUint::U8)), |
1081 | LiteralKind::Bool(val) => Literal::Bool(val), | 1086 | LiteralKind::Bool(val) => Literal::Bool(val), |
1082 | LiteralKind::Char => Literal::Char(Default::default()), | 1087 | LiteralKind::Char => Literal::Char(Default::default()), |
1083 | } | 1088 | } |
diff --git a/crates/hir_def/src/body/tests/block.rs b/crates/hir_def/src/body/tests/block.rs index a5ec0883f..8bca72a17 100644 --- a/crates/hir_def/src/body/tests/block.rs +++ b/crates/hir_def/src/body/tests/block.rs | |||
@@ -259,3 +259,32 @@ fn main() { | |||
259 | "#]], | 259 | "#]], |
260 | ); | 260 | ); |
261 | } | 261 | } |
262 | |||
263 | #[test] | ||
264 | fn underscore_import() { | ||
265 | // This used to panic, because the default (private) visibility inside block expressions would | ||
266 | // point into the containing `DefMap`, which visibilities should never be able to do. | ||
267 | mark::check!(adjust_vis_in_block_def_map); | ||
268 | check_at( | ||
269 | r#" | ||
270 | mod m { | ||
271 | fn main() { | ||
272 | use Tr as _; | ||
273 | trait Tr {} | ||
274 | $0 | ||
275 | } | ||
276 | } | ||
277 | "#, | ||
278 | expect![[r#" | ||
279 | block scope | ||
280 | _: t | ||
281 | Tr: t | ||
282 | |||
283 | crate | ||
284 | m: t | ||
285 | |||
286 | crate::m | ||
287 | main: v | ||
288 | "#]], | ||
289 | ); | ||
290 | } | ||
diff --git a/crates/hir_def/src/builtin_type.rs b/crates/hir_def/src/builtin_type.rs index 0f872b5c0..7cbaf30b8 100644 --- a/crates/hir_def/src/builtin_type.rs +++ b/crates/hir_def/src/builtin_type.rs | |||
@@ -6,38 +6,32 @@ | |||
6 | use std::fmt; | 6 | use std::fmt; |
7 | 7 | ||
8 | use hir_expand::name::{name, AsName, Name}; | 8 | use hir_expand::name::{name, AsName, Name}; |
9 | 9 | /// Different signed int types. | |
10 | #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] | 10 | #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] |
11 | pub enum Signedness { | 11 | pub enum BuiltinInt { |
12 | Signed, | 12 | Isize, |
13 | Unsigned, | 13 | I8, |
14 | } | 14 | I16, |
15 | 15 | I32, | |
16 | #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] | 16 | I64, |
17 | pub enum IntBitness { | 17 | I128, |
18 | Xsize, | ||
19 | X8, | ||
20 | X16, | ||
21 | X32, | ||
22 | X64, | ||
23 | X128, | ||
24 | } | ||
25 | |||
26 | #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] | ||
27 | pub enum FloatBitness { | ||
28 | X32, | ||
29 | X64, | ||
30 | } | 18 | } |
31 | 19 | ||
32 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 20 | /// Different unsigned int types. |
33 | pub struct BuiltinInt { | 21 | #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] |
34 | pub signedness: Signedness, | 22 | pub enum BuiltinUint { |
35 | pub bitness: IntBitness, | 23 | Usize, |
24 | U8, | ||
25 | U16, | ||
26 | U32, | ||
27 | U64, | ||
28 | U128, | ||
36 | } | 29 | } |
37 | 30 | ||
38 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 31 | #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] |
39 | pub struct BuiltinFloat { | 32 | pub enum BuiltinFloat { |
40 | pub bitness: FloatBitness, | 33 | F32, |
34 | F64, | ||
41 | } | 35 | } |
42 | 36 | ||
43 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 37 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
@@ -46,6 +40,7 @@ pub enum BuiltinType { | |||
46 | Bool, | 40 | Bool, |
47 | Str, | 41 | Str, |
48 | Int(BuiltinInt), | 42 | Int(BuiltinInt), |
43 | Uint(BuiltinUint), | ||
49 | Float(BuiltinFloat), | 44 | Float(BuiltinFloat), |
50 | } | 45 | } |
51 | 46 | ||
@@ -56,19 +51,19 @@ impl BuiltinType { | |||
56 | (name![bool], BuiltinType::Bool), | 51 | (name![bool], BuiltinType::Bool), |
57 | (name![str], BuiltinType::Str), | 52 | (name![str], BuiltinType::Str), |
58 | 53 | ||
59 | (name![isize], BuiltinType::Int(BuiltinInt::ISIZE)), | 54 | (name![isize], BuiltinType::Int(BuiltinInt::Isize)), |
60 | (name![i8], BuiltinType::Int(BuiltinInt::I8)), | 55 | (name![i8], BuiltinType::Int(BuiltinInt::I8)), |
61 | (name![i16], BuiltinType::Int(BuiltinInt::I16)), | 56 | (name![i16], BuiltinType::Int(BuiltinInt::I16)), |
62 | (name![i32], BuiltinType::Int(BuiltinInt::I32)), | 57 | (name![i32], BuiltinType::Int(BuiltinInt::I32)), |
63 | (name![i64], BuiltinType::Int(BuiltinInt::I64)), | 58 | (name![i64], BuiltinType::Int(BuiltinInt::I64)), |
64 | (name![i128], BuiltinType::Int(BuiltinInt::I128)), | 59 | (name![i128], BuiltinType::Int(BuiltinInt::I128)), |
65 | 60 | ||
66 | (name![usize], BuiltinType::Int(BuiltinInt::USIZE)), | 61 | (name![usize], BuiltinType::Uint(BuiltinUint::Usize)), |
67 | (name![u8], BuiltinType::Int(BuiltinInt::U8)), | 62 | (name![u8], BuiltinType::Uint(BuiltinUint::U8)), |
68 | (name![u16], BuiltinType::Int(BuiltinInt::U16)), | 63 | (name![u16], BuiltinType::Uint(BuiltinUint::U16)), |
69 | (name![u32], BuiltinType::Int(BuiltinInt::U32)), | 64 | (name![u32], BuiltinType::Uint(BuiltinUint::U32)), |
70 | (name![u64], BuiltinType::Int(BuiltinInt::U64)), | 65 | (name![u64], BuiltinType::Uint(BuiltinUint::U64)), |
71 | (name![u128], BuiltinType::Int(BuiltinInt::U128)), | 66 | (name![u128], BuiltinType::Uint(BuiltinUint::U128)), |
72 | 67 | ||
73 | (name![f32], BuiltinType::Float(BuiltinFloat::F32)), | 68 | (name![f32], BuiltinType::Float(BuiltinFloat::F32)), |
74 | (name![f64], BuiltinType::Float(BuiltinFloat::F64)), | 69 | (name![f64], BuiltinType::Float(BuiltinFloat::F64)), |
@@ -81,24 +76,25 @@ impl AsName for BuiltinType { | |||
81 | BuiltinType::Char => name![char], | 76 | BuiltinType::Char => name![char], |
82 | BuiltinType::Bool => name![bool], | 77 | BuiltinType::Bool => name![bool], |
83 | BuiltinType::Str => name![str], | 78 | BuiltinType::Str => name![str], |
84 | BuiltinType::Int(BuiltinInt { signedness, bitness }) => match (signedness, bitness) { | 79 | BuiltinType::Int(it) => match it { |
85 | (Signedness::Signed, IntBitness::Xsize) => name![isize], | 80 | BuiltinInt::Isize => name![isize], |
86 | (Signedness::Signed, IntBitness::X8) => name![i8], | 81 | BuiltinInt::I8 => name![i8], |
87 | (Signedness::Signed, IntBitness::X16) => name![i16], | 82 | BuiltinInt::I16 => name![i16], |
88 | (Signedness::Signed, IntBitness::X32) => name![i32], | 83 | BuiltinInt::I32 => name![i32], |
89 | (Signedness::Signed, IntBitness::X64) => name![i64], | 84 | BuiltinInt::I64 => name![i64], |
90 | (Signedness::Signed, IntBitness::X128) => name![i128], | 85 | BuiltinInt::I128 => name![i128], |
91 | 86 | }, | |
92 | (Signedness::Unsigned, IntBitness::Xsize) => name![usize], | 87 | BuiltinType::Uint(it) => match it { |
93 | (Signedness::Unsigned, IntBitness::X8) => name![u8], | 88 | BuiltinUint::Usize => name![usize], |
94 | (Signedness::Unsigned, IntBitness::X16) => name![u16], | 89 | BuiltinUint::U8 => name![u8], |
95 | (Signedness::Unsigned, IntBitness::X32) => name![u32], | 90 | BuiltinUint::U16 => name![u16], |
96 | (Signedness::Unsigned, IntBitness::X64) => name![u64], | 91 | BuiltinUint::U32 => name![u32], |
97 | (Signedness::Unsigned, IntBitness::X128) => name![u128], | 92 | BuiltinUint::U64 => name![u64], |
93 | BuiltinUint::U128 => name![u128], | ||
98 | }, | 94 | }, |
99 | BuiltinType::Float(BuiltinFloat { bitness }) => match bitness { | 95 | BuiltinType::Float(it) => match it { |
100 | FloatBitness::X32 => name![f32], | 96 | BuiltinFloat::F32 => name![f32], |
101 | FloatBitness::X64 => name![f64], | 97 | BuiltinFloat::F64 => name![f64], |
102 | }, | 98 | }, |
103 | } | 99 | } |
104 | } | 100 | } |
@@ -113,31 +109,26 @@ impl fmt::Display for BuiltinType { | |||
113 | 109 | ||
114 | #[rustfmt::skip] | 110 | #[rustfmt::skip] |
115 | impl BuiltinInt { | 111 | impl BuiltinInt { |
116 | pub const ISIZE: BuiltinInt = BuiltinInt { signedness: Signedness::Signed, bitness: IntBitness::Xsize }; | ||
117 | pub const I8 : BuiltinInt = BuiltinInt { signedness: Signedness::Signed, bitness: IntBitness::X8 }; | ||
118 | pub const I16 : BuiltinInt = BuiltinInt { signedness: Signedness::Signed, bitness: IntBitness::X16 }; | ||
119 | pub const I32 : BuiltinInt = BuiltinInt { signedness: Signedness::Signed, bitness: IntBitness::X32 }; | ||
120 | pub const I64 : BuiltinInt = BuiltinInt { signedness: Signedness::Signed, bitness: IntBitness::X64 }; | ||
121 | pub const I128 : BuiltinInt = BuiltinInt { signedness: Signedness::Signed, bitness: IntBitness::X128 }; | ||
122 | |||
123 | pub const USIZE: BuiltinInt = BuiltinInt { signedness: Signedness::Unsigned, bitness: IntBitness::Xsize }; | ||
124 | pub const U8 : BuiltinInt = BuiltinInt { signedness: Signedness::Unsigned, bitness: IntBitness::X8 }; | ||
125 | pub const U16 : BuiltinInt = BuiltinInt { signedness: Signedness::Unsigned, bitness: IntBitness::X16 }; | ||
126 | pub const U32 : BuiltinInt = BuiltinInt { signedness: Signedness::Unsigned, bitness: IntBitness::X32 }; | ||
127 | pub const U64 : BuiltinInt = BuiltinInt { signedness: Signedness::Unsigned, bitness: IntBitness::X64 }; | ||
128 | pub const U128 : BuiltinInt = BuiltinInt { signedness: Signedness::Unsigned, bitness: IntBitness::X128 }; | ||
129 | |||
130 | |||
131 | pub fn from_suffix(suffix: &str) -> Option<BuiltinInt> { | 112 | pub fn from_suffix(suffix: &str) -> Option<BuiltinInt> { |
132 | let res = match suffix { | 113 | let res = match suffix { |
133 | "isize" => Self::ISIZE, | 114 | "isize" => Self::Isize, |
134 | "i8" => Self::I8, | 115 | "i8" => Self::I8, |
135 | "i16" => Self::I16, | 116 | "i16" => Self::I16, |
136 | "i32" => Self::I32, | 117 | "i32" => Self::I32, |
137 | "i64" => Self::I64, | 118 | "i64" => Self::I64, |
138 | "i128" => Self::I128, | 119 | "i128" => Self::I128, |
139 | 120 | ||
140 | "usize" => Self::USIZE, | 121 | _ => return None, |
122 | }; | ||
123 | Some(res) | ||
124 | } | ||
125 | } | ||
126 | |||
127 | #[rustfmt::skip] | ||
128 | impl BuiltinUint { | ||
129 | pub fn from_suffix(suffix: &str) -> Option<BuiltinUint> { | ||
130 | let res = match suffix { | ||
131 | "usize" => Self::Usize, | ||
141 | "u8" => Self::U8, | 132 | "u8" => Self::U8, |
142 | "u16" => Self::U16, | 133 | "u16" => Self::U16, |
143 | "u32" => Self::U32, | 134 | "u32" => Self::U32, |
@@ -152,9 +143,6 @@ impl BuiltinInt { | |||
152 | 143 | ||
153 | #[rustfmt::skip] | 144 | #[rustfmt::skip] |
154 | impl BuiltinFloat { | 145 | impl BuiltinFloat { |
155 | pub const F32: BuiltinFloat = BuiltinFloat { bitness: FloatBitness::X32 }; | ||
156 | pub const F64: BuiltinFloat = BuiltinFloat { bitness: FloatBitness::X64 }; | ||
157 | |||
158 | pub fn from_suffix(suffix: &str) -> Option<BuiltinFloat> { | 146 | pub fn from_suffix(suffix: &str) -> Option<BuiltinFloat> { |
159 | let res = match suffix { | 147 | let res = match suffix { |
160 | "f32" => BuiltinFloat::F32, | 148 | "f32" => BuiltinFloat::F32, |
diff --git a/crates/hir_def/src/diagnostics.rs b/crates/hir_def/src/diagnostics.rs index ab3f059ce..ac7474f63 100644 --- a/crates/hir_def/src/diagnostics.rs +++ b/crates/hir_def/src/diagnostics.rs | |||
@@ -95,6 +95,34 @@ impl Diagnostic for UnresolvedImport { | |||
95 | } | 95 | } |
96 | } | 96 | } |
97 | 97 | ||
98 | // Diagnostic: unresolved-macro-call | ||
99 | // | ||
100 | // This diagnostic is triggered if rust-analyzer is unable to resolove path to a | ||
101 | // macro in a macro invocation. | ||
102 | #[derive(Debug)] | ||
103 | pub struct UnresolvedMacroCall { | ||
104 | pub file: HirFileId, | ||
105 | pub node: AstPtr<ast::MacroCall>, | ||
106 | } | ||
107 | |||
108 | impl Diagnostic for UnresolvedMacroCall { | ||
109 | fn code(&self) -> DiagnosticCode { | ||
110 | DiagnosticCode("unresolved-macro-call") | ||
111 | } | ||
112 | fn message(&self) -> String { | ||
113 | "unresolved macro call".to_string() | ||
114 | } | ||
115 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | ||
116 | InFile::new(self.file, self.node.clone().into()) | ||
117 | } | ||
118 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
119 | self | ||
120 | } | ||
121 | fn is_experimental(&self) -> bool { | ||
122 | true | ||
123 | } | ||
124 | } | ||
125 | |||
98 | // Diagnostic: inactive-code | 126 | // Diagnostic: inactive-code |
99 | // | 127 | // |
100 | // This diagnostic is shown for code with inactive `#[cfg]` attributes. | 128 | // This diagnostic is shown for code with inactive `#[cfg]` attributes. |
diff --git a/crates/hir_def/src/expr.rs b/crates/hir_def/src/expr.rs index 4d72eaeaf..24be93773 100644 --- a/crates/hir_def/src/expr.rs +++ b/crates/hir_def/src/expr.rs | |||
@@ -17,7 +17,7 @@ use la_arena::{Idx, RawIdx}; | |||
17 | use syntax::ast::RangeOp; | 17 | use syntax::ast::RangeOp; |
18 | 18 | ||
19 | use crate::{ | 19 | use crate::{ |
20 | builtin_type::{BuiltinFloat, BuiltinInt}, | 20 | builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint}, |
21 | path::{GenericArgs, Path}, | 21 | path::{GenericArgs, Path}, |
22 | type_ref::{Mutability, Rawness, TypeRef}, | 22 | type_ref::{Mutability, Rawness, TypeRef}, |
23 | BlockId, | 23 | BlockId, |
@@ -43,6 +43,7 @@ pub enum Literal { | |||
43 | Char(char), | 43 | Char(char), |
44 | Bool(bool), | 44 | Bool(bool), |
45 | Int(u64, Option<BuiltinInt>), | 45 | Int(u64, Option<BuiltinInt>), |
46 | Uint(u64, Option<BuiltinUint>), | ||
46 | Float(u64, Option<BuiltinFloat>), // FIXME: f64 is not Eq | 47 | Float(u64, Option<BuiltinFloat>), // FIXME: f64 is not Eq |
47 | } | 48 | } |
48 | 49 | ||
diff --git a/crates/hir_def/src/find_path.rs b/crates/hir_def/src/find_path.rs index 5e2a711b8..3e19a7702 100644 --- a/crates/hir_def/src/find_path.rs +++ b/crates/hir_def/src/find_path.rs | |||
@@ -1,5 +1,7 @@ | |||
1 | //! An algorithm to find a path to refer to a certain item. | 1 | //! An algorithm to find a path to refer to a certain item. |
2 | 2 | ||
3 | use std::iter; | ||
4 | |||
3 | use hir_expand::name::{known, AsName, Name}; | 5 | use hir_expand::name::{known, AsName, Name}; |
4 | use rustc_hash::FxHashSet; | 6 | use rustc_hash::FxHashSet; |
5 | use test_utils::mark; | 7 | use test_utils::mark; |
@@ -95,7 +97,7 @@ fn find_path_inner( | |||
95 | item: ItemInNs, | 97 | item: ItemInNs, |
96 | from: ModuleId, | 98 | from: ModuleId, |
97 | max_len: usize, | 99 | max_len: usize, |
98 | prefixed: Option<PrefixKind>, | 100 | mut prefixed: Option<PrefixKind>, |
99 | ) -> Option<ModPath> { | 101 | ) -> Option<ModPath> { |
100 | if max_len == 0 { | 102 | if max_len == 0 { |
101 | return None; | 103 | return None; |
@@ -114,8 +116,9 @@ fn find_path_inner( | |||
114 | } | 116 | } |
115 | 117 | ||
116 | // - if the item is the crate root, return `crate` | 118 | // - if the item is the crate root, return `crate` |
117 | let root = def_map.module_id(def_map.root()); | 119 | let root = def_map.crate_root(db); |
118 | if item == ItemInNs::Types(ModuleDefId::ModuleId(root)) && def_map.block_id().is_none() { | 120 | if item == ItemInNs::Types(ModuleDefId::ModuleId(root)) && def_map.block_id().is_none() { |
121 | // FIXME: the `block_id()` check should be unnecessary, but affects the result | ||
119 | return Some(ModPath::from_segments(PathKind::Crate, Vec::new())); | 122 | return Some(ModPath::from_segments(PathKind::Crate, Vec::new())); |
120 | } | 123 | } |
121 | 124 | ||
@@ -165,7 +168,7 @@ fn find_path_inner( | |||
165 | 168 | ||
166 | // - otherwise, look for modules containing (reexporting) it and import it from one of those | 169 | // - otherwise, look for modules containing (reexporting) it and import it from one of those |
167 | 170 | ||
168 | let crate_root = def_map.module_id(def_map.root()); | 171 | let crate_root = def_map.crate_root(db); |
169 | let crate_attrs = db.attrs(crate_root.into()); | 172 | let crate_attrs = db.attrs(crate_root.into()); |
170 | let prefer_no_std = crate_attrs.by_key("no_std").exists(); | 173 | let prefer_no_std = crate_attrs.by_key("no_std").exists(); |
171 | let mut best_path = None; | 174 | let mut best_path = None; |
@@ -228,12 +231,16 @@ fn find_path_inner( | |||
228 | } | 231 | } |
229 | } | 232 | } |
230 | 233 | ||
231 | if let Some(mut prefix) = prefixed.map(PrefixKind::prefix) { | 234 | // If the item is declared inside a block expression, don't use a prefix, as we don't handle |
232 | if matches!(prefix, PathKind::Crate | PathKind::Super(0)) && def_map.block_id().is_some() { | 235 | // that correctly (FIXME). |
233 | // Inner items cannot be referred to via `crate::` or `self::` paths. | 236 | if let Some(item_module) = item.as_module_def_id().and_then(|did| did.module(db)) { |
234 | prefix = PathKind::Plain; | 237 | if item_module.def_map(db).block_id().is_some() && prefixed.is_some() { |
238 | mark::hit!(prefixed_in_block_expression); | ||
239 | prefixed = Some(PrefixKind::Plain); | ||
235 | } | 240 | } |
241 | } | ||
236 | 242 | ||
243 | if let Some(prefix) = prefixed.map(PrefixKind::prefix) { | ||
237 | best_path.or_else(|| { | 244 | best_path.or_else(|| { |
238 | scope_name.map(|scope_name| ModPath::from_segments(prefix, vec![scope_name])) | 245 | scope_name.map(|scope_name| ModPath::from_segments(prefix, vec![scope_name])) |
239 | }) | 246 | }) |
@@ -285,12 +292,12 @@ fn find_local_import_locations( | |||
285 | let data = &def_map[from.local_id]; | 292 | let data = &def_map[from.local_id]; |
286 | let mut worklist = | 293 | let mut worklist = |
287 | data.children.values().map(|child| def_map.module_id(*child)).collect::<Vec<_>>(); | 294 | data.children.values().map(|child| def_map.module_id(*child)).collect::<Vec<_>>(); |
288 | let mut parent = data.parent; | 295 | for ancestor in iter::successors(from.containing_module(db), |m| m.containing_module(db)) { |
289 | while let Some(p) = parent { | 296 | worklist.push(ancestor); |
290 | worklist.push(def_map.module_id(p)); | ||
291 | parent = def_map[p].parent; | ||
292 | } | 297 | } |
293 | 298 | ||
299 | let def_map = def_map.crate_root(db).def_map(db); | ||
300 | |||
294 | let mut seen: FxHashSet<_> = FxHashSet::default(); | 301 | let mut seen: FxHashSet<_> = FxHashSet::default(); |
295 | 302 | ||
296 | let mut locations = Vec::new(); | 303 | let mut locations = Vec::new(); |
@@ -301,7 +308,14 @@ fn find_local_import_locations( | |||
301 | 308 | ||
302 | let ext_def_map; | 309 | let ext_def_map; |
303 | let data = if module.krate == from.krate { | 310 | let data = if module.krate == from.krate { |
304 | &def_map[module.local_id] | 311 | if module.block.is_some() { |
312 | // Re-query the block's DefMap | ||
313 | ext_def_map = module.def_map(db); | ||
314 | &ext_def_map[module.local_id] | ||
315 | } else { | ||
316 | // Reuse the root DefMap | ||
317 | &def_map[module.local_id] | ||
318 | } | ||
305 | } else { | 319 | } else { |
306 | // The crate might reexport a module defined in another crate. | 320 | // The crate might reexport a module defined in another crate. |
307 | ext_def_map = module.def_map(db); | 321 | ext_def_map = module.def_map(db); |
@@ -828,6 +842,7 @@ mod tests { | |||
828 | 842 | ||
829 | #[test] | 843 | #[test] |
830 | fn inner_items_from_inner_module() { | 844 | fn inner_items_from_inner_module() { |
845 | mark::check!(prefixed_in_block_expression); | ||
831 | check_found_path( | 846 | check_found_path( |
832 | r#" | 847 | r#" |
833 | fn main() { | 848 | fn main() { |
@@ -869,4 +884,24 @@ mod tests { | |||
869 | "super::Struct", | 884 | "super::Struct", |
870 | ); | 885 | ); |
871 | } | 886 | } |
887 | |||
888 | #[test] | ||
889 | fn outer_items_with_inner_items_present() { | ||
890 | check_found_path( | ||
891 | r#" | ||
892 | mod module { | ||
893 | pub struct CompleteMe; | ||
894 | } | ||
895 | |||
896 | fn main() { | ||
897 | fn inner() {} | ||
898 | $0 | ||
899 | } | ||
900 | "#, | ||
901 | "module::CompleteMe", | ||
902 | "module::CompleteMe", | ||
903 | "crate::module::CompleteMe", | ||
904 | "self::module::CompleteMe", | ||
905 | ) | ||
906 | } | ||
872 | } | 907 | } |
diff --git a/crates/hir_def/src/item_scope.rs b/crates/hir_def/src/item_scope.rs index ee46c3330..4e5daa2ff 100644 --- a/crates/hir_def/src/item_scope.rs +++ b/crates/hir_def/src/item_scope.rs | |||
@@ -12,8 +12,8 @@ use stdx::format_to; | |||
12 | use test_utils::mark; | 12 | use test_utils::mark; |
13 | 13 | ||
14 | use crate::{ | 14 | use crate::{ |
15 | db::DefDatabase, per_ns::PerNs, visibility::Visibility, AdtId, BuiltinType, HasModule, ImplId, | 15 | db::DefDatabase, per_ns::PerNs, visibility::Visibility, AdtId, BuiltinType, ImplId, |
16 | LocalModuleId, Lookup, MacroDefId, ModuleDefId, ModuleId, TraitId, | 16 | LocalModuleId, MacroDefId, ModuleDefId, ModuleId, TraitId, |
17 | }; | 17 | }; |
18 | 18 | ||
19 | #[derive(Copy, Clone)] | 19 | #[derive(Copy, Clone)] |
@@ -375,19 +375,9 @@ impl ItemInNs { | |||
375 | 375 | ||
376 | /// Returns the crate defining this item (or `None` if `self` is built-in). | 376 | /// Returns the crate defining this item (or `None` if `self` is built-in). |
377 | pub fn krate(&self, db: &dyn DefDatabase) -> Option<CrateId> { | 377 | pub fn krate(&self, db: &dyn DefDatabase) -> Option<CrateId> { |
378 | Some(match self { | 378 | match self { |
379 | ItemInNs::Types(did) | ItemInNs::Values(did) => match did { | 379 | ItemInNs::Types(did) | ItemInNs::Values(did) => did.module(db).map(|m| m.krate), |
380 | ModuleDefId::ModuleId(id) => id.krate, | 380 | ItemInNs::Macros(id) => Some(id.krate), |
381 | ModuleDefId::FunctionId(id) => id.lookup(db).module(db).krate, | 381 | } |
382 | ModuleDefId::AdtId(id) => id.module(db).krate, | ||
383 | ModuleDefId::EnumVariantId(id) => id.parent.lookup(db).container.module(db).krate, | ||
384 | ModuleDefId::ConstId(id) => id.lookup(db).container.module(db).krate, | ||
385 | ModuleDefId::StaticId(id) => id.lookup(db).container.module(db).krate, | ||
386 | ModuleDefId::TraitId(id) => id.lookup(db).container.module(db).krate, | ||
387 | ModuleDefId::TypeAliasId(id) => id.lookup(db).module(db).krate, | ||
388 | ModuleDefId::BuiltinType(_) => return None, | ||
389 | }, | ||
390 | ItemInNs::Macros(id) => return Some(id.krate), | ||
391 | }) | ||
392 | } | 382 | } |
393 | } | 383 | } |
diff --git a/crates/hir_def/src/lib.rs b/crates/hir_def/src/lib.rs index b50923747..4498d94bb 100644 --- a/crates/hir_def/src/lib.rs +++ b/crates/hir_def/src/lib.rs | |||
@@ -57,8 +57,10 @@ use std::{ | |||
57 | 57 | ||
58 | use base_db::{impl_intern_key, salsa, CrateId}; | 58 | use base_db::{impl_intern_key, salsa, CrateId}; |
59 | use hir_expand::{ | 59 | use hir_expand::{ |
60 | ast_id_map::FileAstId, eager::expand_eager_macro, hygiene::Hygiene, AstId, HirFileId, InFile, | 60 | ast_id_map::FileAstId, |
61 | MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, | 61 | eager::{expand_eager_macro, ErrorEmitted}, |
62 | hygiene::Hygiene, | ||
63 | AstId, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, | ||
62 | }; | 64 | }; |
63 | use la_arena::Idx; | 65 | use la_arena::Idx; |
64 | use nameres::DefMap; | 66 | use nameres::DefMap; |
@@ -95,6 +97,10 @@ impl ModuleId { | |||
95 | pub fn krate(&self) -> CrateId { | 97 | pub fn krate(&self) -> CrateId { |
96 | self.krate | 98 | self.krate |
97 | } | 99 | } |
100 | |||
101 | pub fn containing_module(&self, db: &dyn db::DefDatabase) -> Option<ModuleId> { | ||
102 | self.def_map(db).containing_module(self.local_id) | ||
103 | } | ||
98 | } | 104 | } |
99 | 105 | ||
100 | /// An ID of a module, **local** to a specific crate | 106 | /// An ID of a module, **local** to a specific crate |
@@ -527,6 +533,25 @@ impl HasModule for StaticLoc { | |||
527 | } | 533 | } |
528 | } | 534 | } |
529 | 535 | ||
536 | impl ModuleDefId { | ||
537 | /// Returns the module containing `self` (or `self`, if `self` is itself a module). | ||
538 | /// | ||
539 | /// Returns `None` if `self` refers to a primitive type. | ||
540 | pub fn module(&self, db: &dyn db::DefDatabase) -> Option<ModuleId> { | ||
541 | Some(match self { | ||
542 | ModuleDefId::ModuleId(id) => *id, | ||
543 | ModuleDefId::FunctionId(id) => id.lookup(db).module(db), | ||
544 | ModuleDefId::AdtId(id) => id.module(db), | ||
545 | ModuleDefId::EnumVariantId(id) => id.parent.lookup(db).container.module(db), | ||
546 | ModuleDefId::ConstId(id) => id.lookup(db).container.module(db), | ||
547 | ModuleDefId::StaticId(id) => id.lookup(db).container.module(db), | ||
548 | ModuleDefId::TraitId(id) => id.lookup(db).container.module(db), | ||
549 | ModuleDefId::TypeAliasId(id) => id.lookup(db).module(db), | ||
550 | ModuleDefId::BuiltinType(_) => return None, | ||
551 | }) | ||
552 | } | ||
553 | } | ||
554 | |||
530 | impl AttrDefId { | 555 | impl AttrDefId { |
531 | pub fn krate(&self, db: &dyn db::DefDatabase) -> CrateId { | 556 | pub fn krate(&self, db: &dyn db::DefDatabase) -> CrateId { |
532 | match self { | 557 | match self { |
@@ -592,8 +617,15 @@ impl AsMacroCall for InFile<&ast::MacroCall> { | |||
592 | error_sink(mbe::ExpandError::Other("malformed macro invocation".into())); | 617 | error_sink(mbe::ExpandError::Other("malformed macro invocation".into())); |
593 | } | 618 | } |
594 | 619 | ||
595 | AstIdWithPath::new(ast_id.file_id, ast_id.value, path?) | 620 | macro_call_as_call_id( |
596 | .as_call_id_with_errors(db, krate, resolver, error_sink) | 621 | &AstIdWithPath::new(ast_id.file_id, ast_id.value, path?), |
622 | db, | ||
623 | krate, | ||
624 | resolver, | ||
625 | error_sink, | ||
626 | ) | ||
627 | .ok()? | ||
628 | .ok() | ||
597 | } | 629 | } |
598 | } | 630 | } |
599 | 631 | ||
@@ -610,61 +642,50 @@ impl<T: ast::AstNode> AstIdWithPath<T> { | |||
610 | } | 642 | } |
611 | } | 643 | } |
612 | 644 | ||
613 | impl AsMacroCall for AstIdWithPath<ast::MacroCall> { | 645 | struct UnresolvedMacro; |
614 | fn as_call_id_with_errors( | 646 | |
615 | &self, | 647 | fn macro_call_as_call_id( |
616 | db: &dyn db::DefDatabase, | 648 | call: &AstIdWithPath<ast::MacroCall>, |
617 | krate: CrateId, | 649 | db: &dyn db::DefDatabase, |
618 | resolver: impl Fn(path::ModPath) -> Option<MacroDefId>, | 650 | krate: CrateId, |
619 | error_sink: &mut dyn FnMut(mbe::ExpandError), | 651 | resolver: impl Fn(path::ModPath) -> Option<MacroDefId>, |
620 | ) -> Option<MacroCallId> { | 652 | error_sink: &mut dyn FnMut(mbe::ExpandError), |
621 | let def: MacroDefId = resolver(self.path.clone()).or_else(|| { | 653 | ) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> { |
622 | error_sink(mbe::ExpandError::Other(format!("could not resolve macro `{}`", self.path))); | 654 | let def: MacroDefId = resolver(call.path.clone()).ok_or(UnresolvedMacro)?; |
623 | None | 655 | |
624 | })?; | 656 | let res = if let MacroDefKind::BuiltInEager(_) = def.kind { |
625 | 657 | let macro_call = InFile::new(call.ast_id.file_id, call.ast_id.to_node(db.upcast())); | |
626 | if let MacroDefKind::BuiltInEager(_) = def.kind { | 658 | let hygiene = Hygiene::new(db.upcast(), call.ast_id.file_id); |
627 | let macro_call = InFile::new(self.ast_id.file_id, self.ast_id.to_node(db.upcast())); | 659 | |
628 | let hygiene = Hygiene::new(db.upcast(), self.ast_id.file_id); | 660 | expand_eager_macro( |
629 | 661 | db.upcast(), | |
630 | Some( | 662 | krate, |
631 | expand_eager_macro( | 663 | macro_call, |
632 | db.upcast(), | 664 | def, |
633 | krate, | 665 | &|path: ast::Path| resolver(path::ModPath::from_src(path, &hygiene)?), |
634 | macro_call, | 666 | error_sink, |
635 | def, | 667 | ) |
636 | &|path: ast::Path| resolver(path::ModPath::from_src(path, &hygiene)?), | 668 | .map(MacroCallId::from) |
637 | error_sink, | 669 | } else { |
638 | ) | 670 | Ok(def.as_lazy_macro(db.upcast(), krate, MacroCallKind::FnLike(call.ast_id)).into()) |
639 | .ok()? | 671 | }; |
640 | .into(), | 672 | Ok(res) |
641 | ) | ||
642 | } else { | ||
643 | Some(def.as_lazy_macro(db.upcast(), krate, MacroCallKind::FnLike(self.ast_id)).into()) | ||
644 | } | ||
645 | } | ||
646 | } | 673 | } |
647 | 674 | ||
648 | impl AsMacroCall for AstIdWithPath<ast::Item> { | 675 | fn item_attr_as_call_id( |
649 | fn as_call_id_with_errors( | 676 | item_attr: &AstIdWithPath<ast::Item>, |
650 | &self, | 677 | db: &dyn db::DefDatabase, |
651 | db: &dyn db::DefDatabase, | 678 | krate: CrateId, |
652 | krate: CrateId, | 679 | resolver: impl Fn(path::ModPath) -> Option<MacroDefId>, |
653 | resolver: impl Fn(path::ModPath) -> Option<MacroDefId>, | 680 | ) -> Result<MacroCallId, UnresolvedMacro> { |
654 | error_sink: &mut dyn FnMut(mbe::ExpandError), | 681 | let def: MacroDefId = resolver(item_attr.path.clone()).ok_or(UnresolvedMacro)?; |
655 | ) -> Option<MacroCallId> { | 682 | let last_segment = item_attr.path.segments().last().ok_or(UnresolvedMacro)?; |
656 | let def: MacroDefId = resolver(self.path.clone()).or_else(|| { | 683 | let res = def |
657 | error_sink(mbe::ExpandError::Other(format!("could not resolve macro `{}`", self.path))); | 684 | .as_lazy_macro( |
658 | None | 685 | db.upcast(), |
659 | })?; | 686 | krate, |
660 | 687 | MacroCallKind::Attr(item_attr.ast_id, last_segment.to_string()), | |
661 | Some( | ||
662 | def.as_lazy_macro( | ||
663 | db.upcast(), | ||
664 | krate, | ||
665 | MacroCallKind::Attr(self.ast_id, self.path.segments().last()?.to_string()), | ||
666 | ) | ||
667 | .into(), | ||
668 | ) | 688 | ) |
669 | } | 689 | .into(); |
690 | Ok(res) | ||
670 | } | 691 | } |
diff --git a/crates/hir_def/src/nameres.rs b/crates/hir_def/src/nameres.rs index f92232eb3..003d668ca 100644 --- a/crates/hir_def/src/nameres.rs +++ b/crates/hir_def/src/nameres.rs | |||
@@ -343,6 +343,18 @@ impl DefMap { | |||
343 | Some(self.block?.parent) | 343 | Some(self.block?.parent) |
344 | } | 344 | } |
345 | 345 | ||
346 | /// Returns the module containing `local_mod`, either the parent `mod`, or the module containing | ||
347 | /// the block, if `self` corresponds to a block expression. | ||
348 | pub fn containing_module(&self, local_mod: LocalModuleId) -> Option<ModuleId> { | ||
349 | match &self[local_mod].parent { | ||
350 | Some(parent) => Some(self.module_id(*parent)), | ||
351 | None => match &self.block { | ||
352 | Some(block) => Some(block.parent), | ||
353 | None => None, | ||
354 | }, | ||
355 | } | ||
356 | } | ||
357 | |||
346 | // FIXME: this can use some more human-readable format (ideally, an IR | 358 | // FIXME: this can use some more human-readable format (ideally, an IR |
347 | // even), as this should be a great debugging aid. | 359 | // even), as this should be a great debugging aid. |
348 | pub fn dump(&self, db: &dyn DefDatabase) -> String { | 360 | pub fn dump(&self, db: &dyn DefDatabase) -> String { |
@@ -417,6 +429,8 @@ mod diagnostics { | |||
417 | 429 | ||
418 | UnresolvedProcMacro { ast: MacroCallKind }, | 430 | UnresolvedProcMacro { ast: MacroCallKind }, |
419 | 431 | ||
432 | UnresolvedMacroCall { ast: AstId<ast::MacroCall> }, | ||
433 | |||
420 | MacroError { ast: MacroCallKind, message: String }, | 434 | MacroError { ast: MacroCallKind, message: String }, |
421 | } | 435 | } |
422 | 436 | ||
@@ -477,6 +491,13 @@ mod diagnostics { | |||
477 | Self { in_module: container, kind: DiagnosticKind::MacroError { ast, message } } | 491 | Self { in_module: container, kind: DiagnosticKind::MacroError { ast, message } } |
478 | } | 492 | } |
479 | 493 | ||
494 | pub(super) fn unresolved_macro_call( | ||
495 | container: LocalModuleId, | ||
496 | ast: AstId<ast::MacroCall>, | ||
497 | ) -> Self { | ||
498 | Self { in_module: container, kind: DiagnosticKind::UnresolvedMacroCall { ast } } | ||
499 | } | ||
500 | |||
480 | pub(super) fn add_to( | 501 | pub(super) fn add_to( |
481 | &self, | 502 | &self, |
482 | db: &dyn DefDatabase, | 503 | db: &dyn DefDatabase, |
@@ -589,6 +610,11 @@ mod diagnostics { | |||
589 | }); | 610 | }); |
590 | } | 611 | } |
591 | 612 | ||
613 | DiagnosticKind::UnresolvedMacroCall { ast } => { | ||
614 | let node = ast.to_node(db.upcast()); | ||
615 | sink.push(UnresolvedMacroCall { file: ast.file_id, node: AstPtr::new(&node) }); | ||
616 | } | ||
617 | |||
592 | DiagnosticKind::MacroError { ast, message } => { | 618 | DiagnosticKind::MacroError { ast, message } => { |
593 | let (file, ast) = match ast { | 619 | let (file, ast) = match ast { |
594 | MacroCallKind::FnLike(ast) => { | 620 | MacroCallKind::FnLike(ast) => { |
diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index 9996a0807..e51d89b43 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs | |||
@@ -13,7 +13,7 @@ use hir_expand::{ | |||
13 | builtin_macro::find_builtin_macro, | 13 | builtin_macro::find_builtin_macro, |
14 | name::{AsName, Name}, | 14 | name::{AsName, Name}, |
15 | proc_macro::ProcMacroExpander, | 15 | proc_macro::ProcMacroExpander, |
16 | HirFileId, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, | 16 | HirFileId, MacroCallId, MacroDefId, MacroDefKind, |
17 | }; | 17 | }; |
18 | use hir_expand::{InFile, MacroCallLoc}; | 18 | use hir_expand::{InFile, MacroCallLoc}; |
19 | use rustc_hash::{FxHashMap, FxHashSet}; | 19 | use rustc_hash::{FxHashMap, FxHashSet}; |
@@ -24,11 +24,13 @@ use tt::{Leaf, TokenTree}; | |||
24 | use crate::{ | 24 | use crate::{ |
25 | attr::Attrs, | 25 | attr::Attrs, |
26 | db::DefDatabase, | 26 | db::DefDatabase, |
27 | item_attr_as_call_id, | ||
27 | item_scope::{ImportType, PerNsGlobImports}, | 28 | item_scope::{ImportType, PerNsGlobImports}, |
28 | item_tree::{ | 29 | item_tree::{ |
29 | self, FileItemTreeId, ItemTree, ItemTreeId, MacroCall, MacroRules, Mod, ModItem, ModKind, | 30 | self, FileItemTreeId, ItemTree, ItemTreeId, MacroCall, MacroRules, Mod, ModItem, ModKind, |
30 | StructDefKind, | 31 | StructDefKind, |
31 | }, | 32 | }, |
33 | macro_call_as_call_id, | ||
32 | nameres::{ | 34 | nameres::{ |
33 | diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint, | 35 | diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint, |
34 | BuiltinShadowMode, DefMap, ModuleData, ModuleOrigin, ResolveMode, | 36 | BuiltinShadowMode, DefMap, ModuleData, ModuleOrigin, ResolveMode, |
@@ -36,9 +38,9 @@ use crate::{ | |||
36 | path::{ImportAlias, ModPath, PathKind}, | 38 | path::{ImportAlias, ModPath, PathKind}, |
37 | per_ns::PerNs, | 39 | per_ns::PerNs, |
38 | visibility::{RawVisibility, Visibility}, | 40 | visibility::{RawVisibility, Visibility}, |
39 | AdtId, AsMacroCall, AstId, AstIdWithPath, ConstLoc, ContainerId, EnumLoc, EnumVariantId, | 41 | AdtId, AstId, AstIdWithPath, ConstLoc, ContainerId, EnumLoc, EnumVariantId, FunctionLoc, |
40 | FunctionLoc, ImplLoc, Intern, LocalModuleId, ModuleDefId, StaticLoc, StructLoc, TraitLoc, | 42 | ImplLoc, Intern, LocalModuleId, ModuleDefId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, |
41 | TypeAliasLoc, UnionLoc, | 43 | UnionLoc, UnresolvedMacro, |
42 | }; | 44 | }; |
43 | 45 | ||
44 | const GLOB_RECURSION_LIMIT: usize = 100; | 46 | const GLOB_RECURSION_LIMIT: usize = 100; |
@@ -790,8 +792,11 @@ impl DefCollector<'_> { | |||
790 | return false; | 792 | return false; |
791 | } | 793 | } |
792 | 794 | ||
793 | if let Some(call_id) = | 795 | match macro_call_as_call_id( |
794 | directive.ast_id.as_call_id(self.db, self.def_map.krate, |path| { | 796 | &directive.ast_id, |
797 | self.db, | ||
798 | self.def_map.krate, | ||
799 | |path| { | ||
795 | let resolved_res = self.def_map.resolve_path_fp_with_macro( | 800 | let resolved_res = self.def_map.resolve_path_fp_with_macro( |
796 | self.db, | 801 | self.db, |
797 | ResolveMode::Other, | 802 | ResolveMode::Other, |
@@ -800,24 +805,29 @@ impl DefCollector<'_> { | |||
800 | BuiltinShadowMode::Module, | 805 | BuiltinShadowMode::Module, |
801 | ); | 806 | ); |
802 | resolved_res.resolved_def.take_macros() | 807 | resolved_res.resolved_def.take_macros() |
803 | }) | 808 | }, |
804 | { | 809 | &mut |_err| (), |
805 | resolved.push((directive.module_id, call_id, directive.depth)); | 810 | ) { |
806 | res = ReachedFixedPoint::No; | 811 | Ok(Ok(call_id)) => { |
807 | return false; | 812 | resolved.push((directive.module_id, call_id, directive.depth)); |
813 | res = ReachedFixedPoint::No; | ||
814 | return false; | ||
815 | } | ||
816 | Err(UnresolvedMacro) | Ok(Err(_)) => {} | ||
808 | } | 817 | } |
809 | 818 | ||
810 | true | 819 | true |
811 | }); | 820 | }); |
812 | attribute_macros.retain(|directive| { | 821 | attribute_macros.retain(|directive| { |
813 | if let Some(call_id) = | 822 | match item_attr_as_call_id(&directive.ast_id, self.db, self.def_map.krate, |path| { |
814 | directive.ast_id.as_call_id(self.db, self.def_map.krate, |path| { | 823 | self.resolve_attribute_macro(&directive, &path) |
815 | self.resolve_attribute_macro(&directive, &path) | 824 | }) { |
816 | }) | 825 | Ok(call_id) => { |
817 | { | 826 | resolved.push((directive.module_id, call_id, 0)); |
818 | resolved.push((directive.module_id, call_id, 0)); | 827 | res = ReachedFixedPoint::No; |
819 | res = ReachedFixedPoint::No; | 828 | return false; |
820 | return false; | 829 | } |
830 | Err(UnresolvedMacro) => (), | ||
821 | } | 831 | } |
822 | 832 | ||
823 | true | 833 | true |
@@ -902,7 +912,8 @@ impl DefCollector<'_> { | |||
902 | 912 | ||
903 | for directive in &self.unexpanded_macros { | 913 | for directive in &self.unexpanded_macros { |
904 | let mut error = None; | 914 | let mut error = None; |
905 | directive.ast_id.as_call_id_with_errors( | 915 | match macro_call_as_call_id( |
916 | &directive.ast_id, | ||
906 | self.db, | 917 | self.db, |
907 | self.def_map.krate, | 918 | self.def_map.krate, |
908 | |path| { | 919 | |path| { |
@@ -918,15 +929,15 @@ impl DefCollector<'_> { | |||
918 | &mut |e| { | 929 | &mut |e| { |
919 | error.get_or_insert(e); | 930 | error.get_or_insert(e); |
920 | }, | 931 | }, |
921 | ); | 932 | ) { |
922 | 933 | Ok(_) => (), | |
923 | if let Some(err) = error { | 934 | Err(UnresolvedMacro) => { |
924 | self.def_map.diagnostics.push(DefDiagnostic::macro_error( | 935 | self.def_map.diagnostics.push(DefDiagnostic::unresolved_macro_call( |
925 | directive.module_id, | 936 | directive.module_id, |
926 | MacroCallKind::FnLike(directive.ast_id.ast_id), | 937 | directive.ast_id.ast_id, |
927 | err.to_string(), | 938 | )); |
928 | )); | 939 | } |
929 | } | 940 | }; |
930 | } | 941 | } |
931 | 942 | ||
932 | // Emit diagnostics for all remaining unresolved imports. | 943 | // Emit diagnostics for all remaining unresolved imports. |
@@ -1446,8 +1457,11 @@ impl ModCollector<'_, '_> { | |||
1446 | let mut ast_id = AstIdWithPath::new(self.file_id, mac.ast_id, mac.path.clone()); | 1457 | let mut ast_id = AstIdWithPath::new(self.file_id, mac.ast_id, mac.path.clone()); |
1447 | 1458 | ||
1448 | // Case 1: try to resolve in legacy scope and expand macro_rules | 1459 | // Case 1: try to resolve in legacy scope and expand macro_rules |
1449 | if let Some(macro_call_id) = | 1460 | if let Ok(Ok(macro_call_id)) = macro_call_as_call_id( |
1450 | ast_id.as_call_id(self.def_collector.db, self.def_collector.def_map.krate, |path| { | 1461 | &ast_id, |
1462 | self.def_collector.db, | ||
1463 | self.def_collector.def_map.krate, | ||
1464 | |path| { | ||
1451 | path.as_ident().and_then(|name| { | 1465 | path.as_ident().and_then(|name| { |
1452 | self.def_collector.def_map.with_ancestor_maps( | 1466 | self.def_collector.def_map.with_ancestor_maps( |
1453 | self.def_collector.db, | 1467 | self.def_collector.db, |
@@ -1455,8 +1469,9 @@ impl ModCollector<'_, '_> { | |||
1455 | &mut |map, module| map[module].scope.get_legacy_macro(&name), | 1469 | &mut |map, module| map[module].scope.get_legacy_macro(&name), |
1456 | ) | 1470 | ) |
1457 | }) | 1471 | }) |
1458 | }) | 1472 | }, |
1459 | { | 1473 | &mut |_err| (), |
1474 | ) { | ||
1460 | self.def_collector.unexpanded_macros.push(MacroDirective { | 1475 | self.def_collector.unexpanded_macros.push(MacroDirective { |
1461 | module_id: self.module_id, | 1476 | module_id: self.module_id, |
1462 | ast_id, | 1477 | ast_id, |
diff --git a/crates/hir_def/src/nameres/path_resolution.rs b/crates/hir_def/src/nameres/path_resolution.rs index fdcdc23ae..dd1db0094 100644 --- a/crates/hir_def/src/nameres/path_resolution.rs +++ b/crates/hir_def/src/nameres/path_resolution.rs | |||
@@ -77,7 +77,7 @@ impl DefMap { | |||
77 | original_module: LocalModuleId, | 77 | original_module: LocalModuleId, |
78 | visibility: &RawVisibility, | 78 | visibility: &RawVisibility, |
79 | ) -> Option<Visibility> { | 79 | ) -> Option<Visibility> { |
80 | match visibility { | 80 | let mut vis = match visibility { |
81 | RawVisibility::Module(path) => { | 81 | RawVisibility::Module(path) => { |
82 | let (result, remaining) = | 82 | let (result, remaining) = |
83 | self.resolve_path(db, original_module, &path, BuiltinShadowMode::Module); | 83 | self.resolve_path(db, original_module, &path, BuiltinShadowMode::Module); |
@@ -86,15 +86,28 @@ impl DefMap { | |||
86 | } | 86 | } |
87 | let types = result.take_types()?; | 87 | let types = result.take_types()?; |
88 | match types { | 88 | match types { |
89 | ModuleDefId::ModuleId(m) => Some(Visibility::Module(m)), | 89 | ModuleDefId::ModuleId(m) => Visibility::Module(m), |
90 | _ => { | 90 | _ => { |
91 | // error: visibility needs to refer to module | 91 | // error: visibility needs to refer to module |
92 | None | 92 | return None; |
93 | } | 93 | } |
94 | } | 94 | } |
95 | } | 95 | } |
96 | RawVisibility::Public => Some(Visibility::Public), | 96 | RawVisibility::Public => Visibility::Public, |
97 | }; | ||
98 | |||
99 | // In block expressions, `self` normally refers to the containing non-block module, and | ||
100 | // `super` to its parent (etc.). However, visibilities must only refer to a module in the | ||
101 | // DefMap they're written in, so we restrict them when that happens. | ||
102 | if let Visibility::Module(m) = vis { | ||
103 | if self.block_id() != m.block { | ||
104 | mark::hit!(adjust_vis_in_block_def_map); | ||
105 | vis = Visibility::Module(self.module_id(self.root())); | ||
106 | log::debug!("visibility {:?} points outside DefMap, adjusting to {:?}", m, vis); | ||
107 | } | ||
97 | } | 108 | } |
109 | |||
110 | Some(vis) | ||
98 | } | 111 | } |
99 | 112 | ||
100 | // Returns Yes if we are sure that additions to `ItemMap` wouldn't change | 113 | // Returns Yes if we are sure that additions to `ItemMap` wouldn't change |
diff --git a/crates/hir_def/src/path/lower.rs b/crates/hir_def/src/path/lower.rs index a469546c1..505493a74 100644 --- a/crates/hir_def/src/path/lower.rs +++ b/crates/hir_def/src/path/lower.rs | |||
@@ -101,8 +101,12 @@ pub(super) fn lower_path(mut path: ast::Path, hygiene: &Hygiene) -> Option<Path> | |||
101 | break; | 101 | break; |
102 | } | 102 | } |
103 | ast::PathSegmentKind::SelfKw => { | 103 | ast::PathSegmentKind::SelfKw => { |
104 | kind = PathKind::Super(0); | 104 | // don't break out if `self` is the last segment of a path, this mean we got an |
105 | break; | 105 | // use tree like `foo::{self}` which we want to resolve as `foo` |
106 | if !segments.is_empty() { | ||
107 | kind = PathKind::Super(0); | ||
108 | break; | ||
109 | } | ||
106 | } | 110 | } |
107 | ast::PathSegmentKind::SuperKw => { | 111 | ast::PathSegmentKind::SuperKw => { |
108 | let nested_super_count = if let PathKind::Super(n) = kind { n } else { 0 }; | 112 | let nested_super_count = if let PathKind::Super(n) = kind { n } else { 0 }; |
@@ -117,6 +121,11 @@ pub(super) fn lower_path(mut path: ast::Path, hygiene: &Hygiene) -> Option<Path> | |||
117 | segments.reverse(); | 121 | segments.reverse(); |
118 | generic_args.reverse(); | 122 | generic_args.reverse(); |
119 | 123 | ||
124 | if segments.is_empty() && kind == PathKind::Plain && type_anchor.is_none() { | ||
125 | // plain empty paths don't exist, this means we got a single `self` segment as our path | ||
126 | kind = PathKind::Super(0); | ||
127 | } | ||
128 | |||
120 | // handle local_inner_macros : | 129 | // handle local_inner_macros : |
121 | // Basically, even in rustc it is quite hacky: | 130 | // Basically, even in rustc it is quite hacky: |
122 | // https://github.com/rust-lang/rust/blob/614f273e9388ddd7804d5cbc80b8865068a3744e/src/librustc_resolve/macros.rs#L456 | 131 | // https://github.com/rust-lang/rust/blob/614f273e9388ddd7804d5cbc80b8865068a3744e/src/librustc_resolve/macros.rs#L456 |
diff --git a/crates/hir_expand/src/builtin_macro.rs b/crates/hir_expand/src/builtin_macro.rs index 57bc6fbd7..eb57ea7d6 100644 --- a/crates/hir_expand/src/builtin_macro.rs +++ b/crates/hir_expand/src/builtin_macro.rs | |||
@@ -6,7 +6,7 @@ use crate::{ | |||
6 | 6 | ||
7 | use base_db::{AnchoredPath, FileId}; | 7 | use base_db::{AnchoredPath, FileId}; |
8 | use either::Either; | 8 | use either::Either; |
9 | use mbe::{parse_to_token_tree, ExpandResult}; | 9 | use mbe::{parse_exprs_with_sep, parse_to_token_tree, ExpandResult}; |
10 | use parser::FragmentKind; | 10 | use parser::FragmentKind; |
11 | use syntax::ast::{self, AstToken}; | 11 | use syntax::ast::{self, AstToken}; |
12 | 12 | ||
@@ -182,25 +182,10 @@ fn assert_expand( | |||
182 | // ```, | 182 | // ```, |
183 | // which is wrong but useful. | 183 | // which is wrong but useful. |
184 | 184 | ||
185 | let mut args = Vec::new(); | 185 | let args = parse_exprs_with_sep(tt, ','); |
186 | let mut current = Vec::new(); | ||
187 | for tt in tt.token_trees.iter().cloned() { | ||
188 | match tt { | ||
189 | tt::TokenTree::Leaf(tt::Leaf::Punct(p)) if p.char == ',' => { | ||
190 | args.push(current); | ||
191 | current = Vec::new(); | ||
192 | } | ||
193 | _ => { | ||
194 | current.push(tt); | ||
195 | } | ||
196 | } | ||
197 | } | ||
198 | if !current.is_empty() { | ||
199 | args.push(current); | ||
200 | } | ||
201 | 186 | ||
202 | let arg_tts = args.into_iter().flat_map(|arg| { | 187 | let arg_tts = args.into_iter().flat_map(|arg| { |
203 | quote! { &(##arg), } | 188 | quote! { &(#arg), } |
204 | }.token_trees).collect::<Vec<_>>(); | 189 | }.token_trees).collect::<Vec<_>>(); |
205 | 190 | ||
206 | let expanded = quote! { | 191 | let expanded = quote! { |
@@ -238,35 +223,21 @@ fn format_args_expand( | |||
238 | // ]) | 223 | // ]) |
239 | // ```, | 224 | // ```, |
240 | // which is still not really correct, but close enough for now | 225 | // which is still not really correct, but close enough for now |
241 | let mut args = Vec::new(); | 226 | let mut args = parse_exprs_with_sep(tt, ','); |
242 | let mut current = Vec::new(); | 227 | |
243 | for tt in tt.token_trees.iter().cloned() { | ||
244 | match tt { | ||
245 | tt::TokenTree::Leaf(tt::Leaf::Punct(p)) if p.char == ',' => { | ||
246 | args.push(current); | ||
247 | current = Vec::new(); | ||
248 | } | ||
249 | _ => { | ||
250 | current.push(tt); | ||
251 | } | ||
252 | } | ||
253 | } | ||
254 | if !current.is_empty() { | ||
255 | args.push(current); | ||
256 | } | ||
257 | if args.is_empty() { | 228 | if args.is_empty() { |
258 | return ExpandResult::only_err(mbe::ExpandError::NoMatchingRule); | 229 | return ExpandResult::only_err(mbe::ExpandError::NoMatchingRule); |
259 | } | 230 | } |
260 | for arg in &mut args { | 231 | for arg in &mut args { |
261 | // Remove `key =`. | 232 | // Remove `key =`. |
262 | if matches!(arg.get(1), Some(tt::TokenTree::Leaf(tt::Leaf::Punct(p))) if p.char == '=' && p.spacing != tt::Spacing::Joint) | 233 | if matches!(arg.token_trees.get(1), Some(tt::TokenTree::Leaf(tt::Leaf::Punct(p))) if p.char == '=' && p.spacing != tt::Spacing::Joint) |
263 | { | 234 | { |
264 | arg.drain(..2); | 235 | arg.token_trees.drain(..2); |
265 | } | 236 | } |
266 | } | 237 | } |
267 | let _format_string = args.remove(0); | 238 | let _format_string = args.remove(0); |
268 | let arg_tts = args.into_iter().flat_map(|arg| { | 239 | let arg_tts = args.into_iter().flat_map(|arg| { |
269 | quote! { std::fmt::ArgumentV1::new(&(##arg), std::fmt::Display::fmt), } | 240 | quote! { std::fmt::ArgumentV1::new(&(#arg), std::fmt::Display::fmt), } |
270 | }.token_trees).collect::<Vec<_>>(); | 241 | }.token_trees).collect::<Vec<_>>(); |
271 | let expanded = quote! { | 242 | let expanded = quote! { |
272 | std::fmt::Arguments::new_v1(&[], &[##arg_tts]) | 243 | std::fmt::Arguments::new_v1(&[], &[##arg_tts]) |
@@ -720,6 +691,25 @@ mod tests { | |||
720 | } | 691 | } |
721 | 692 | ||
722 | #[test] | 693 | #[test] |
694 | fn test_format_args_expand_with_comma_exprs() { | ||
695 | let expanded = expand_builtin_macro( | ||
696 | r#" | ||
697 | #[rustc_builtin_macro] | ||
698 | macro_rules! format_args { | ||
699 | ($fmt:expr) => ({ /* compiler built-in */ }); | ||
700 | ($fmt:expr, $($args:tt)*) => ({ /* compiler built-in */ }) | ||
701 | } | ||
702 | format_args!("{} {:?}", a::<A,B>(), b); | ||
703 | "#, | ||
704 | ); | ||
705 | |||
706 | assert_eq!( | ||
707 | expanded, | ||
708 | r#"std::fmt::Arguments::new_v1(&[], &[std::fmt::ArgumentV1::new(&(a::<A,B>()),std::fmt::Display::fmt),std::fmt::ArgumentV1::new(&(b),std::fmt::Display::fmt),])"# | ||
709 | ); | ||
710 | } | ||
711 | |||
712 | #[test] | ||
723 | fn test_include_bytes_expand() { | 713 | fn test_include_bytes_expand() { |
724 | let expanded = expand_builtin_macro( | 714 | let expanded = expand_builtin_macro( |
725 | r#" | 715 | r#" |
diff --git a/crates/hir_expand/src/name.rs b/crates/hir_expand/src/name.rs index c7609e90d..c94fb580a 100644 --- a/crates/hir_expand/src/name.rs +++ b/crates/hir_expand/src/name.rs | |||
@@ -189,6 +189,7 @@ pub mod known { | |||
189 | // Components of known path (function name) | 189 | // Components of known path (function name) |
190 | filter_map, | 190 | filter_map, |
191 | next, | 191 | next, |
192 | iter_mut, | ||
192 | // Builtin macros | 193 | // Builtin macros |
193 | file, | 194 | file, |
194 | column, | 195 | column, |
diff --git a/crates/hir_ty/Cargo.toml b/crates/hir_ty/Cargo.toml index 6ef9d1e7e..d1302d749 100644 --- a/crates/hir_ty/Cargo.toml +++ b/crates/hir_ty/Cargo.toml | |||
@@ -17,9 +17,9 @@ ena = "0.14.0" | |||
17 | log = "0.4.8" | 17 | log = "0.4.8" |
18 | rustc-hash = "1.1.0" | 18 | rustc-hash = "1.1.0" |
19 | scoped-tls = "1" | 19 | scoped-tls = "1" |
20 | chalk-solve = { version = "0.58", default-features = false } | 20 | chalk-solve = { version = "0.59", default-features = false } |
21 | chalk-ir = "0.58" | 21 | chalk-ir = "0.59" |
22 | chalk-recursive = "0.58" | 22 | chalk-recursive = "0.59" |
23 | la-arena = { version = "0.2.0", path = "../../lib/arena" } | 23 | la-arena = { version = "0.2.0", path = "../../lib/arena" } |
24 | 24 | ||
25 | stdx = { path = "../stdx", version = "0.0.0" } | 25 | stdx = { path = "../stdx", version = "0.0.0" } |
diff --git a/crates/hir_ty/src/autoderef.rs b/crates/hir_ty/src/autoderef.rs index ece68183e..be1fd1f13 100644 --- a/crates/hir_ty/src/autoderef.rs +++ b/crates/hir_ty/src/autoderef.rs | |||
@@ -81,7 +81,7 @@ fn deref_by_trait( | |||
81 | 81 | ||
82 | // Now do the assoc type projection | 82 | // Now do the assoc type projection |
83 | let projection = super::traits::ProjectionPredicate { | 83 | let projection = super::traits::ProjectionPredicate { |
84 | ty: Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, ty.value.kinds.len())), | 84 | ty: Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, ty.value.kinds.len())), |
85 | projection_ty: super::ProjectionTy { associated_ty: target, parameters }, | 85 | projection_ty: super::ProjectionTy { associated_ty: target, parameters }, |
86 | }; | 86 | }; |
87 | 87 | ||
@@ -89,8 +89,10 @@ fn deref_by_trait( | |||
89 | 89 | ||
90 | let in_env = InEnvironment { value: obligation, environment: ty.environment }; | 90 | let in_env = InEnvironment { value: obligation, environment: ty.environment }; |
91 | 91 | ||
92 | let canonical = | 92 | let canonical = Canonical::new( |
93 | Canonical::new(in_env, ty.value.kinds.iter().copied().chain(Some(super::TyKind::General))); | 93 | in_env, |
94 | ty.value.kinds.iter().copied().chain(Some(chalk_ir::TyVariableKind::General)), | ||
95 | ); | ||
94 | 96 | ||
95 | let solution = db.trait_solve(krate, canonical)?; | 97 | let solution = db.trait_solve(krate, canonical)?; |
96 | 98 | ||
@@ -112,7 +114,8 @@ fn deref_by_trait( | |||
112 | // new variables in that case | 114 | // new variables in that case |
113 | 115 | ||
114 | for i in 1..vars.0.kinds.len() { | 116 | for i in 1..vars.0.kinds.len() { |
115 | if vars.0.value[i - 1] != Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, i - 1)) | 117 | if vars.0.value[i - 1] |
118 | != Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, i - 1)) | ||
116 | { | 119 | { |
117 | warn!("complex solution for derefing {:?}: {:?}, ignoring", ty.value, solution); | 120 | warn!("complex solution for derefing {:?}: {:?}, ignoring", ty.value, solution); |
118 | return None; | 121 | return None; |
diff --git a/crates/hir_ty/src/diagnostics/expr.rs b/crates/hir_ty/src/diagnostics/expr.rs index d740b7265..66a88e2b6 100644 --- a/crates/hir_ty/src/diagnostics/expr.rs +++ b/crates/hir_ty/src/diagnostics/expr.rs | |||
@@ -17,7 +17,7 @@ use crate::{ | |||
17 | MissingPatFields, RemoveThisSemicolon, | 17 | MissingPatFields, RemoveThisSemicolon, |
18 | }, | 18 | }, |
19 | utils::variant_data, | 19 | utils::variant_data, |
20 | ApplicationTy, InferenceResult, Ty, TypeCtor, | 20 | InferenceResult, Ty, |
21 | }; | 21 | }; |
22 | 22 | ||
23 | pub(crate) use hir_def::{ | 23 | pub(crate) use hir_def::{ |
@@ -381,14 +381,11 @@ impl<'a, 'b> ExprValidator<'a, 'b> { | |||
381 | _ => return, | 381 | _ => return, |
382 | }; | 382 | }; |
383 | 383 | ||
384 | let core_result_ctor = TypeCtor::Adt(AdtId::EnumId(core_result_enum)); | 384 | let (params, required) = match mismatch.expected { |
385 | let core_option_ctor = TypeCtor::Adt(AdtId::EnumId(core_option_enum)); | 385 | Ty::Adt(AdtId::EnumId(enum_id), ref parameters) if enum_id == core_result_enum => { |
386 | |||
387 | let (params, required) = match &mismatch.expected { | ||
388 | Ty::Apply(ApplicationTy { ctor, parameters }) if ctor == &core_result_ctor => { | ||
389 | (parameters, "Ok".to_string()) | 386 | (parameters, "Ok".to_string()) |
390 | } | 387 | } |
391 | Ty::Apply(ApplicationTy { ctor, parameters }) if ctor == &core_option_ctor => { | 388 | Ty::Adt(AdtId::EnumId(enum_id), ref parameters) if enum_id == core_option_enum => { |
392 | (parameters, "Some".to_string()) | 389 | (parameters, "Some".to_string()) |
393 | } | 390 | } |
394 | _ => return, | 391 | _ => return, |
diff --git a/crates/hir_ty/src/diagnostics/match_check.rs b/crates/hir_ty/src/diagnostics/match_check.rs index 1c1423fbf..86fee0050 100644 --- a/crates/hir_ty/src/diagnostics/match_check.rs +++ b/crates/hir_ty/src/diagnostics/match_check.rs | |||
@@ -227,7 +227,7 @@ use hir_def::{ | |||
227 | use la_arena::Idx; | 227 | use la_arena::Idx; |
228 | use smallvec::{smallvec, SmallVec}; | 228 | use smallvec::{smallvec, SmallVec}; |
229 | 229 | ||
230 | use crate::{db::HirDatabase, ApplicationTy, InferenceResult, Ty, TypeCtor}; | 230 | use crate::{db::HirDatabase, InferenceResult, Ty}; |
231 | 231 | ||
232 | #[derive(Debug, Clone, Copy)] | 232 | #[derive(Debug, Clone, Copy)] |
233 | /// Either a pattern from the source code being analyzed, represented as | 233 | /// Either a pattern from the source code being analyzed, represented as |
@@ -627,14 +627,12 @@ pub(super) fn is_useful( | |||
627 | // - `!` type | 627 | // - `!` type |
628 | // In those cases, no match arm is useful. | 628 | // In those cases, no match arm is useful. |
629 | match cx.infer[cx.match_expr].strip_references() { | 629 | match cx.infer[cx.match_expr].strip_references() { |
630 | Ty::Apply(ApplicationTy { ctor: TypeCtor::Adt(AdtId::EnumId(enum_id)), .. }) => { | 630 | Ty::Adt(AdtId::EnumId(enum_id), ..) => { |
631 | if cx.db.enum_data(*enum_id).variants.is_empty() { | 631 | if cx.db.enum_data(*enum_id).variants.is_empty() { |
632 | return Ok(Usefulness::NotUseful); | 632 | return Ok(Usefulness::NotUseful); |
633 | } | 633 | } |
634 | } | 634 | } |
635 | Ty::Apply(ApplicationTy { ctor: TypeCtor::Never, .. }) => { | 635 | Ty::Never => return Ok(Usefulness::NotUseful), |
636 | return Ok(Usefulness::NotUseful); | ||
637 | } | ||
638 | _ => (), | 636 | _ => (), |
639 | } | 637 | } |
640 | 638 | ||
diff --git a/crates/hir_ty/src/diagnostics/unsafe_check.rs b/crates/hir_ty/src/diagnostics/unsafe_check.rs index 9c506112d..e77a20fea 100644 --- a/crates/hir_ty/src/diagnostics/unsafe_check.rs +++ b/crates/hir_ty/src/diagnostics/unsafe_check.rs | |||
@@ -11,9 +11,7 @@ use hir_def::{ | |||
11 | }; | 11 | }; |
12 | use hir_expand::diagnostics::DiagnosticSink; | 12 | use hir_expand::diagnostics::DiagnosticSink; |
13 | 13 | ||
14 | use crate::{ | 14 | use crate::{db::HirDatabase, diagnostics::MissingUnsafe, InferenceResult, Ty}; |
15 | db::HirDatabase, diagnostics::MissingUnsafe, ApplicationTy, InferenceResult, Ty, TypeCtor, | ||
16 | }; | ||
17 | 15 | ||
18 | pub(super) struct UnsafeValidator<'a, 'b: 'a> { | 16 | pub(super) struct UnsafeValidator<'a, 'b: 'a> { |
19 | owner: DefWithBodyId, | 17 | owner: DefWithBodyId, |
@@ -112,7 +110,7 @@ fn walk_unsafe( | |||
112 | } | 110 | } |
113 | } | 111 | } |
114 | Expr::UnaryOp { expr, op: UnaryOp::Deref } => { | 112 | Expr::UnaryOp { expr, op: UnaryOp::Deref } => { |
115 | if let Ty::Apply(ApplicationTy { ctor: TypeCtor::RawPtr(..), .. }) = &infer[*expr] { | 113 | if let Ty::Raw(..) = &infer[*expr] { |
116 | unsafe_exprs.push(UnsafeExpr { expr: current, inside_unsafe_block }); | 114 | unsafe_exprs.push(UnsafeExpr { expr: current, inside_unsafe_block }); |
117 | } | 115 | } |
118 | } | 116 | } |
diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index 271fcbfaf..d4a8b48e6 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs | |||
@@ -3,10 +3,12 @@ | |||
3 | use std::{borrow::Cow, fmt}; | 3 | use std::{borrow::Cow, fmt}; |
4 | 4 | ||
5 | use crate::{ | 5 | use crate::{ |
6 | db::HirDatabase, utils::generics, ApplicationTy, CallableDefId, FnSig, GenericPredicate, | 6 | db::HirDatabase, primitive, utils::generics, AliasTy, CallableDefId, CallableSig, |
7 | Lifetime, Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, | 7 | GenericPredicate, Lifetime, Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Scalar, Substs, |
8 | TraitRef, Ty, | ||
8 | }; | 9 | }; |
9 | use arrayvec::ArrayVec; | 10 | use arrayvec::ArrayVec; |
11 | use chalk_ir::Mutability; | ||
10 | use hir_def::{ | 12 | use hir_def::{ |
11 | db::DefDatabase, find_path, generics::TypeParamProvenance, item_scope::ItemInNs, AdtId, | 13 | db::DefDatabase, find_path, generics::TypeParamProvenance, item_scope::ItemInNs, AdtId, |
12 | AssocContainerId, HasModule, Lookup, ModuleId, TraitId, | 14 | AssocContainerId, HasModule, Lookup, ModuleId, TraitId, |
@@ -234,39 +236,79 @@ impl HirDisplay for &Ty { | |||
234 | } | 236 | } |
235 | } | 237 | } |
236 | 238 | ||
237 | impl HirDisplay for ApplicationTy { | 239 | impl HirDisplay for ProjectionTy { |
238 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | 240 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { |
239 | if f.should_truncate() { | 241 | if f.should_truncate() { |
240 | return write!(f, "{}", TYPE_HINT_TRUNCATION); | 242 | return write!(f, "{}", TYPE_HINT_TRUNCATION); |
241 | } | 243 | } |
242 | 244 | ||
243 | match self.ctor { | 245 | let trait_ = f.db.trait_data(self.trait_(f.db)); |
244 | TypeCtor::Bool => write!(f, "bool")?, | 246 | let first_parameter = self.parameters[0].into_displayable( |
245 | TypeCtor::Char => write!(f, "char")?, | 247 | f.db, |
246 | TypeCtor::Int(t) => write!(f, "{}", t)?, | 248 | f.max_size, |
247 | TypeCtor::Float(t) => write!(f, "{}", t)?, | 249 | f.omit_verbose_types, |
248 | TypeCtor::Str => write!(f, "str")?, | 250 | f.display_target, |
249 | TypeCtor::Slice => { | 251 | ); |
250 | let t = self.parameters.as_single(); | 252 | write!(f, "<{} as {}", first_parameter, trait_.name)?; |
253 | if self.parameters.len() > 1 { | ||
254 | write!(f, "<")?; | ||
255 | f.write_joined(&self.parameters[1..], ", ")?; | ||
256 | write!(f, ">")?; | ||
257 | } | ||
258 | write!(f, ">::{}", f.db.type_alias_data(self.associated_ty).name)?; | ||
259 | Ok(()) | ||
260 | } | ||
261 | } | ||
262 | |||
263 | impl HirDisplay for Ty { | ||
264 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | ||
265 | if f.should_truncate() { | ||
266 | return write!(f, "{}", TYPE_HINT_TRUNCATION); | ||
267 | } | ||
268 | |||
269 | match self { | ||
270 | Ty::Never => write!(f, "!")?, | ||
271 | Ty::Str => write!(f, "str")?, | ||
272 | Ty::Scalar(Scalar::Bool) => write!(f, "bool")?, | ||
273 | Ty::Scalar(Scalar::Char) => write!(f, "char")?, | ||
274 | &Ty::Scalar(Scalar::Float(t)) => write!(f, "{}", primitive::float_ty_to_string(t))?, | ||
275 | &Ty::Scalar(Scalar::Int(t)) => write!(f, "{}", primitive::int_ty_to_string(t))?, | ||
276 | &Ty::Scalar(Scalar::Uint(t)) => write!(f, "{}", primitive::uint_ty_to_string(t))?, | ||
277 | Ty::Slice(parameters) => { | ||
278 | let t = parameters.as_single(); | ||
251 | write!(f, "[")?; | 279 | write!(f, "[")?; |
252 | t.hir_fmt(f)?; | 280 | t.hir_fmt(f)?; |
253 | write!(f, "]")?; | 281 | write!(f, "]")?; |
254 | } | 282 | } |
255 | TypeCtor::Array => { | 283 | Ty::Array(parameters) => { |
256 | let t = self.parameters.as_single(); | 284 | let t = parameters.as_single(); |
257 | write!(f, "[")?; | 285 | write!(f, "[")?; |
258 | t.hir_fmt(f)?; | 286 | t.hir_fmt(f)?; |
259 | write!(f, "; _]")?; | 287 | write!(f, "; _]")?; |
260 | } | 288 | } |
261 | TypeCtor::RawPtr(m) | TypeCtor::Ref(m) => { | 289 | Ty::Raw(m, parameters) | Ty::Ref(m, parameters) => { |
262 | let t = self.parameters.as_single(); | 290 | let t = parameters.as_single(); |
263 | let ty_display = | 291 | let ty_display = |
264 | t.into_displayable(f.db, f.max_size, f.omit_verbose_types, f.display_target); | 292 | t.into_displayable(f.db, f.max_size, f.omit_verbose_types, f.display_target); |
265 | 293 | ||
266 | if matches!(self.ctor, TypeCtor::RawPtr(_)) { | 294 | if matches!(self, Ty::Raw(..)) { |
267 | write!(f, "*{}", m.as_keyword_for_ptr())?; | 295 | write!( |
296 | f, | ||
297 | "*{}", | ||
298 | match m { | ||
299 | Mutability::Not => "const ", | ||
300 | Mutability::Mut => "mut ", | ||
301 | } | ||
302 | )?; | ||
268 | } else { | 303 | } else { |
269 | write!(f, "&{}", m.as_keyword_for_ref())?; | 304 | write!( |
305 | f, | ||
306 | "&{}", | ||
307 | match m { | ||
308 | Mutability::Not => "", | ||
309 | Mutability::Mut => "mut ", | ||
310 | } | ||
311 | )?; | ||
270 | } | 312 | } |
271 | 313 | ||
272 | let datas; | 314 | let datas; |
@@ -274,10 +316,10 @@ impl HirDisplay for ApplicationTy { | |||
274 | Ty::Dyn(predicates) if predicates.len() > 1 => { | 316 | Ty::Dyn(predicates) if predicates.len() > 1 => { |
275 | Cow::Borrowed(predicates.as_ref()) | 317 | Cow::Borrowed(predicates.as_ref()) |
276 | } | 318 | } |
277 | &Ty::Opaque(OpaqueTy { | 319 | &Ty::Alias(AliasTy::Opaque(OpaqueTy { |
278 | opaque_ty_id: OpaqueTyId::ReturnTypeImplTrait(func, idx), | 320 | opaque_ty_id: OpaqueTyId::ReturnTypeImplTrait(func, idx), |
279 | ref parameters, | 321 | ref parameters, |
280 | }) => { | 322 | })) => { |
281 | datas = | 323 | datas = |
282 | f.db.return_type_impl_traits(func).expect("impl trait id without data"); | 324 | f.db.return_type_impl_traits(func).expect("impl trait id without data"); |
283 | let data = (*datas) | 325 | let data = (*datas) |
@@ -304,25 +346,24 @@ impl HirDisplay for ApplicationTy { | |||
304 | write!(f, "{}", ty_display)?; | 346 | write!(f, "{}", ty_display)?; |
305 | } | 347 | } |
306 | } | 348 | } |
307 | TypeCtor::Never => write!(f, "!")?, | 349 | Ty::Tuple(_, substs) => { |
308 | TypeCtor::Tuple { .. } => { | 350 | if substs.len() == 1 { |
309 | let ts = &self.parameters; | ||
310 | if ts.len() == 1 { | ||
311 | write!(f, "(")?; | 351 | write!(f, "(")?; |
312 | ts[0].hir_fmt(f)?; | 352 | substs[0].hir_fmt(f)?; |
313 | write!(f, ",)")?; | 353 | write!(f, ",)")?; |
314 | } else { | 354 | } else { |
315 | write!(f, "(")?; | 355 | write!(f, "(")?; |
316 | f.write_joined(&*ts.0, ", ")?; | 356 | f.write_joined(&*substs.0, ", ")?; |
317 | write!(f, ")")?; | 357 | write!(f, ")")?; |
318 | } | 358 | } |
319 | } | 359 | } |
320 | TypeCtor::FnPtr { is_varargs, .. } => { | 360 | Ty::Function(fn_ptr) => { |
321 | let sig = FnSig::from_fn_ptr_substs(&self.parameters, is_varargs); | 361 | let sig = CallableSig::from_fn_ptr(fn_ptr); |
322 | sig.hir_fmt(f)?; | 362 | sig.hir_fmt(f)?; |
323 | } | 363 | } |
324 | TypeCtor::FnDef(def) => { | 364 | Ty::FnDef(def, parameters) => { |
325 | let sig = f.db.callable_item_signature(def).subst(&self.parameters); | 365 | let def = *def; |
366 | let sig = f.db.callable_item_signature(def).subst(parameters); | ||
326 | match def { | 367 | match def { |
327 | CallableDefId::FunctionId(ff) => { | 368 | CallableDefId::FunctionId(ff) => { |
328 | write!(f, "fn {}", f.db.function_data(ff).name)? | 369 | write!(f, "fn {}", f.db.function_data(ff).name)? |
@@ -332,7 +373,7 @@ impl HirDisplay for ApplicationTy { | |||
332 | write!(f, "{}", f.db.enum_data(e.parent).variants[e.local_id].name)? | 373 | write!(f, "{}", f.db.enum_data(e.parent).variants[e.local_id].name)? |
333 | } | 374 | } |
334 | }; | 375 | }; |
335 | if self.parameters.len() > 0 { | 376 | if parameters.len() > 0 { |
336 | let generics = generics(f.db.upcast(), def.into()); | 377 | let generics = generics(f.db.upcast(), def.into()); |
337 | let (parent_params, self_param, type_params, _impl_trait_params) = | 378 | let (parent_params, self_param, type_params, _impl_trait_params) = |
338 | generics.provenance_split(); | 379 | generics.provenance_split(); |
@@ -340,7 +381,7 @@ impl HirDisplay for ApplicationTy { | |||
340 | // We print all params except implicit impl Trait params. Still a bit weird; should we leave out parent and self? | 381 | // We print all params except implicit impl Trait params. Still a bit weird; should we leave out parent and self? |
341 | if total_len > 0 { | 382 | if total_len > 0 { |
342 | write!(f, "<")?; | 383 | write!(f, "<")?; |
343 | f.write_joined(&self.parameters.0[..total_len], ", ")?; | 384 | f.write_joined(¶meters.0[..total_len], ", ")?; |
344 | write!(f, ">")?; | 385 | write!(f, ">")?; |
345 | } | 386 | } |
346 | } | 387 | } |
@@ -359,10 +400,10 @@ impl HirDisplay for ApplicationTy { | |||
359 | write!(f, " -> {}", ret_display)?; | 400 | write!(f, " -> {}", ret_display)?; |
360 | } | 401 | } |
361 | } | 402 | } |
362 | TypeCtor::Adt(def_id) => { | 403 | Ty::Adt(def_id, parameters) => { |
363 | match f.display_target { | 404 | match f.display_target { |
364 | DisplayTarget::Diagnostics | DisplayTarget::Test => { | 405 | DisplayTarget::Diagnostics | DisplayTarget::Test => { |
365 | let name = match def_id { | 406 | let name = match *def_id { |
366 | AdtId::StructId(it) => f.db.struct_data(it).name.clone(), | 407 | AdtId::StructId(it) => f.db.struct_data(it).name.clone(), |
367 | AdtId::UnionId(it) => f.db.union_data(it).name.clone(), | 408 | AdtId::UnionId(it) => f.db.union_data(it).name.clone(), |
368 | AdtId::EnumId(it) => f.db.enum_data(it).name.clone(), | 409 | AdtId::EnumId(it) => f.db.enum_data(it).name.clone(), |
@@ -372,7 +413,7 @@ impl HirDisplay for ApplicationTy { | |||
372 | DisplayTarget::SourceCode { module_id } => { | 413 | DisplayTarget::SourceCode { module_id } => { |
373 | if let Some(path) = find_path::find_path( | 414 | if let Some(path) = find_path::find_path( |
374 | f.db.upcast(), | 415 | f.db.upcast(), |
375 | ItemInNs::Types(def_id.into()), | 416 | ItemInNs::Types((*def_id).into()), |
376 | module_id, | 417 | module_id, |
377 | ) { | 418 | ) { |
378 | write!(f, "{}", path)?; | 419 | write!(f, "{}", path)?; |
@@ -384,19 +425,18 @@ impl HirDisplay for ApplicationTy { | |||
384 | } | 425 | } |
385 | } | 426 | } |
386 | 427 | ||
387 | if self.parameters.len() > 0 { | 428 | if parameters.len() > 0 { |
388 | let parameters_to_write = | 429 | let parameters_to_write = |
389 | if f.display_target.is_source_code() || f.omit_verbose_types() { | 430 | if f.display_target.is_source_code() || f.omit_verbose_types() { |
390 | match self | 431 | match self |
391 | .ctor | ||
392 | .as_generic_def() | 432 | .as_generic_def() |
393 | .map(|generic_def_id| f.db.generic_defaults(generic_def_id)) | 433 | .map(|generic_def_id| f.db.generic_defaults(generic_def_id)) |
394 | .filter(|defaults| !defaults.is_empty()) | 434 | .filter(|defaults| !defaults.is_empty()) |
395 | { | 435 | { |
396 | None => self.parameters.0.as_ref(), | 436 | None => parameters.0.as_ref(), |
397 | Some(default_parameters) => { | 437 | Some(default_parameters) => { |
398 | let mut default_from = 0; | 438 | let mut default_from = 0; |
399 | for (i, parameter) in self.parameters.iter().enumerate() { | 439 | for (i, parameter) in parameters.iter().enumerate() { |
400 | match (parameter, default_parameters.get(i)) { | 440 | match (parameter, default_parameters.get(i)) { |
401 | (&Ty::Unknown, _) | (_, None) => { | 441 | (&Ty::Unknown, _) | (_, None) => { |
402 | default_from = i + 1; | 442 | default_from = i + 1; |
@@ -404,18 +444,18 @@ impl HirDisplay for ApplicationTy { | |||
404 | (_, Some(default_parameter)) => { | 444 | (_, Some(default_parameter)) => { |
405 | let actual_default = default_parameter | 445 | let actual_default = default_parameter |
406 | .clone() | 446 | .clone() |
407 | .subst(&self.parameters.prefix(i)); | 447 | .subst(¶meters.prefix(i)); |
408 | if parameter != &actual_default { | 448 | if parameter != &actual_default { |
409 | default_from = i + 1; | 449 | default_from = i + 1; |
410 | } | 450 | } |
411 | } | 451 | } |
412 | } | 452 | } |
413 | } | 453 | } |
414 | &self.parameters.0[0..default_from] | 454 | ¶meters.0[0..default_from] |
415 | } | 455 | } |
416 | } | 456 | } |
417 | } else { | 457 | } else { |
418 | self.parameters.0.as_ref() | 458 | parameters.0.as_ref() |
419 | }; | 459 | }; |
420 | if !parameters_to_write.is_empty() { | 460 | if !parameters_to_write.is_empty() { |
421 | write!(f, "<")?; | 461 | write!(f, "<")?; |
@@ -424,61 +464,54 @@ impl HirDisplay for ApplicationTy { | |||
424 | } | 464 | } |
425 | } | 465 | } |
426 | } | 466 | } |
427 | TypeCtor::AssociatedType(type_alias) => { | 467 | Ty::AssociatedType(type_alias, parameters) => { |
428 | let trait_ = match type_alias.lookup(f.db.upcast()).container { | 468 | let trait_ = match type_alias.lookup(f.db.upcast()).container { |
429 | AssocContainerId::TraitId(it) => it, | 469 | AssocContainerId::TraitId(it) => it, |
430 | _ => panic!("not an associated type"), | 470 | _ => panic!("not an associated type"), |
431 | }; | 471 | }; |
432 | let trait_ = f.db.trait_data(trait_); | 472 | let trait_ = f.db.trait_data(trait_); |
433 | let type_alias_data = f.db.type_alias_data(type_alias); | 473 | let type_alias_data = f.db.type_alias_data(*type_alias); |
434 | 474 | ||
435 | // Use placeholder associated types when the target is test (https://rust-lang.github.io/chalk/book/clauses/type_equality.html#placeholder-associated-types) | 475 | // Use placeholder associated types when the target is test (https://rust-lang.github.io/chalk/book/clauses/type_equality.html#placeholder-associated-types) |
436 | if f.display_target.is_test() { | 476 | if f.display_target.is_test() { |
437 | write!(f, "{}::{}", trait_.name, type_alias_data.name)?; | 477 | write!(f, "{}::{}", trait_.name, type_alias_data.name)?; |
438 | if self.parameters.len() > 0 { | 478 | if parameters.len() > 0 { |
439 | write!(f, "<")?; | 479 | write!(f, "<")?; |
440 | f.write_joined(&*self.parameters.0, ", ")?; | 480 | f.write_joined(&*parameters.0, ", ")?; |
441 | write!(f, ">")?; | 481 | write!(f, ">")?; |
442 | } | 482 | } |
443 | } else { | 483 | } else { |
444 | let projection_ty = ProjectionTy { | 484 | let projection_ty = |
445 | associated_ty: type_alias, | 485 | ProjectionTy { associated_ty: *type_alias, parameters: parameters.clone() }; |
446 | parameters: self.parameters.clone(), | ||
447 | }; | ||
448 | 486 | ||
449 | projection_ty.hir_fmt(f)?; | 487 | projection_ty.hir_fmt(f)?; |
450 | } | 488 | } |
451 | } | 489 | } |
452 | TypeCtor::ForeignType(type_alias) => { | 490 | Ty::ForeignType(type_alias) => { |
453 | let type_alias = f.db.type_alias_data(type_alias); | 491 | let type_alias = f.db.type_alias_data(*type_alias); |
454 | write!(f, "{}", type_alias.name)?; | 492 | write!(f, "{}", type_alias.name)?; |
455 | if self.parameters.len() > 0 { | ||
456 | write!(f, "<")?; | ||
457 | f.write_joined(&*self.parameters.0, ", ")?; | ||
458 | write!(f, ">")?; | ||
459 | } | ||
460 | } | 493 | } |
461 | TypeCtor::OpaqueType(opaque_ty_id) => { | 494 | Ty::OpaqueType(opaque_ty_id, parameters) => { |
462 | match opaque_ty_id { | 495 | match opaque_ty_id { |
463 | OpaqueTyId::ReturnTypeImplTrait(func, idx) => { | 496 | &OpaqueTyId::ReturnTypeImplTrait(func, idx) => { |
464 | let datas = | 497 | let datas = |
465 | f.db.return_type_impl_traits(func).expect("impl trait id without data"); | 498 | f.db.return_type_impl_traits(func).expect("impl trait id without data"); |
466 | let data = (*datas) | 499 | let data = (*datas) |
467 | .as_ref() | 500 | .as_ref() |
468 | .map(|rpit| rpit.impl_traits[idx as usize].bounds.clone()); | 501 | .map(|rpit| rpit.impl_traits[idx as usize].bounds.clone()); |
469 | let bounds = data.subst(&self.parameters); | 502 | let bounds = data.subst(¶meters); |
470 | write_bounds_like_dyn_trait_with_prefix("impl", &bounds.value, f)?; | 503 | write_bounds_like_dyn_trait_with_prefix("impl", &bounds.value, f)?; |
471 | // FIXME: it would maybe be good to distinguish this from the alias type (when debug printing), and to show the substitution | 504 | // FIXME: it would maybe be good to distinguish this from the alias type (when debug printing), and to show the substitution |
472 | } | 505 | } |
473 | OpaqueTyId::AsyncBlockTypeImplTrait(..) => { | 506 | OpaqueTyId::AsyncBlockTypeImplTrait(..) => { |
474 | write!(f, "impl Future<Output = ")?; | 507 | write!(f, "impl Future<Output = ")?; |
475 | self.parameters[0].hir_fmt(f)?; | 508 | parameters[0].hir_fmt(f)?; |
476 | write!(f, ">")?; | 509 | write!(f, ">")?; |
477 | } | 510 | } |
478 | } | 511 | } |
479 | } | 512 | } |
480 | TypeCtor::Closure { .. } => { | 513 | Ty::Closure(.., substs) => { |
481 | let sig = self.parameters[0].callable_sig(f.db); | 514 | let sig = substs[0].callable_sig(f.db); |
482 | if let Some(sig) = sig { | 515 | if let Some(sig) = sig { |
483 | if sig.params().is_empty() { | 516 | if sig.params().is_empty() { |
484 | write!(f, "||")?; | 517 | write!(f, "||")?; |
@@ -501,44 +534,6 @@ impl HirDisplay for ApplicationTy { | |||
501 | write!(f, "{{closure}}")?; | 534 | write!(f, "{{closure}}")?; |
502 | } | 535 | } |
503 | } | 536 | } |
504 | } | ||
505 | Ok(()) | ||
506 | } | ||
507 | } | ||
508 | |||
509 | impl HirDisplay for ProjectionTy { | ||
510 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | ||
511 | if f.should_truncate() { | ||
512 | return write!(f, "{}", TYPE_HINT_TRUNCATION); | ||
513 | } | ||
514 | |||
515 | let trait_ = f.db.trait_data(self.trait_(f.db)); | ||
516 | let first_parameter = self.parameters[0].into_displayable( | ||
517 | f.db, | ||
518 | f.max_size, | ||
519 | f.omit_verbose_types, | ||
520 | f.display_target, | ||
521 | ); | ||
522 | write!(f, "<{} as {}", first_parameter, trait_.name)?; | ||
523 | if self.parameters.len() > 1 { | ||
524 | write!(f, "<")?; | ||
525 | f.write_joined(&self.parameters[1..], ", ")?; | ||
526 | write!(f, ">")?; | ||
527 | } | ||
528 | write!(f, ">::{}", f.db.type_alias_data(self.associated_ty).name)?; | ||
529 | Ok(()) | ||
530 | } | ||
531 | } | ||
532 | |||
533 | impl HirDisplay for Ty { | ||
534 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | ||
535 | if f.should_truncate() { | ||
536 | return write!(f, "{}", TYPE_HINT_TRUNCATION); | ||
537 | } | ||
538 | |||
539 | match self { | ||
540 | Ty::Apply(a_ty) => a_ty.hir_fmt(f)?, | ||
541 | Ty::Projection(p_ty) => p_ty.hir_fmt(f)?, | ||
542 | Ty::Placeholder(id) => { | 537 | Ty::Placeholder(id) => { |
543 | let generics = generics(f.db.upcast(), id.parent); | 538 | let generics = generics(f.db.upcast(), id.parent); |
544 | let param_data = &generics.params.types[id.local_id]; | 539 | let param_data = &generics.params.types[id.local_id]; |
@@ -557,11 +552,12 @@ impl HirDisplay for Ty { | |||
557 | } | 552 | } |
558 | } | 553 | } |
559 | } | 554 | } |
560 | Ty::Bound(idx) => write!(f, "?{}.{}", idx.debruijn.depth(), idx.index)?, | 555 | Ty::BoundVar(idx) => write!(f, "?{}.{}", idx.debruijn.depth(), idx.index)?, |
561 | Ty::Dyn(predicates) => { | 556 | Ty::Dyn(predicates) => { |
562 | write_bounds_like_dyn_trait_with_prefix("dyn", predicates, f)?; | 557 | write_bounds_like_dyn_trait_with_prefix("dyn", predicates, f)?; |
563 | } | 558 | } |
564 | Ty::Opaque(opaque_ty) => { | 559 | Ty::Alias(AliasTy::Projection(p_ty)) => p_ty.hir_fmt(f)?, |
560 | Ty::Alias(AliasTy::Opaque(opaque_ty)) => { | ||
565 | match opaque_ty.opaque_ty_id { | 561 | match opaque_ty.opaque_ty_id { |
566 | OpaqueTyId::ReturnTypeImplTrait(func, idx) => { | 562 | OpaqueTyId::ReturnTypeImplTrait(func, idx) => { |
567 | let datas = | 563 | let datas = |
@@ -585,13 +581,13 @@ impl HirDisplay for Ty { | |||
585 | } | 581 | } |
586 | write!(f, "{{unknown}}")?; | 582 | write!(f, "{{unknown}}")?; |
587 | } | 583 | } |
588 | Ty::Infer(..) => write!(f, "_")?, | 584 | Ty::InferenceVar(..) => write!(f, "_")?, |
589 | } | 585 | } |
590 | Ok(()) | 586 | Ok(()) |
591 | } | 587 | } |
592 | } | 588 | } |
593 | 589 | ||
594 | impl HirDisplay for FnSig { | 590 | impl HirDisplay for CallableSig { |
595 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | 591 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { |
596 | write!(f, "fn(")?; | 592 | write!(f, "fn(")?; |
597 | f.write_joined(self.params(), ", ")?; | 593 | f.write_joined(self.params(), ", ")?; |
diff --git a/crates/hir_ty/src/infer.rs b/crates/hir_ty/src/infer.rs index 4b683c5a7..4d771a91e 100644 --- a/crates/hir_ty/src/infer.rs +++ b/crates/hir_ty/src/infer.rs | |||
@@ -18,6 +18,7 @@ use std::mem; | |||
18 | use std::ops::Index; | 18 | use std::ops::Index; |
19 | use std::sync::Arc; | 19 | use std::sync::Arc; |
20 | 20 | ||
21 | use chalk_ir::Mutability; | ||
21 | use hir_def::{ | 22 | use hir_def::{ |
22 | body::Body, | 23 | body::Body, |
23 | data::{ConstData, FunctionData, StaticData}, | 24 | data::{ConstData, FunctionData, StaticData}, |
@@ -25,7 +26,7 @@ use hir_def::{ | |||
25 | lang_item::LangItemTarget, | 26 | lang_item::LangItemTarget, |
26 | path::{path, Path}, | 27 | path::{path, Path}, |
27 | resolver::{HasResolver, Resolver, TypeNs}, | 28 | resolver::{HasResolver, Resolver, TypeNs}, |
28 | type_ref::{Mutability, TypeRef}, | 29 | type_ref::TypeRef, |
29 | AdtId, AssocItemId, DefWithBodyId, EnumVariantId, FieldId, FunctionId, Lookup, TraitId, | 30 | AdtId, AssocItemId, DefWithBodyId, EnumVariantId, FieldId, FunctionId, Lookup, TraitId, |
30 | TypeAliasId, VariantId, | 31 | TypeAliasId, VariantId, |
31 | }; | 32 | }; |
@@ -36,25 +37,15 @@ use stdx::impl_from; | |||
36 | use syntax::SmolStr; | 37 | use syntax::SmolStr; |
37 | 38 | ||
38 | use super::{ | 39 | use super::{ |
39 | primitive::{FloatTy, IntTy}, | ||
40 | traits::{Guidance, Obligation, ProjectionPredicate, Solution}, | 40 | traits::{Guidance, Obligation, ProjectionPredicate, Solution}, |
41 | InEnvironment, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypeCtor, TypeWalk, | 41 | InEnvironment, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypeWalk, |
42 | }; | 42 | }; |
43 | use crate::{ | 43 | use crate::{ |
44 | db::HirDatabase, infer::diagnostics::InferenceDiagnostic, lower::ImplTraitLoweringMode, | 44 | db::HirDatabase, infer::diagnostics::InferenceDiagnostic, lower::ImplTraitLoweringMode, AliasTy, |
45 | }; | 45 | }; |
46 | 46 | ||
47 | pub(crate) use unify::unify; | 47 | pub(crate) use unify::unify; |
48 | 48 | ||
49 | macro_rules! ty_app { | ||
50 | ($ctor:pat, $param:pat) => { | ||
51 | crate::Ty::Apply(crate::ApplicationTy { ctor: $ctor, parameters: $param }) | ||
52 | }; | ||
53 | ($ctor:pat) => { | ||
54 | ty_app!($ctor, _) | ||
55 | }; | ||
56 | } | ||
57 | |||
58 | mod unify; | 49 | mod unify; |
59 | mod path; | 50 | mod path; |
60 | mod expr; | 51 | mod expr; |
@@ -97,7 +88,7 @@ impl BindingMode { | |||
97 | fn convert(annotation: BindingAnnotation) -> BindingMode { | 88 | fn convert(annotation: BindingAnnotation) -> BindingMode { |
98 | match annotation { | 89 | match annotation { |
99 | BindingAnnotation::Unannotated | BindingAnnotation::Mutable => BindingMode::Move, | 90 | BindingAnnotation::Unannotated | BindingAnnotation::Mutable => BindingMode::Move, |
100 | BindingAnnotation::Ref => BindingMode::Ref(Mutability::Shared), | 91 | BindingAnnotation::Ref => BindingMode::Ref(Mutability::Not), |
101 | BindingAnnotation::RefMut => BindingMode::Ref(Mutability::Mut), | 92 | BindingAnnotation::RefMut => BindingMode::Ref(Mutability::Mut), |
102 | } | 93 | } |
103 | } | 94 | } |
@@ -405,7 +396,7 @@ impl<'a> InferenceContext<'a> { | |||
405 | fn normalize_associated_types_in(&mut self, ty: Ty) -> Ty { | 396 | fn normalize_associated_types_in(&mut self, ty: Ty) -> Ty { |
406 | let ty = self.resolve_ty_as_possible(ty); | 397 | let ty = self.resolve_ty_as_possible(ty); |
407 | ty.fold(&mut |ty| match ty { | 398 | ty.fold(&mut |ty| match ty { |
408 | Ty::Projection(proj_ty) => self.normalize_projection_ty(proj_ty), | 399 | Ty::Alias(AliasTy::Projection(proj_ty)) => self.normalize_projection_ty(proj_ty), |
409 | _ => ty, | 400 | _ => ty, |
410 | }) | 401 | }) |
411 | } | 402 | } |
@@ -664,30 +655,17 @@ impl<'a> InferenceContext<'a> { | |||
664 | /// two are used for inference of literal values (e.g. `100` could be one of | 655 | /// two are used for inference of literal values (e.g. `100` could be one of |
665 | /// several integer types). | 656 | /// several integer types). |
666 | #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] | 657 | #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] |
667 | pub enum InferTy { | 658 | pub struct InferenceVar { |
668 | TypeVar(unify::TypeVarId), | 659 | index: u32, |
669 | IntVar(unify::TypeVarId), | ||
670 | FloatVar(unify::TypeVarId), | ||
671 | MaybeNeverTypeVar(unify::TypeVarId), | ||
672 | } | 660 | } |
673 | 661 | ||
674 | impl InferTy { | 662 | impl InferenceVar { |
675 | fn to_inner(self) -> unify::TypeVarId { | 663 | fn to_inner(self) -> unify::TypeVarId { |
676 | match self { | 664 | unify::TypeVarId(self.index) |
677 | InferTy::TypeVar(ty) | ||
678 | | InferTy::IntVar(ty) | ||
679 | | InferTy::FloatVar(ty) | ||
680 | | InferTy::MaybeNeverTypeVar(ty) => ty, | ||
681 | } | ||
682 | } | 665 | } |
683 | 666 | ||
684 | fn fallback_value(self) -> Ty { | 667 | fn from_inner(unify::TypeVarId(index): unify::TypeVarId) -> Self { |
685 | match self { | 668 | InferenceVar { index } |
686 | InferTy::TypeVar(..) => Ty::Unknown, | ||
687 | InferTy::IntVar(..) => Ty::simple(TypeCtor::Int(IntTy::i32())), | ||
688 | InferTy::FloatVar(..) => Ty::simple(TypeCtor::Float(FloatTy::f64())), | ||
689 | InferTy::MaybeNeverTypeVar(..) => Ty::simple(TypeCtor::Never), | ||
690 | } | ||
691 | } | 669 | } |
692 | } | 670 | } |
693 | 671 | ||
diff --git a/crates/hir_ty/src/infer/coerce.rs b/crates/hir_ty/src/infer/coerce.rs index 32c7c57cd..cf0a3add4 100644 --- a/crates/hir_ty/src/infer/coerce.rs +++ b/crates/hir_ty/src/infer/coerce.rs | |||
@@ -4,12 +4,13 @@ | |||
4 | //! | 4 | //! |
5 | //! See: https://doc.rust-lang.org/nomicon/coercions.html | 5 | //! See: https://doc.rust-lang.org/nomicon/coercions.html |
6 | 6 | ||
7 | use hir_def::{lang_item::LangItemTarget, type_ref::Mutability}; | 7 | use chalk_ir::{Mutability, TyVariableKind}; |
8 | use hir_def::lang_item::LangItemTarget; | ||
8 | use test_utils::mark; | 9 | use test_utils::mark; |
9 | 10 | ||
10 | use crate::{autoderef, traits::Solution, Obligation, Substs, TraitRef, Ty, TypeCtor}; | 11 | use crate::{autoderef, traits::Solution, Obligation, Substs, TraitRef, Ty}; |
11 | 12 | ||
12 | use super::{unify::TypeVarValue, InEnvironment, InferTy, InferenceContext}; | 13 | use super::{InEnvironment, InferenceContext}; |
13 | 14 | ||
14 | impl<'a> InferenceContext<'a> { | 15 | impl<'a> InferenceContext<'a> { |
15 | /// Unify two types, but may coerce the first one to the second one | 16 | /// Unify two types, but may coerce the first one to the second one |
@@ -33,7 +34,7 @@ impl<'a> InferenceContext<'a> { | |||
33 | } else if self.coerce(ty2, ty1) { | 34 | } else if self.coerce(ty2, ty1) { |
34 | ty1.clone() | 35 | ty1.clone() |
35 | } else { | 36 | } else { |
36 | if let (ty_app!(TypeCtor::FnDef(_)), ty_app!(TypeCtor::FnDef(_))) = (ty1, ty2) { | 37 | if let (Ty::FnDef(..), Ty::FnDef(..)) = (ty1, ty2) { |
37 | mark::hit!(coerce_fn_reification); | 38 | mark::hit!(coerce_fn_reification); |
38 | // Special case: two function types. Try to coerce both to | 39 | // Special case: two function types. Try to coerce both to |
39 | // pointers to have a chance at getting a match. See | 40 | // pointers to have a chance at getting a match. See |
@@ -53,12 +54,11 @@ impl<'a> InferenceContext<'a> { | |||
53 | fn coerce_inner(&mut self, mut from_ty: Ty, to_ty: &Ty) -> bool { | 54 | fn coerce_inner(&mut self, mut from_ty: Ty, to_ty: &Ty) -> bool { |
54 | match (&from_ty, to_ty) { | 55 | match (&from_ty, to_ty) { |
55 | // Never type will make type variable to fallback to Never Type instead of Unknown. | 56 | // Never type will make type variable to fallback to Never Type instead of Unknown. |
56 | (ty_app!(TypeCtor::Never), Ty::Infer(InferTy::TypeVar(tv))) => { | 57 | (Ty::Never, Ty::InferenceVar(tv, TyVariableKind::General)) => { |
57 | let var = self.table.new_maybe_never_type_var(); | 58 | self.table.type_variable_table.set_diverging(*tv, true); |
58 | self.table.var_unification_table.union_value(*tv, TypeVarValue::Known(var)); | ||
59 | return true; | 59 | return true; |
60 | } | 60 | } |
61 | (ty_app!(TypeCtor::Never), _) => return true, | 61 | (Ty::Never, _) => return true, |
62 | 62 | ||
63 | // Trivial cases, this should go after `never` check to | 63 | // Trivial cases, this should go after `never` check to |
64 | // avoid infer result type to be never | 64 | // avoid infer result type to be never |
@@ -71,38 +71,33 @@ impl<'a> InferenceContext<'a> { | |||
71 | 71 | ||
72 | // Pointer weakening and function to pointer | 72 | // Pointer weakening and function to pointer |
73 | match (&mut from_ty, to_ty) { | 73 | match (&mut from_ty, to_ty) { |
74 | // `*mut T`, `&mut T, `&T`` -> `*const T` | 74 | // `*mut T` -> `*const T` |
75 | // `&mut T` -> `&T` | 75 | // `&mut T` -> `&T` |
76 | // `&mut T` -> `*mut T` | 76 | (Ty::Raw(m1, ..), Ty::Raw(m2 @ Mutability::Not, ..)) |
77 | (ty_app!(c1@TypeCtor::RawPtr(_)), ty_app!(c2@TypeCtor::RawPtr(Mutability::Shared))) | 77 | | (Ty::Ref(m1, ..), Ty::Ref(m2 @ Mutability::Not, ..)) => { |
78 | | (ty_app!(c1@TypeCtor::Ref(_)), ty_app!(c2@TypeCtor::RawPtr(Mutability::Shared))) | 78 | *m1 = *m2; |
79 | | (ty_app!(c1@TypeCtor::Ref(_)), ty_app!(c2@TypeCtor::Ref(Mutability::Shared))) | 79 | } |
80 | | (ty_app!(c1@TypeCtor::Ref(Mutability::Mut)), ty_app!(c2@TypeCtor::RawPtr(_))) => { | 80 | // `&T` -> `*const T` |
81 | *c1 = *c2; | 81 | // `&mut T` -> `*mut T`/`*const T` |
82 | (Ty::Ref(.., substs), &Ty::Raw(m2 @ Mutability::Not, ..)) | ||
83 | | (Ty::Ref(Mutability::Mut, substs), &Ty::Raw(m2, ..)) => { | ||
84 | from_ty = Ty::Raw(m2, substs.clone()); | ||
82 | } | 85 | } |
83 | 86 | ||
84 | // Illegal mutablity conversion | 87 | // Illegal mutability conversion |
85 | ( | 88 | (Ty::Raw(Mutability::Not, ..), Ty::Raw(Mutability::Mut, ..)) |
86 | ty_app!(TypeCtor::RawPtr(Mutability::Shared)), | 89 | | (Ty::Ref(Mutability::Not, ..), Ty::Ref(Mutability::Mut, ..)) => return false, |
87 | ty_app!(TypeCtor::RawPtr(Mutability::Mut)), | ||
88 | ) | ||
89 | | ( | ||
90 | ty_app!(TypeCtor::Ref(Mutability::Shared)), | ||
91 | ty_app!(TypeCtor::Ref(Mutability::Mut)), | ||
92 | ) => return false, | ||
93 | 90 | ||
94 | // `{function_type}` -> `fn()` | 91 | // `{function_type}` -> `fn()` |
95 | (ty_app!(TypeCtor::FnDef(_)), ty_app!(TypeCtor::FnPtr { .. })) => { | 92 | (Ty::FnDef(..), Ty::Function { .. }) => match from_ty.callable_sig(self.db) { |
96 | match from_ty.callable_sig(self.db) { | 93 | None => return false, |
97 | None => return false, | 94 | Some(sig) => { |
98 | Some(sig) => { | 95 | from_ty = Ty::fn_ptr(sig); |
99 | from_ty = Ty::fn_ptr(sig); | ||
100 | } | ||
101 | } | 96 | } |
102 | } | 97 | }, |
103 | 98 | ||
104 | (ty_app!(TypeCtor::Closure { .. }, params), ty_app!(TypeCtor::FnPtr { .. })) => { | 99 | (Ty::Closure(.., substs), Ty::Function { .. }) => { |
105 | from_ty = params[0].clone(); | 100 | from_ty = substs[0].clone(); |
106 | } | 101 | } |
107 | 102 | ||
108 | _ => {} | 103 | _ => {} |
@@ -115,9 +110,7 @@ impl<'a> InferenceContext<'a> { | |||
115 | // Auto Deref if cannot coerce | 110 | // Auto Deref if cannot coerce |
116 | match (&from_ty, to_ty) { | 111 | match (&from_ty, to_ty) { |
117 | // FIXME: DerefMut | 112 | // FIXME: DerefMut |
118 | (ty_app!(TypeCtor::Ref(_), st1), ty_app!(TypeCtor::Ref(_), st2)) => { | 113 | (Ty::Ref(_, st1), Ty::Ref(_, st2)) => self.unify_autoderef_behind_ref(&st1[0], &st2[0]), |
119 | self.unify_autoderef_behind_ref(&st1[0], &st2[0]) | ||
120 | } | ||
121 | 114 | ||
122 | // Otherwise, normal unify | 115 | // Otherwise, normal unify |
123 | _ => self.unify(&from_ty, to_ty), | 116 | _ => self.unify(&from_ty, to_ty), |
@@ -178,17 +171,17 @@ impl<'a> InferenceContext<'a> { | |||
178 | }, | 171 | }, |
179 | ) { | 172 | ) { |
180 | let derefed_ty = canonicalized.decanonicalize_ty(derefed_ty.value); | 173 | let derefed_ty = canonicalized.decanonicalize_ty(derefed_ty.value); |
181 | match (&*self.resolve_ty_shallow(&derefed_ty), &*to_ty) { | 174 | let from_ty = self.resolve_ty_shallow(&derefed_ty); |
182 | // Stop when constructor matches. | 175 | // Stop when constructor matches. |
183 | (ty_app!(from_ctor, st1), ty_app!(to_ctor, st2)) if from_ctor == to_ctor => { | 176 | if from_ty.equals_ctor(&to_ty) { |
184 | // It will not recurse to `coerce`. | 177 | // It will not recurse to `coerce`. |
185 | return self.table.unify_substs(st1, st2, 0); | 178 | return match (from_ty.substs(), to_ty.substs()) { |
186 | } | 179 | (Some(st1), Some(st2)) => self.table.unify_substs(st1, st2, 0), |
187 | _ => { | 180 | (None, None) => true, |
188 | if self.table.unify_inner_trivial(&derefed_ty, &to_ty, 0) { | 181 | _ => false, |
189 | return true; | 182 | }; |
190 | } | 183 | } else if self.table.unify_inner_trivial(&derefed_ty, &to_ty, 0) { |
191 | } | 184 | return true; |
192 | } | 185 | } |
193 | } | 186 | } |
194 | 187 | ||
diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index cb59a6937..cf1f1038a 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs | |||
@@ -3,8 +3,8 @@ | |||
3 | use std::iter::{repeat, repeat_with}; | 3 | use std::iter::{repeat, repeat_with}; |
4 | use std::{mem, sync::Arc}; | 4 | use std::{mem, sync::Arc}; |
5 | 5 | ||
6 | use chalk_ir::{Mutability, TyVariableKind}; | ||
6 | use hir_def::{ | 7 | use hir_def::{ |
7 | builtin_type::Signedness, | ||
8 | expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp}, | 8 | expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp}, |
9 | path::{GenericArg, GenericArgs}, | 9 | path::{GenericArg, GenericArgs}, |
10 | resolver::resolver_for_expr, | 10 | resolver::resolver_for_expr, |
@@ -15,11 +15,14 @@ use syntax::ast::RangeOp; | |||
15 | use test_utils::mark; | 15 | use test_utils::mark; |
16 | 16 | ||
17 | use crate::{ | 17 | use crate::{ |
18 | autoderef, method_resolution, op, | 18 | autoderef, |
19 | lower::lower_to_chalk_mutability, | ||
20 | method_resolution, op, | ||
21 | primitive::{self, UintTy}, | ||
19 | traits::{FnTrait, InEnvironment}, | 22 | traits::{FnTrait, InEnvironment}, |
20 | utils::{generics, variant_data, Generics}, | 23 | utils::{generics, variant_data, Generics}, |
21 | ApplicationTy, Binders, CallableDefId, InferTy, IntTy, Mutability, Obligation, OpaqueTyId, | 24 | Binders, CallableDefId, FnPointer, FnSig, Obligation, OpaqueTyId, Rawness, Scalar, Substs, |
22 | Rawness, Substs, TraitRef, Ty, TypeCtor, | 25 | TraitRef, Ty, |
23 | }; | 26 | }; |
24 | 27 | ||
25 | use super::{ | 28 | use super::{ |
@@ -82,10 +85,7 @@ impl<'a> InferenceContext<'a> { | |||
82 | arg_tys.push(arg); | 85 | arg_tys.push(arg); |
83 | } | 86 | } |
84 | let parameters = param_builder.build(); | 87 | let parameters = param_builder.build(); |
85 | let arg_ty = Ty::Apply(ApplicationTy { | 88 | let arg_ty = Ty::Tuple(num_args, parameters); |
86 | ctor: TypeCtor::Tuple { cardinality: num_args as u16 }, | ||
87 | parameters, | ||
88 | }); | ||
89 | let substs = | 89 | let substs = |
90 | Substs::build_for_generics(&generic_params).push(ty.clone()).push(arg_ty).build(); | 90 | Substs::build_for_generics(&generic_params).push(ty.clone()).push(arg_ty).build(); |
91 | 91 | ||
@@ -120,7 +120,7 @@ impl<'a> InferenceContext<'a> { | |||
120 | Expr::Missing => Ty::Unknown, | 120 | Expr::Missing => Ty::Unknown, |
121 | Expr::If { condition, then_branch, else_branch } => { | 121 | Expr::If { condition, then_branch, else_branch } => { |
122 | // if let is desugared to match, so this is always simple if | 122 | // if let is desugared to match, so this is always simple if |
123 | self.infer_expr(*condition, &Expectation::has_type(Ty::simple(TypeCtor::Bool))); | 123 | self.infer_expr(*condition, &Expectation::has_type(Ty::Scalar(Scalar::Bool))); |
124 | 124 | ||
125 | let condition_diverges = mem::replace(&mut self.diverges, Diverges::Maybe); | 125 | let condition_diverges = mem::replace(&mut self.diverges, Diverges::Maybe); |
126 | let mut both_arms_diverge = Diverges::Always; | 126 | let mut both_arms_diverge = Diverges::Always; |
@@ -175,7 +175,7 @@ impl<'a> InferenceContext<'a> { | |||
175 | // existenail type AsyncBlockImplTrait<InnerType>: Future<Output = InnerType> | 175 | // existenail type AsyncBlockImplTrait<InnerType>: Future<Output = InnerType> |
176 | let inner_ty = self.infer_expr(*body, &Expectation::none()); | 176 | let inner_ty = self.infer_expr(*body, &Expectation::none()); |
177 | let opaque_ty_id = OpaqueTyId::AsyncBlockTypeImplTrait(self.owner, *body); | 177 | let opaque_ty_id = OpaqueTyId::AsyncBlockTypeImplTrait(self.owner, *body); |
178 | Ty::apply_one(TypeCtor::OpaqueType(opaque_ty_id), inner_ty) | 178 | Ty::OpaqueType(opaque_ty_id, Substs::single(inner_ty)) |
179 | } | 179 | } |
180 | Expr::Loop { body, label } => { | 180 | Expr::Loop { body, label } => { |
181 | self.breakables.push(BreakableContext { | 181 | self.breakables.push(BreakableContext { |
@@ -193,7 +193,7 @@ impl<'a> InferenceContext<'a> { | |||
193 | if ctxt.may_break { | 193 | if ctxt.may_break { |
194 | ctxt.break_ty | 194 | ctxt.break_ty |
195 | } else { | 195 | } else { |
196 | Ty::simple(TypeCtor::Never) | 196 | Ty::Never |
197 | } | 197 | } |
198 | } | 198 | } |
199 | Expr::While { condition, body, label } => { | 199 | Expr::While { condition, body, label } => { |
@@ -203,7 +203,7 @@ impl<'a> InferenceContext<'a> { | |||
203 | label: label.map(|label| self.body[label].name.clone()), | 203 | label: label.map(|label| self.body[label].name.clone()), |
204 | }); | 204 | }); |
205 | // while let is desugared to a match loop, so this is always simple while | 205 | // while let is desugared to a match loop, so this is always simple while |
206 | self.infer_expr(*condition, &Expectation::has_type(Ty::simple(TypeCtor::Bool))); | 206 | self.infer_expr(*condition, &Expectation::has_type(Ty::Scalar(Scalar::Bool))); |
207 | self.infer_expr(*body, &Expectation::has_type(Ty::unit())); | 207 | self.infer_expr(*body, &Expectation::has_type(Ty::unit())); |
208 | let _ctxt = self.breakables.pop().expect("breakable stack broken"); | 208 | let _ctxt = self.breakables.pop().expect("breakable stack broken"); |
209 | // the body may not run, so it diverging doesn't mean we diverge | 209 | // the body may not run, so it diverging doesn't mean we diverge |
@@ -250,12 +250,12 @@ impl<'a> InferenceContext<'a> { | |||
250 | None => self.table.new_type_var(), | 250 | None => self.table.new_type_var(), |
251 | }; | 251 | }; |
252 | sig_tys.push(ret_ty.clone()); | 252 | sig_tys.push(ret_ty.clone()); |
253 | let sig_ty = Ty::apply( | 253 | let sig_ty = Ty::Function(FnPointer { |
254 | TypeCtor::FnPtr { num_args: sig_tys.len() as u16 - 1, is_varargs: false }, | 254 | num_args: sig_tys.len() - 1, |
255 | Substs(sig_tys.clone().into()), | 255 | sig: FnSig { variadic: false }, |
256 | ); | 256 | substs: Substs(sig_tys.clone().into()), |
257 | let closure_ty = | 257 | }); |
258 | Ty::apply_one(TypeCtor::Closure { def: self.owner, expr: tgt_expr }, sig_ty); | 258 | let closure_ty = Ty::Closure(self.owner, tgt_expr, Substs::single(sig_ty)); |
259 | 259 | ||
260 | // Eagerly try to relate the closure type with the expected | 260 | // Eagerly try to relate the closure type with the expected |
261 | // type, otherwise we often won't have enough information to | 261 | // type, otherwise we often won't have enough information to |
@@ -306,11 +306,8 @@ impl<'a> InferenceContext<'a> { | |||
306 | Expr::Match { expr, arms } => { | 306 | Expr::Match { expr, arms } => { |
307 | let input_ty = self.infer_expr(*expr, &Expectation::none()); | 307 | let input_ty = self.infer_expr(*expr, &Expectation::none()); |
308 | 308 | ||
309 | let mut result_ty = if arms.is_empty() { | 309 | let mut result_ty = |
310 | Ty::simple(TypeCtor::Never) | 310 | if arms.is_empty() { Ty::Never } else { self.table.new_type_var() }; |
311 | } else { | ||
312 | self.table.new_type_var() | ||
313 | }; | ||
314 | 311 | ||
315 | let matchee_diverges = self.diverges; | 312 | let matchee_diverges = self.diverges; |
316 | let mut all_arms_diverge = Diverges::Always; | 313 | let mut all_arms_diverge = Diverges::Always; |
@@ -321,7 +318,7 @@ impl<'a> InferenceContext<'a> { | |||
321 | if let Some(guard_expr) = arm.guard { | 318 | if let Some(guard_expr) = arm.guard { |
322 | self.infer_expr( | 319 | self.infer_expr( |
323 | guard_expr, | 320 | guard_expr, |
324 | &Expectation::has_type(Ty::simple(TypeCtor::Bool)), | 321 | &Expectation::has_type(Ty::Scalar(Scalar::Bool)), |
325 | ); | 322 | ); |
326 | } | 323 | } |
327 | 324 | ||
@@ -339,7 +336,7 @@ impl<'a> InferenceContext<'a> { | |||
339 | let resolver = resolver_for_expr(self.db.upcast(), self.owner, tgt_expr); | 336 | let resolver = resolver_for_expr(self.db.upcast(), self.owner, tgt_expr); |
340 | self.infer_path(&resolver, p, tgt_expr.into()).unwrap_or(Ty::Unknown) | 337 | self.infer_path(&resolver, p, tgt_expr.into()).unwrap_or(Ty::Unknown) |
341 | } | 338 | } |
342 | Expr::Continue { .. } => Ty::simple(TypeCtor::Never), | 339 | Expr::Continue { .. } => Ty::Never, |
343 | Expr::Break { expr, label } => { | 340 | Expr::Break { expr, label } => { |
344 | let val_ty = if let Some(expr) = expr { | 341 | let val_ty = if let Some(expr) = expr { |
345 | self.infer_expr(*expr, &Expectation::none()) | 342 | self.infer_expr(*expr, &Expectation::none()) |
@@ -364,8 +361,7 @@ impl<'a> InferenceContext<'a> { | |||
364 | expr: tgt_expr, | 361 | expr: tgt_expr, |
365 | }); | 362 | }); |
366 | } | 363 | } |
367 | 364 | Ty::Never | |
368 | Ty::simple(TypeCtor::Never) | ||
369 | } | 365 | } |
370 | Expr::Return { expr } => { | 366 | Expr::Return { expr } => { |
371 | if let Some(expr) = expr { | 367 | if let Some(expr) = expr { |
@@ -374,14 +370,14 @@ impl<'a> InferenceContext<'a> { | |||
374 | let unit = Ty::unit(); | 370 | let unit = Ty::unit(); |
375 | self.coerce(&unit, &self.return_ty.clone()); | 371 | self.coerce(&unit, &self.return_ty.clone()); |
376 | } | 372 | } |
377 | Ty::simple(TypeCtor::Never) | 373 | Ty::Never |
378 | } | 374 | } |
379 | Expr::Yield { expr } => { | 375 | Expr::Yield { expr } => { |
380 | // FIXME: track yield type for coercion | 376 | // FIXME: track yield type for coercion |
381 | if let Some(expr) = expr { | 377 | if let Some(expr) = expr { |
382 | self.infer_expr(*expr, &Expectation::none()); | 378 | self.infer_expr(*expr, &Expectation::none()); |
383 | } | 379 | } |
384 | Ty::simple(TypeCtor::Never) | 380 | Ty::Never |
385 | } | 381 | } |
386 | Expr::RecordLit { path, fields, spread } => { | 382 | Expr::RecordLit { path, fields, spread } => { |
387 | let (ty, def_id) = self.resolve_variant(path.as_ref()); | 383 | let (ty, def_id) = self.resolve_variant(path.as_ref()); |
@@ -391,7 +387,7 @@ impl<'a> InferenceContext<'a> { | |||
391 | 387 | ||
392 | self.unify(&ty, &expected.ty); | 388 | self.unify(&ty, &expected.ty); |
393 | 389 | ||
394 | let substs = ty.substs().unwrap_or_else(Substs::empty); | 390 | let substs = ty.substs().cloned().unwrap_or_else(Substs::empty); |
395 | let field_types = def_id.map(|it| self.db.field_types(it)).unwrap_or_default(); | 391 | let field_types = def_id.map(|it| self.db.field_types(it)).unwrap_or_default(); |
396 | let variant_data = def_id.map(|it| variant_data(self.db.upcast(), it)); | 392 | let variant_data = def_id.map(|it| variant_data(self.db.upcast(), it)); |
397 | for (field_idx, field) in fields.iter().enumerate() { | 393 | for (field_idx, field) in fields.iter().enumerate() { |
@@ -430,30 +426,23 @@ impl<'a> InferenceContext<'a> { | |||
430 | }, | 426 | }, |
431 | ) | 427 | ) |
432 | .find_map(|derefed_ty| match canonicalized.decanonicalize_ty(derefed_ty.value) { | 428 | .find_map(|derefed_ty| match canonicalized.decanonicalize_ty(derefed_ty.value) { |
433 | Ty::Apply(a_ty) => match a_ty.ctor { | 429 | Ty::Tuple(_, substs) => { |
434 | TypeCtor::Tuple { .. } => name | 430 | name.as_tuple_index().and_then(|idx| substs.0.get(idx).cloned()) |
435 | .as_tuple_index() | 431 | } |
436 | .and_then(|idx| a_ty.parameters.0.get(idx).cloned()), | 432 | Ty::Adt(AdtId::StructId(s), parameters) => { |
437 | TypeCtor::Adt(AdtId::StructId(s)) => { | 433 | self.db.struct_data(s).variant_data.field(name).map(|local_id| { |
438 | self.db.struct_data(s).variant_data.field(name).map(|local_id| { | 434 | let field = FieldId { parent: s.into(), local_id }; |
439 | let field = FieldId { parent: s.into(), local_id }; | 435 | self.write_field_resolution(tgt_expr, field); |
440 | self.write_field_resolution(tgt_expr, field); | 436 | self.db.field_types(s.into())[field.local_id].clone().subst(¶meters) |
441 | self.db.field_types(s.into())[field.local_id] | 437 | }) |
442 | .clone() | 438 | } |
443 | .subst(&a_ty.parameters) | 439 | Ty::Adt(AdtId::UnionId(u), parameters) => { |
444 | }) | 440 | self.db.union_data(u).variant_data.field(name).map(|local_id| { |
445 | } | 441 | let field = FieldId { parent: u.into(), local_id }; |
446 | TypeCtor::Adt(AdtId::UnionId(u)) => { | 442 | self.write_field_resolution(tgt_expr, field); |
447 | self.db.union_data(u).variant_data.field(name).map(|local_id| { | 443 | self.db.field_types(u.into())[field.local_id].clone().subst(¶meters) |
448 | let field = FieldId { parent: u.into(), local_id }; | 444 | }) |
449 | self.write_field_resolution(tgt_expr, field); | 445 | } |
450 | self.db.field_types(u.into())[field.local_id] | ||
451 | .clone() | ||
452 | .subst(&a_ty.parameters) | ||
453 | }) | ||
454 | } | ||
455 | _ => None, | ||
456 | }, | ||
457 | _ => None, | 446 | _ => None, |
458 | }) | 447 | }) |
459 | .unwrap_or(Ty::Unknown); | 448 | .unwrap_or(Ty::Unknown); |
@@ -475,10 +464,11 @@ impl<'a> InferenceContext<'a> { | |||
475 | cast_ty | 464 | cast_ty |
476 | } | 465 | } |
477 | Expr::Ref { expr, rawness, mutability } => { | 466 | Expr::Ref { expr, rawness, mutability } => { |
467 | let mutability = lower_to_chalk_mutability(*mutability); | ||
478 | let expectation = if let Some((exp_inner, exp_rawness, exp_mutability)) = | 468 | let expectation = if let Some((exp_inner, exp_rawness, exp_mutability)) = |
479 | &expected.ty.as_reference_or_ptr() | 469 | &expected.ty.as_reference_or_ptr() |
480 | { | 470 | { |
481 | if *exp_mutability == Mutability::Mut && *mutability == Mutability::Shared { | 471 | if *exp_mutability == Mutability::Mut && mutability == Mutability::Not { |
482 | // FIXME: throw type error - expected mut reference but found shared ref, | 472 | // FIXME: throw type error - expected mut reference but found shared ref, |
483 | // which cannot be coerced | 473 | // which cannot be coerced |
484 | } | 474 | } |
@@ -491,19 +481,24 @@ impl<'a> InferenceContext<'a> { | |||
491 | Expectation::none() | 481 | Expectation::none() |
492 | }; | 482 | }; |
493 | let inner_ty = self.infer_expr_inner(*expr, &expectation); | 483 | let inner_ty = self.infer_expr_inner(*expr, &expectation); |
494 | let ty = match rawness { | 484 | match rawness { |
495 | Rawness::RawPtr => TypeCtor::RawPtr(*mutability), | 485 | Rawness::RawPtr => Ty::Raw(mutability, Substs::single(inner_ty)), |
496 | Rawness::Ref => TypeCtor::Ref(*mutability), | 486 | Rawness::Ref => Ty::Ref(mutability, Substs::single(inner_ty)), |
497 | }; | 487 | } |
498 | Ty::apply_one(ty, inner_ty) | ||
499 | } | 488 | } |
500 | Expr::Box { expr } => { | 489 | Expr::Box { expr } => { |
501 | let inner_ty = self.infer_expr_inner(*expr, &Expectation::none()); | 490 | let inner_ty = self.infer_expr_inner(*expr, &Expectation::none()); |
502 | if let Some(box_) = self.resolve_boxed_box() { | 491 | if let Some(box_) = self.resolve_boxed_box() { |
503 | let mut sb = Substs::build_for_type_ctor(self.db, TypeCtor::Adt(box_)); | 492 | let mut sb = Substs::builder(generics(self.db.upcast(), box_.into()).len()); |
504 | sb = sb.push(inner_ty); | 493 | sb = sb.push(inner_ty); |
494 | match self.db.generic_defaults(box_.into()).as_ref() { | ||
495 | [_, alloc_ty, ..] if !alloc_ty.value.is_unknown() => { | ||
496 | sb = sb.push(alloc_ty.value.clone()); | ||
497 | } | ||
498 | _ => (), | ||
499 | } | ||
505 | sb = sb.fill(repeat_with(|| self.table.new_type_var())); | 500 | sb = sb.fill(repeat_with(|| self.table.new_type_var())); |
506 | Ty::apply(TypeCtor::Adt(box_), sb.build()) | 501 | Ty::Adt(box_, sb.build()) |
507 | } else { | 502 | } else { |
508 | Ty::Unknown | 503 | Ty::Unknown |
509 | } | 504 | } |
@@ -533,13 +528,11 @@ impl<'a> InferenceContext<'a> { | |||
533 | UnaryOp::Neg => { | 528 | UnaryOp::Neg => { |
534 | match &inner_ty { | 529 | match &inner_ty { |
535 | // Fast path for builtins | 530 | // Fast path for builtins |
536 | Ty::Apply(ApplicationTy { | 531 | Ty::Scalar(Scalar::Int(_)) |
537 | ctor: TypeCtor::Int(IntTy { signedness: Signedness::Signed, .. }), | 532 | | Ty::Scalar(Scalar::Uint(_)) |
538 | .. | 533 | | Ty::Scalar(Scalar::Float(_)) |
539 | }) | 534 | | Ty::InferenceVar(_, TyVariableKind::Integer) |
540 | | Ty::Apply(ApplicationTy { ctor: TypeCtor::Float(_), .. }) | 535 | | Ty::InferenceVar(_, TyVariableKind::Float) => inner_ty, |
541 | | Ty::Infer(InferTy::IntVar(..)) | ||
542 | | Ty::Infer(InferTy::FloatVar(..)) => inner_ty, | ||
543 | // Otherwise we resolve via the std::ops::Neg trait | 536 | // Otherwise we resolve via the std::ops::Neg trait |
544 | _ => self | 537 | _ => self |
545 | .resolve_associated_type(inner_ty, self.resolve_ops_neg_output()), | 538 | .resolve_associated_type(inner_ty, self.resolve_ops_neg_output()), |
@@ -548,9 +541,10 @@ impl<'a> InferenceContext<'a> { | |||
548 | UnaryOp::Not => { | 541 | UnaryOp::Not => { |
549 | match &inner_ty { | 542 | match &inner_ty { |
550 | // Fast path for builtins | 543 | // Fast path for builtins |
551 | Ty::Apply(ApplicationTy { ctor: TypeCtor::Bool, .. }) | 544 | Ty::Scalar(Scalar::Bool) |
552 | | Ty::Apply(ApplicationTy { ctor: TypeCtor::Int(_), .. }) | 545 | | Ty::Scalar(Scalar::Int(_)) |
553 | | Ty::Infer(InferTy::IntVar(..)) => inner_ty, | 546 | | Ty::Scalar(Scalar::Uint(_)) |
547 | | Ty::InferenceVar(_, TyVariableKind::Integer) => inner_ty, | ||
554 | // Otherwise we resolve via the std::ops::Not trait | 548 | // Otherwise we resolve via the std::ops::Not trait |
555 | _ => self | 549 | _ => self |
556 | .resolve_associated_type(inner_ty, self.resolve_ops_not_output()), | 550 | .resolve_associated_type(inner_ty, self.resolve_ops_not_output()), |
@@ -561,7 +555,7 @@ impl<'a> InferenceContext<'a> { | |||
561 | Expr::BinaryOp { lhs, rhs, op } => match op { | 555 | Expr::BinaryOp { lhs, rhs, op } => match op { |
562 | Some(op) => { | 556 | Some(op) => { |
563 | let lhs_expectation = match op { | 557 | let lhs_expectation = match op { |
564 | BinaryOp::LogicOp(..) => Expectation::has_type(Ty::simple(TypeCtor::Bool)), | 558 | BinaryOp::LogicOp(..) => Expectation::has_type(Ty::Scalar(Scalar::Bool)), |
565 | _ => Expectation::none(), | 559 | _ => Expectation::none(), |
566 | }; | 560 | }; |
567 | let lhs_ty = self.infer_expr(*lhs, &lhs_expectation); | 561 | let lhs_ty = self.infer_expr(*lhs, &lhs_expectation); |
@@ -592,31 +586,31 @@ impl<'a> InferenceContext<'a> { | |||
592 | let rhs_ty = rhs.map(|e| self.infer_expr(e, &rhs_expect)); | 586 | let rhs_ty = rhs.map(|e| self.infer_expr(e, &rhs_expect)); |
593 | match (range_type, lhs_ty, rhs_ty) { | 587 | match (range_type, lhs_ty, rhs_ty) { |
594 | (RangeOp::Exclusive, None, None) => match self.resolve_range_full() { | 588 | (RangeOp::Exclusive, None, None) => match self.resolve_range_full() { |
595 | Some(adt) => Ty::simple(TypeCtor::Adt(adt)), | 589 | Some(adt) => Ty::Adt(adt, Substs::empty()), |
596 | None => Ty::Unknown, | 590 | None => Ty::Unknown, |
597 | }, | 591 | }, |
598 | (RangeOp::Exclusive, None, Some(ty)) => match self.resolve_range_to() { | 592 | (RangeOp::Exclusive, None, Some(ty)) => match self.resolve_range_to() { |
599 | Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty), | 593 | Some(adt) => Ty::Adt(adt, Substs::single(ty)), |
600 | None => Ty::Unknown, | 594 | None => Ty::Unknown, |
601 | }, | 595 | }, |
602 | (RangeOp::Inclusive, None, Some(ty)) => { | 596 | (RangeOp::Inclusive, None, Some(ty)) => { |
603 | match self.resolve_range_to_inclusive() { | 597 | match self.resolve_range_to_inclusive() { |
604 | Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty), | 598 | Some(adt) => Ty::Adt(adt, Substs::single(ty)), |
605 | None => Ty::Unknown, | 599 | None => Ty::Unknown, |
606 | } | 600 | } |
607 | } | 601 | } |
608 | (RangeOp::Exclusive, Some(_), Some(ty)) => match self.resolve_range() { | 602 | (RangeOp::Exclusive, Some(_), Some(ty)) => match self.resolve_range() { |
609 | Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty), | 603 | Some(adt) => Ty::Adt(adt, Substs::single(ty)), |
610 | None => Ty::Unknown, | 604 | None => Ty::Unknown, |
611 | }, | 605 | }, |
612 | (RangeOp::Inclusive, Some(_), Some(ty)) => { | 606 | (RangeOp::Inclusive, Some(_), Some(ty)) => { |
613 | match self.resolve_range_inclusive() { | 607 | match self.resolve_range_inclusive() { |
614 | Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty), | 608 | Some(adt) => Ty::Adt(adt, Substs::single(ty)), |
615 | None => Ty::Unknown, | 609 | None => Ty::Unknown, |
616 | } | 610 | } |
617 | } | 611 | } |
618 | (RangeOp::Exclusive, Some(ty), None) => match self.resolve_range_from() { | 612 | (RangeOp::Exclusive, Some(ty), None) => match self.resolve_range_from() { |
619 | Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty), | 613 | Some(adt) => Ty::Adt(adt, Substs::single(ty)), |
620 | None => Ty::Unknown, | 614 | None => Ty::Unknown, |
621 | }, | 615 | }, |
622 | (RangeOp::Inclusive, _, None) => Ty::Unknown, | 616 | (RangeOp::Inclusive, _, None) => Ty::Unknown, |
@@ -650,7 +644,7 @@ impl<'a> InferenceContext<'a> { | |||
650 | } | 644 | } |
651 | Expr::Tuple { exprs } => { | 645 | Expr::Tuple { exprs } => { |
652 | let mut tys = match &expected.ty { | 646 | let mut tys = match &expected.ty { |
653 | ty_app!(TypeCtor::Tuple { .. }, st) => st | 647 | Ty::Tuple(_, substs) => substs |
654 | .iter() | 648 | .iter() |
655 | .cloned() | 649 | .cloned() |
656 | .chain(repeat_with(|| self.table.new_type_var())) | 650 | .chain(repeat_with(|| self.table.new_type_var())) |
@@ -663,15 +657,11 @@ impl<'a> InferenceContext<'a> { | |||
663 | self.infer_expr_coerce(*expr, &Expectation::has_type(ty.clone())); | 657 | self.infer_expr_coerce(*expr, &Expectation::has_type(ty.clone())); |
664 | } | 658 | } |
665 | 659 | ||
666 | Ty::apply(TypeCtor::Tuple { cardinality: tys.len() as u16 }, Substs(tys.into())) | 660 | Ty::Tuple(tys.len(), Substs(tys.into())) |
667 | } | 661 | } |
668 | Expr::Array(array) => { | 662 | Expr::Array(array) => { |
669 | let elem_ty = match &expected.ty { | 663 | let elem_ty = match &expected.ty { |
670 | // FIXME: remove when https://github.com/rust-lang/rust/issues/80501 is fixed | 664 | Ty::Array(st) | Ty::Slice(st) => st.as_single().clone(), |
671 | #[allow(unreachable_patterns)] | ||
672 | ty_app!(TypeCtor::Array, st) | ty_app!(TypeCtor::Slice, st) => { | ||
673 | st.as_single().clone() | ||
674 | } | ||
675 | _ => self.table.new_type_var(), | 665 | _ => self.table.new_type_var(), |
676 | }; | 666 | }; |
677 | 667 | ||
@@ -688,30 +678,38 @@ impl<'a> InferenceContext<'a> { | |||
688 | ); | 678 | ); |
689 | self.infer_expr( | 679 | self.infer_expr( |
690 | *repeat, | 680 | *repeat, |
691 | &Expectation::has_type(Ty::simple(TypeCtor::Int(IntTy::usize()))), | 681 | &Expectation::has_type(Ty::Scalar(Scalar::Uint(UintTy::Usize))), |
692 | ); | 682 | ); |
693 | } | 683 | } |
694 | } | 684 | } |
695 | 685 | ||
696 | Ty::apply_one(TypeCtor::Array, elem_ty) | 686 | Ty::Array(Substs::single(elem_ty)) |
697 | } | 687 | } |
698 | Expr::Literal(lit) => match lit { | 688 | Expr::Literal(lit) => match lit { |
699 | Literal::Bool(..) => Ty::simple(TypeCtor::Bool), | 689 | Literal::Bool(..) => Ty::Scalar(Scalar::Bool), |
700 | Literal::String(..) => { | 690 | Literal::String(..) => Ty::Ref(Mutability::Not, Substs::single(Ty::Str)), |
701 | Ty::apply_one(TypeCtor::Ref(Mutability::Shared), Ty::simple(TypeCtor::Str)) | ||
702 | } | ||
703 | Literal::ByteString(..) => { | 691 | Literal::ByteString(..) => { |
704 | let byte_type = Ty::simple(TypeCtor::Int(IntTy::u8())); | 692 | let byte_type = Ty::Scalar(Scalar::Uint(UintTy::U8)); |
705 | let array_type = Ty::apply_one(TypeCtor::Array, byte_type); | 693 | let array_type = Ty::Array(Substs::single(byte_type)); |
706 | Ty::apply_one(TypeCtor::Ref(Mutability::Shared), array_type) | 694 | Ty::Ref(Mutability::Not, Substs::single(array_type)) |
707 | } | 695 | } |
708 | Literal::Char(..) => Ty::simple(TypeCtor::Char), | 696 | Literal::Char(..) => Ty::Scalar(Scalar::Char), |
709 | Literal::Int(_v, ty) => match ty { | 697 | Literal::Int(_v, ty) => match ty { |
710 | Some(int_ty) => Ty::simple(TypeCtor::Int((*int_ty).into())), | 698 | Some(int_ty) => { |
699 | Ty::Scalar(Scalar::Int(primitive::int_ty_from_builtin(*int_ty))) | ||
700 | } | ||
701 | None => self.table.new_integer_var(), | ||
702 | }, | ||
703 | Literal::Uint(_v, ty) => match ty { | ||
704 | Some(int_ty) => { | ||
705 | Ty::Scalar(Scalar::Uint(primitive::uint_ty_from_builtin(*int_ty))) | ||
706 | } | ||
711 | None => self.table.new_integer_var(), | 707 | None => self.table.new_integer_var(), |
712 | }, | 708 | }, |
713 | Literal::Float(_v, ty) => match ty { | 709 | Literal::Float(_v, ty) => match ty { |
714 | Some(float_ty) => Ty::simple(TypeCtor::Float((*float_ty).into())), | 710 | Some(float_ty) => { |
711 | Ty::Scalar(Scalar::Float(primitive::float_ty_from_builtin(*float_ty))) | ||
712 | } | ||
715 | None => self.table.new_float_var(), | 713 | None => self.table.new_float_var(), |
716 | }, | 714 | }, |
717 | }, | 715 | }, |
@@ -767,7 +765,7 @@ impl<'a> InferenceContext<'a> { | |||
767 | // `!`). | 765 | // `!`). |
768 | if self.diverges.is_always() { | 766 | if self.diverges.is_always() { |
769 | // we don't even make an attempt at coercion | 767 | // we don't even make an attempt at coercion |
770 | self.table.new_maybe_never_type_var() | 768 | self.table.new_maybe_never_var() |
771 | } else { | 769 | } else { |
772 | self.coerce(&Ty::unit(), expected.coercion_target()); | 770 | self.coerce(&Ty::unit(), expected.coercion_target()); |
773 | Ty::unit() | 771 | Ty::unit() |
@@ -824,7 +822,7 @@ impl<'a> InferenceContext<'a> { | |||
824 | // Apply autoref so the below unification works correctly | 822 | // Apply autoref so the below unification works correctly |
825 | // FIXME: return correct autorefs from lookup_method | 823 | // FIXME: return correct autorefs from lookup_method |
826 | let actual_receiver_ty = match expected_receiver_ty.as_reference() { | 824 | let actual_receiver_ty = match expected_receiver_ty.as_reference() { |
827 | Some((_, mutability)) => Ty::apply_one(TypeCtor::Ref(mutability), derefed_receiver_ty), | 825 | Some((_, mutability)) => Ty::Ref(mutability, Substs::single(derefed_receiver_ty)), |
828 | _ => derefed_receiver_ty, | 826 | _ => derefed_receiver_ty, |
829 | }; | 827 | }; |
830 | self.unify(&expected_receiver_ty, &actual_receiver_ty); | 828 | self.unify(&expected_receiver_ty, &actual_receiver_ty); |
@@ -901,30 +899,26 @@ impl<'a> InferenceContext<'a> { | |||
901 | } | 899 | } |
902 | 900 | ||
903 | fn register_obligations_for_call(&mut self, callable_ty: &Ty) { | 901 | fn register_obligations_for_call(&mut self, callable_ty: &Ty) { |
904 | if let Ty::Apply(a_ty) = callable_ty { | 902 | if let &Ty::FnDef(def, ref parameters) = callable_ty { |
905 | if let TypeCtor::FnDef(def) = a_ty.ctor { | 903 | let generic_predicates = self.db.generic_predicates(def.into()); |
906 | let generic_predicates = self.db.generic_predicates(def.into()); | 904 | for predicate in generic_predicates.iter() { |
907 | for predicate in generic_predicates.iter() { | 905 | let predicate = predicate.clone().subst(parameters); |
908 | let predicate = predicate.clone().subst(&a_ty.parameters); | 906 | if let Some(obligation) = Obligation::from_predicate(predicate) { |
909 | if let Some(obligation) = Obligation::from_predicate(predicate) { | 907 | self.obligations.push(obligation); |
910 | self.obligations.push(obligation); | ||
911 | } | ||
912 | } | 908 | } |
913 | // add obligation for trait implementation, if this is a trait method | 909 | } |
914 | match def { | 910 | // add obligation for trait implementation, if this is a trait method |
915 | CallableDefId::FunctionId(f) => { | 911 | match def { |
916 | if let AssocContainerId::TraitId(trait_) = | 912 | CallableDefId::FunctionId(f) => { |
917 | f.lookup(self.db.upcast()).container | 913 | if let AssocContainerId::TraitId(trait_) = f.lookup(self.db.upcast()).container |
918 | { | 914 | { |
919 | // construct a TraitDef | 915 | // construct a TraitDef |
920 | let substs = a_ty | 916 | let substs = |
921 | .parameters | 917 | parameters.prefix(generics(self.db.upcast(), trait_.into()).len()); |
922 | .prefix(generics(self.db.upcast(), trait_.into()).len()); | 918 | self.obligations.push(Obligation::Trait(TraitRef { trait_, substs })); |
923 | self.obligations.push(Obligation::Trait(TraitRef { trait_, substs })); | ||
924 | } | ||
925 | } | 919 | } |
926 | CallableDefId::StructId(_) | CallableDefId::EnumVariantId(_) => {} | ||
927 | } | 920 | } |
921 | CallableDefId::StructId(_) | CallableDefId::EnumVariantId(_) => {} | ||
928 | } | 922 | } |
929 | } | 923 | } |
930 | } | 924 | } |
diff --git a/crates/hir_ty/src/infer/pat.rs b/crates/hir_ty/src/infer/pat.rs index d974f805b..eb099311c 100644 --- a/crates/hir_ty/src/infer/pat.rs +++ b/crates/hir_ty/src/infer/pat.rs | |||
@@ -3,17 +3,17 @@ | |||
3 | use std::iter::repeat; | 3 | use std::iter::repeat; |
4 | use std::sync::Arc; | 4 | use std::sync::Arc; |
5 | 5 | ||
6 | use chalk_ir::Mutability; | ||
6 | use hir_def::{ | 7 | use hir_def::{ |
7 | expr::{BindingAnnotation, Expr, Literal, Pat, PatId, RecordFieldPat}, | 8 | expr::{BindingAnnotation, Expr, Literal, Pat, PatId, RecordFieldPat}, |
8 | path::Path, | 9 | path::Path, |
9 | type_ref::Mutability, | ||
10 | FieldId, | 10 | FieldId, |
11 | }; | 11 | }; |
12 | use hir_expand::name::Name; | 12 | use hir_expand::name::Name; |
13 | use test_utils::mark; | 13 | use test_utils::mark; |
14 | 14 | ||
15 | use super::{BindingMode, Expectation, InferenceContext}; | 15 | use super::{BindingMode, Expectation, InferenceContext}; |
16 | use crate::{utils::variant_data, Substs, Ty, TypeCtor}; | 16 | use crate::{lower::lower_to_chalk_mutability, utils::variant_data, Substs, Ty}; |
17 | 17 | ||
18 | impl<'a> InferenceContext<'a> { | 18 | impl<'a> InferenceContext<'a> { |
19 | fn infer_tuple_struct_pat( | 19 | fn infer_tuple_struct_pat( |
@@ -32,7 +32,7 @@ impl<'a> InferenceContext<'a> { | |||
32 | } | 32 | } |
33 | self.unify(&ty, expected); | 33 | self.unify(&ty, expected); |
34 | 34 | ||
35 | let substs = ty.substs().unwrap_or_else(Substs::empty); | 35 | let substs = ty.substs().cloned().unwrap_or_else(Substs::empty); |
36 | 36 | ||
37 | let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default(); | 37 | let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default(); |
38 | let (pre, post) = match ellipsis { | 38 | let (pre, post) = match ellipsis { |
@@ -71,7 +71,7 @@ impl<'a> InferenceContext<'a> { | |||
71 | 71 | ||
72 | self.unify(&ty, expected); | 72 | self.unify(&ty, expected); |
73 | 73 | ||
74 | let substs = ty.substs().unwrap_or_else(Substs::empty); | 74 | let substs = ty.substs().cloned().unwrap_or_else(Substs::empty); |
75 | 75 | ||
76 | let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default(); | 76 | let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default(); |
77 | for subpat in subpats { | 77 | for subpat in subpats { |
@@ -103,7 +103,7 @@ impl<'a> InferenceContext<'a> { | |||
103 | expected = inner; | 103 | expected = inner; |
104 | default_bm = match default_bm { | 104 | default_bm = match default_bm { |
105 | BindingMode::Move => BindingMode::Ref(mutability), | 105 | BindingMode::Move => BindingMode::Ref(mutability), |
106 | BindingMode::Ref(Mutability::Shared) => BindingMode::Ref(Mutability::Shared), | 106 | BindingMode::Ref(Mutability::Not) => BindingMode::Ref(Mutability::Not), |
107 | BindingMode::Ref(Mutability::Mut) => BindingMode::Ref(mutability), | 107 | BindingMode::Ref(Mutability::Mut) => BindingMode::Ref(mutability), |
108 | } | 108 | } |
109 | } | 109 | } |
@@ -138,10 +138,7 @@ impl<'a> InferenceContext<'a> { | |||
138 | inner_tys.extend(expectations_iter.by_ref().take(n_uncovered_patterns).cloned()); | 138 | inner_tys.extend(expectations_iter.by_ref().take(n_uncovered_patterns).cloned()); |
139 | inner_tys.extend(post.iter().zip(expectations_iter).map(infer_pat)); | 139 | inner_tys.extend(post.iter().zip(expectations_iter).map(infer_pat)); |
140 | 140 | ||
141 | Ty::apply( | 141 | Ty::Tuple(inner_tys.len(), Substs(inner_tys.into())) |
142 | TypeCtor::Tuple { cardinality: inner_tys.len() as u16 }, | ||
143 | Substs(inner_tys.into()), | ||
144 | ) | ||
145 | } | 142 | } |
146 | Pat::Or(ref pats) => { | 143 | Pat::Or(ref pats) => { |
147 | if let Some((first_pat, rest)) = pats.split_first() { | 144 | if let Some((first_pat, rest)) = pats.split_first() { |
@@ -155,9 +152,10 @@ impl<'a> InferenceContext<'a> { | |||
155 | } | 152 | } |
156 | } | 153 | } |
157 | Pat::Ref { pat, mutability } => { | 154 | Pat::Ref { pat, mutability } => { |
155 | let mutability = lower_to_chalk_mutability(*mutability); | ||
158 | let expectation = match expected.as_reference() { | 156 | let expectation = match expected.as_reference() { |
159 | Some((inner_ty, exp_mut)) => { | 157 | Some((inner_ty, exp_mut)) => { |
160 | if *mutability != exp_mut { | 158 | if mutability != exp_mut { |
161 | // FIXME: emit type error? | 159 | // FIXME: emit type error? |
162 | } | 160 | } |
163 | inner_ty | 161 | inner_ty |
@@ -165,7 +163,7 @@ impl<'a> InferenceContext<'a> { | |||
165 | _ => &Ty::Unknown, | 163 | _ => &Ty::Unknown, |
166 | }; | 164 | }; |
167 | let subty = self.infer_pat(*pat, expectation, default_bm); | 165 | let subty = self.infer_pat(*pat, expectation, default_bm); |
168 | Ty::apply_one(TypeCtor::Ref(*mutability), subty) | 166 | Ty::Ref(mutability, Substs::single(subty)) |
169 | } | 167 | } |
170 | Pat::TupleStruct { path: p, args: subpats, ellipsis } => self.infer_tuple_struct_pat( | 168 | Pat::TupleStruct { path: p, args: subpats, ellipsis } => self.infer_tuple_struct_pat( |
171 | p.as_ref(), | 169 | p.as_ref(), |
@@ -198,7 +196,7 @@ impl<'a> InferenceContext<'a> { | |||
198 | 196 | ||
199 | let bound_ty = match mode { | 197 | let bound_ty = match mode { |
200 | BindingMode::Ref(mutability) => { | 198 | BindingMode::Ref(mutability) => { |
201 | Ty::apply_one(TypeCtor::Ref(mutability), inner_ty.clone()) | 199 | Ty::Ref(mutability, Substs::single(inner_ty.clone())) |
202 | } | 200 | } |
203 | BindingMode::Move => inner_ty.clone(), | 201 | BindingMode::Move => inner_ty.clone(), |
204 | }; | 202 | }; |
@@ -207,17 +205,17 @@ impl<'a> InferenceContext<'a> { | |||
207 | return inner_ty; | 205 | return inner_ty; |
208 | } | 206 | } |
209 | Pat::Slice { prefix, slice, suffix } => { | 207 | Pat::Slice { prefix, slice, suffix } => { |
210 | let (container_ty, elem_ty) = match &expected { | 208 | let (container_ty, elem_ty): (fn(_) -> _, _) = match &expected { |
211 | ty_app!(TypeCtor::Array, st) => (TypeCtor::Array, st.as_single().clone()), | 209 | Ty::Array(st) => (Ty::Array, st.as_single().clone()), |
212 | ty_app!(TypeCtor::Slice, st) => (TypeCtor::Slice, st.as_single().clone()), | 210 | Ty::Slice(st) => (Ty::Slice, st.as_single().clone()), |
213 | _ => (TypeCtor::Slice, Ty::Unknown), | 211 | _ => (Ty::Slice, Ty::Unknown), |
214 | }; | 212 | }; |
215 | 213 | ||
216 | for pat_id in prefix.iter().chain(suffix) { | 214 | for pat_id in prefix.iter().chain(suffix) { |
217 | self.infer_pat(*pat_id, &elem_ty, default_bm); | 215 | self.infer_pat(*pat_id, &elem_ty, default_bm); |
218 | } | 216 | } |
219 | 217 | ||
220 | let pat_ty = Ty::apply_one(container_ty, elem_ty); | 218 | let pat_ty = container_ty(Substs::single(elem_ty)); |
221 | if let Some(slice_pat_id) = slice { | 219 | if let Some(slice_pat_id) = slice { |
222 | self.infer_pat(*slice_pat_id, &pat_ty, default_bm); | 220 | self.infer_pat(*slice_pat_id, &pat_ty, default_bm); |
223 | } | 221 | } |
@@ -239,7 +237,7 @@ impl<'a> InferenceContext<'a> { | |||
239 | }; | 237 | }; |
240 | 238 | ||
241 | let inner_ty = self.infer_pat(*inner, inner_expected, default_bm); | 239 | let inner_ty = self.infer_pat(*inner, inner_expected, default_bm); |
242 | Ty::apply_one(TypeCtor::Adt(box_adt), inner_ty) | 240 | Ty::Adt(box_adt, Substs::single(inner_ty)) |
243 | } | 241 | } |
244 | None => Ty::Unknown, | 242 | None => Ty::Unknown, |
245 | }, | 243 | }, |
diff --git a/crates/hir_ty/src/infer/unify.rs b/crates/hir_ty/src/infer/unify.rs index 76984242e..99a89a7f3 100644 --- a/crates/hir_ty/src/infer/unify.rs +++ b/crates/hir_ty/src/infer/unify.rs | |||
@@ -2,14 +2,15 @@ | |||
2 | 2 | ||
3 | use std::borrow::Cow; | 3 | use std::borrow::Cow; |
4 | 4 | ||
5 | use chalk_ir::{FloatTy, IntTy, TyVariableKind}; | ||
5 | use ena::unify::{InPlaceUnificationTable, NoError, UnifyKey, UnifyValue}; | 6 | use ena::unify::{InPlaceUnificationTable, NoError, UnifyKey, UnifyValue}; |
6 | 7 | ||
7 | use test_utils::mark; | 8 | use test_utils::mark; |
8 | 9 | ||
9 | use super::{InferenceContext, Obligation}; | 10 | use super::{InferenceContext, Obligation}; |
10 | use crate::{ | 11 | use crate::{ |
11 | BoundVar, Canonical, DebruijnIndex, GenericPredicate, InEnvironment, InferTy, Substs, Ty, | 12 | BoundVar, Canonical, DebruijnIndex, GenericPredicate, InEnvironment, InferenceVar, Scalar, |
12 | TyKind, TypeCtor, TypeWalk, | 13 | Substs, Ty, TypeWalk, |
13 | }; | 14 | }; |
14 | 15 | ||
15 | impl<'a> InferenceContext<'a> { | 16 | impl<'a> InferenceContext<'a> { |
@@ -26,7 +27,7 @@ where | |||
26 | 'a: 'b, | 27 | 'a: 'b, |
27 | { | 28 | { |
28 | ctx: &'b mut InferenceContext<'a>, | 29 | ctx: &'b mut InferenceContext<'a>, |
29 | free_vars: Vec<InferTy>, | 30 | free_vars: Vec<(InferenceVar, TyVariableKind)>, |
30 | /// A stack of type variables that is used to detect recursive types (which | 31 | /// A stack of type variables that is used to detect recursive types (which |
31 | /// are an error, but we need to protect against them to avoid stack | 32 | /// are an error, but we need to protect against them to avoid stack |
32 | /// overflows). | 33 | /// overflows). |
@@ -36,17 +37,14 @@ where | |||
36 | #[derive(Debug)] | 37 | #[derive(Debug)] |
37 | pub(super) struct Canonicalized<T> { | 38 | pub(super) struct Canonicalized<T> { |
38 | pub(super) value: Canonical<T>, | 39 | pub(super) value: Canonical<T>, |
39 | free_vars: Vec<InferTy>, | 40 | free_vars: Vec<(InferenceVar, TyVariableKind)>, |
40 | } | 41 | } |
41 | 42 | ||
42 | impl<'a, 'b> Canonicalizer<'a, 'b> | 43 | impl<'a, 'b> Canonicalizer<'a, 'b> { |
43 | where | 44 | fn add(&mut self, free_var: InferenceVar, kind: TyVariableKind) -> usize { |
44 | 'a: 'b, | 45 | self.free_vars.iter().position(|&(v, _)| v == free_var).unwrap_or_else(|| { |
45 | { | ||
46 | fn add(&mut self, free_var: InferTy) -> usize { | ||
47 | self.free_vars.iter().position(|&v| v == free_var).unwrap_or_else(|| { | ||
48 | let next_index = self.free_vars.len(); | 46 | let next_index = self.free_vars.len(); |
49 | self.free_vars.push(free_var); | 47 | self.free_vars.push((free_var, kind)); |
50 | next_index | 48 | next_index |
51 | }) | 49 | }) |
52 | } | 50 | } |
@@ -54,11 +52,11 @@ where | |||
54 | fn do_canonicalize<T: TypeWalk>(&mut self, t: T, binders: DebruijnIndex) -> T { | 52 | fn do_canonicalize<T: TypeWalk>(&mut self, t: T, binders: DebruijnIndex) -> T { |
55 | t.fold_binders( | 53 | t.fold_binders( |
56 | &mut |ty, binders| match ty { | 54 | &mut |ty, binders| match ty { |
57 | Ty::Infer(tv) => { | 55 | Ty::InferenceVar(var, kind) => { |
58 | let inner = tv.to_inner(); | 56 | let inner = var.to_inner(); |
59 | if self.var_stack.contains(&inner) { | 57 | if self.var_stack.contains(&inner) { |
60 | // recursive type | 58 | // recursive type |
61 | return tv.fallback_value(); | 59 | return self.ctx.table.type_variable_table.fallback_value(var, kind); |
62 | } | 60 | } |
63 | if let Some(known_ty) = | 61 | if let Some(known_ty) = |
64 | self.ctx.table.var_unification_table.inlined_probe_value(inner).known() | 62 | self.ctx.table.var_unification_table.inlined_probe_value(inner).known() |
@@ -69,14 +67,8 @@ where | |||
69 | result | 67 | result |
70 | } else { | 68 | } else { |
71 | let root = self.ctx.table.var_unification_table.find(inner); | 69 | let root = self.ctx.table.var_unification_table.find(inner); |
72 | let free_var = match tv { | 70 | let position = self.add(InferenceVar::from_inner(root), kind); |
73 | InferTy::TypeVar(_) => InferTy::TypeVar(root), | 71 | Ty::BoundVar(BoundVar::new(binders, position)) |
74 | InferTy::IntVar(_) => InferTy::IntVar(root), | ||
75 | InferTy::FloatVar(_) => InferTy::FloatVar(root), | ||
76 | InferTy::MaybeNeverTypeVar(_) => InferTy::MaybeNeverTypeVar(root), | ||
77 | }; | ||
78 | let position = self.add(free_var); | ||
79 | Ty::Bound(BoundVar::new(binders, position)) | ||
80 | } | 72 | } |
81 | } | 73 | } |
82 | _ => ty, | 74 | _ => ty, |
@@ -86,19 +78,7 @@ where | |||
86 | } | 78 | } |
87 | 79 | ||
88 | fn into_canonicalized<T>(self, result: T) -> Canonicalized<T> { | 80 | fn into_canonicalized<T>(self, result: T) -> Canonicalized<T> { |
89 | let kinds = self | 81 | let kinds = self.free_vars.iter().map(|&(_, k)| k).collect(); |
90 | .free_vars | ||
91 | .iter() | ||
92 | .map(|v| match v { | ||
93 | // mapping MaybeNeverTypeVar to the same kind as general ones | ||
94 | // should be fine, because as opposed to int or float type vars, | ||
95 | // they don't restrict what kind of type can go into them, they | ||
96 | // just affect fallback. | ||
97 | InferTy::TypeVar(_) | InferTy::MaybeNeverTypeVar(_) => TyKind::General, | ||
98 | InferTy::IntVar(_) => TyKind::Integer, | ||
99 | InferTy::FloatVar(_) => TyKind::Float, | ||
100 | }) | ||
101 | .collect(); | ||
102 | Canonicalized { value: Canonical { value: result, kinds }, free_vars: self.free_vars } | 82 | Canonicalized { value: Canonical { value: result, kinds }, free_vars: self.free_vars } |
103 | } | 83 | } |
104 | 84 | ||
@@ -130,9 +110,10 @@ impl<T> Canonicalized<T> { | |||
130 | pub(super) fn decanonicalize_ty(&self, mut ty: Ty) -> Ty { | 110 | pub(super) fn decanonicalize_ty(&self, mut ty: Ty) -> Ty { |
131 | ty.walk_mut_binders( | 111 | ty.walk_mut_binders( |
132 | &mut |ty, binders| { | 112 | &mut |ty, binders| { |
133 | if let &mut Ty::Bound(bound) = ty { | 113 | if let &mut Ty::BoundVar(bound) = ty { |
134 | if bound.debruijn >= binders { | 114 | if bound.debruijn >= binders { |
135 | *ty = Ty::Infer(self.free_vars[bound.index]); | 115 | let (v, k) = self.free_vars[bound.index]; |
116 | *ty = Ty::InferenceVar(v, k); | ||
136 | } | 117 | } |
137 | } | 118 | } |
138 | }, | 119 | }, |
@@ -152,18 +133,18 @@ impl<T> Canonicalized<T> { | |||
152 | .kinds | 133 | .kinds |
153 | .iter() | 134 | .iter() |
154 | .map(|k| match k { | 135 | .map(|k| match k { |
155 | TyKind::General => ctx.table.new_type_var(), | 136 | TyVariableKind::General => ctx.table.new_type_var(), |
156 | TyKind::Integer => ctx.table.new_integer_var(), | 137 | TyVariableKind::Integer => ctx.table.new_integer_var(), |
157 | TyKind::Float => ctx.table.new_float_var(), | 138 | TyVariableKind::Float => ctx.table.new_float_var(), |
158 | }) | 139 | }) |
159 | .collect(), | 140 | .collect(), |
160 | ); | 141 | ); |
161 | for (i, ty) in solution.value.into_iter().enumerate() { | 142 | for (i, ty) in solution.value.into_iter().enumerate() { |
162 | let var = self.free_vars[i]; | 143 | let (v, k) = self.free_vars[i]; |
163 | // eagerly replace projections in the type; we may be getting types | 144 | // eagerly replace projections in the type; we may be getting types |
164 | // e.g. from where clauses where this hasn't happened yet | 145 | // e.g. from where clauses where this hasn't happened yet |
165 | let ty = ctx.normalize_associated_types_in(ty.clone().subst_bound_vars(&new_vars)); | 146 | let ty = ctx.normalize_associated_types_in(ty.clone().subst_bound_vars(&new_vars)); |
166 | ctx.table.unify(&Ty::Infer(var), &ty); | 147 | ctx.table.unify(&Ty::InferenceVar(v, k), &ty); |
167 | } | 148 | } |
168 | } | 149 | } |
169 | } | 150 | } |
@@ -187,7 +168,7 @@ pub(crate) fn unify(tys: &Canonical<(Ty, Ty)>) -> Option<Substs> { | |||
187 | // (kind of hacky) | 168 | // (kind of hacky) |
188 | for (i, var) in vars.iter().enumerate() { | 169 | for (i, var) in vars.iter().enumerate() { |
189 | if &*table.resolve_ty_shallow(var) == var { | 170 | if &*table.resolve_ty_shallow(var) == var { |
190 | table.unify(var, &Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, i))); | 171 | table.unify(var, &Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, i))); |
191 | } | 172 | } |
192 | } | 173 | } |
193 | Some( | 174 | Some( |
@@ -198,31 +179,73 @@ pub(crate) fn unify(tys: &Canonical<(Ty, Ty)>) -> Option<Substs> { | |||
198 | } | 179 | } |
199 | 180 | ||
200 | #[derive(Clone, Debug)] | 181 | #[derive(Clone, Debug)] |
182 | pub(super) struct TypeVariableTable { | ||
183 | inner: Vec<TypeVariableData>, | ||
184 | } | ||
185 | |||
186 | impl TypeVariableTable { | ||
187 | fn push(&mut self, data: TypeVariableData) { | ||
188 | self.inner.push(data); | ||
189 | } | ||
190 | |||
191 | pub(super) fn set_diverging(&mut self, iv: InferenceVar, diverging: bool) { | ||
192 | self.inner[iv.to_inner().0 as usize].diverging = diverging; | ||
193 | } | ||
194 | |||
195 | fn is_diverging(&mut self, iv: InferenceVar) -> bool { | ||
196 | self.inner[iv.to_inner().0 as usize].diverging | ||
197 | } | ||
198 | |||
199 | fn fallback_value(&self, iv: InferenceVar, kind: TyVariableKind) -> Ty { | ||
200 | match kind { | ||
201 | _ if self.inner[iv.to_inner().0 as usize].diverging => Ty::Never, | ||
202 | TyVariableKind::General => Ty::Unknown, | ||
203 | TyVariableKind::Integer => Ty::Scalar(Scalar::Int(IntTy::I32)), | ||
204 | TyVariableKind::Float => Ty::Scalar(Scalar::Float(FloatTy::F64)), | ||
205 | } | ||
206 | } | ||
207 | } | ||
208 | |||
209 | #[derive(Copy, Clone, Debug)] | ||
210 | pub(crate) struct TypeVariableData { | ||
211 | diverging: bool, | ||
212 | } | ||
213 | |||
214 | #[derive(Clone, Debug)] | ||
201 | pub(crate) struct InferenceTable { | 215 | pub(crate) struct InferenceTable { |
202 | pub(super) var_unification_table: InPlaceUnificationTable<TypeVarId>, | 216 | pub(super) var_unification_table: InPlaceUnificationTable<TypeVarId>, |
217 | pub(super) type_variable_table: TypeVariableTable, | ||
203 | } | 218 | } |
204 | 219 | ||
205 | impl InferenceTable { | 220 | impl InferenceTable { |
206 | pub(crate) fn new() -> Self { | 221 | pub(crate) fn new() -> Self { |
207 | InferenceTable { var_unification_table: InPlaceUnificationTable::new() } | 222 | InferenceTable { |
223 | var_unification_table: InPlaceUnificationTable::new(), | ||
224 | type_variable_table: TypeVariableTable { inner: Vec::new() }, | ||
225 | } | ||
226 | } | ||
227 | |||
228 | fn new_var(&mut self, kind: TyVariableKind, diverging: bool) -> Ty { | ||
229 | self.type_variable_table.push(TypeVariableData { diverging }); | ||
230 | let key = self.var_unification_table.new_key(TypeVarValue::Unknown); | ||
231 | assert_eq!(key.0 as usize, self.type_variable_table.inner.len() - 1); | ||
232 | Ty::InferenceVar(InferenceVar::from_inner(key), kind) | ||
208 | } | 233 | } |
209 | 234 | ||
210 | pub(crate) fn new_type_var(&mut self) -> Ty { | 235 | pub(crate) fn new_type_var(&mut self) -> Ty { |
211 | Ty::Infer(InferTy::TypeVar(self.var_unification_table.new_key(TypeVarValue::Unknown))) | 236 | self.new_var(TyVariableKind::General, false) |
212 | } | 237 | } |
213 | 238 | ||
214 | pub(crate) fn new_integer_var(&mut self) -> Ty { | 239 | pub(crate) fn new_integer_var(&mut self) -> Ty { |
215 | Ty::Infer(InferTy::IntVar(self.var_unification_table.new_key(TypeVarValue::Unknown))) | 240 | self.new_var(TyVariableKind::Integer, false) |
216 | } | 241 | } |
217 | 242 | ||
218 | pub(crate) fn new_float_var(&mut self) -> Ty { | 243 | pub(crate) fn new_float_var(&mut self) -> Ty { |
219 | Ty::Infer(InferTy::FloatVar(self.var_unification_table.new_key(TypeVarValue::Unknown))) | 244 | self.new_var(TyVariableKind::Float, false) |
220 | } | 245 | } |
221 | 246 | ||
222 | pub(crate) fn new_maybe_never_type_var(&mut self) -> Ty { | 247 | pub(crate) fn new_maybe_never_var(&mut self) -> Ty { |
223 | Ty::Infer(InferTy::MaybeNeverTypeVar( | 248 | self.new_var(TyVariableKind::General, true) |
224 | self.var_unification_table.new_key(TypeVarValue::Unknown), | ||
225 | )) | ||
226 | } | 249 | } |
227 | 250 | ||
228 | pub(crate) fn resolve_ty_completely(&mut self, ty: Ty) -> Ty { | 251 | pub(crate) fn resolve_ty_completely(&mut self, ty: Ty) -> Ty { |
@@ -257,12 +280,14 @@ impl InferenceTable { | |||
257 | // try to resolve type vars first | 280 | // try to resolve type vars first |
258 | let ty1 = self.resolve_ty_shallow(ty1); | 281 | let ty1 = self.resolve_ty_shallow(ty1); |
259 | let ty2 = self.resolve_ty_shallow(ty2); | 282 | let ty2 = self.resolve_ty_shallow(ty2); |
260 | match (&*ty1, &*ty2) { | 283 | if ty1.equals_ctor(&ty2) { |
261 | (Ty::Apply(a_ty1), Ty::Apply(a_ty2)) if a_ty1.ctor == a_ty2.ctor => { | 284 | match (ty1.substs(), ty2.substs()) { |
262 | self.unify_substs(&a_ty1.parameters, &a_ty2.parameters, depth + 1) | 285 | (Some(st1), Some(st2)) => self.unify_substs(st1, st2, depth + 1), |
286 | (None, None) => true, | ||
287 | _ => false, | ||
263 | } | 288 | } |
264 | 289 | } else { | |
265 | _ => self.unify_inner_trivial(&ty1, &ty2, depth), | 290 | self.unify_inner_trivial(&ty1, &ty2, depth) |
266 | } | 291 | } |
267 | } | 292 | } |
268 | 293 | ||
@@ -281,31 +306,46 @@ impl InferenceTable { | |||
281 | true | 306 | true |
282 | } | 307 | } |
283 | 308 | ||
284 | (Ty::Infer(InferTy::TypeVar(tv1)), Ty::Infer(InferTy::TypeVar(tv2))) | 309 | ( |
285 | | (Ty::Infer(InferTy::IntVar(tv1)), Ty::Infer(InferTy::IntVar(tv2))) | 310 | Ty::InferenceVar(tv1, TyVariableKind::General), |
286 | | (Ty::Infer(InferTy::FloatVar(tv1)), Ty::Infer(InferTy::FloatVar(tv2))) | 311 | Ty::InferenceVar(tv2, TyVariableKind::General), |
312 | ) | ||
287 | | ( | 313 | | ( |
288 | Ty::Infer(InferTy::MaybeNeverTypeVar(tv1)), | 314 | Ty::InferenceVar(tv1, TyVariableKind::Integer), |
289 | Ty::Infer(InferTy::MaybeNeverTypeVar(tv2)), | 315 | Ty::InferenceVar(tv2, TyVariableKind::Integer), |
290 | ) => { | 316 | ) |
317 | | ( | ||
318 | Ty::InferenceVar(tv1, TyVariableKind::Float), | ||
319 | Ty::InferenceVar(tv2, TyVariableKind::Float), | ||
320 | ) if self.type_variable_table.is_diverging(*tv1) | ||
321 | == self.type_variable_table.is_diverging(*tv2) => | ||
322 | { | ||
291 | // both type vars are unknown since we tried to resolve them | 323 | // both type vars are unknown since we tried to resolve them |
292 | self.var_unification_table.union(*tv1, *tv2); | 324 | self.var_unification_table.union(tv1.to_inner(), tv2.to_inner()); |
293 | true | 325 | true |
294 | } | 326 | } |
295 | 327 | ||
296 | // The order of MaybeNeverTypeVar matters here. | 328 | // The order of MaybeNeverTypeVar matters here. |
297 | // Unifying MaybeNeverTypeVar and TypeVar will let the latter become MaybeNeverTypeVar. | 329 | // Unifying MaybeNeverTypeVar and TypeVar will let the latter become MaybeNeverTypeVar. |
298 | // Unifying MaybeNeverTypeVar and other concrete type will let the former become it. | 330 | // Unifying MaybeNeverTypeVar and other concrete type will let the former become it. |
299 | (Ty::Infer(InferTy::TypeVar(tv)), other) | 331 | (Ty::InferenceVar(tv, TyVariableKind::General), other) |
300 | | (other, Ty::Infer(InferTy::TypeVar(tv))) | 332 | | (other, Ty::InferenceVar(tv, TyVariableKind::General)) |
301 | | (Ty::Infer(InferTy::MaybeNeverTypeVar(tv)), other) | 333 | | (Ty::InferenceVar(tv, TyVariableKind::Integer), other @ Ty::Scalar(Scalar::Int(_))) |
302 | | (other, Ty::Infer(InferTy::MaybeNeverTypeVar(tv))) | 334 | | (other @ Ty::Scalar(Scalar::Int(_)), Ty::InferenceVar(tv, TyVariableKind::Integer)) |
303 | | (Ty::Infer(InferTy::IntVar(tv)), other @ ty_app!(TypeCtor::Int(_))) | 335 | | ( |
304 | | (other @ ty_app!(TypeCtor::Int(_)), Ty::Infer(InferTy::IntVar(tv))) | 336 | Ty::InferenceVar(tv, TyVariableKind::Integer), |
305 | | (Ty::Infer(InferTy::FloatVar(tv)), other @ ty_app!(TypeCtor::Float(_))) | 337 | other @ Ty::Scalar(Scalar::Uint(_)), |
306 | | (other @ ty_app!(TypeCtor::Float(_)), Ty::Infer(InferTy::FloatVar(tv))) => { | 338 | ) |
339 | | ( | ||
340 | other @ Ty::Scalar(Scalar::Uint(_)), | ||
341 | Ty::InferenceVar(tv, TyVariableKind::Integer), | ||
342 | ) | ||
343 | | (Ty::InferenceVar(tv, TyVariableKind::Float), other @ Ty::Scalar(Scalar::Float(_))) | ||
344 | | (other @ Ty::Scalar(Scalar::Float(_)), Ty::InferenceVar(tv, TyVariableKind::Float)) => | ||
345 | { | ||
307 | // the type var is unknown since we tried to resolve it | 346 | // the type var is unknown since we tried to resolve it |
308 | self.var_unification_table.union_value(*tv, TypeVarValue::Known(other.clone())); | 347 | self.var_unification_table |
348 | .union_value(tv.to_inner(), TypeVarValue::Known(other.clone())); | ||
309 | true | 349 | true |
310 | } | 350 | } |
311 | 351 | ||
@@ -350,7 +390,7 @@ impl InferenceTable { | |||
350 | mark::hit!(type_var_resolves_to_int_var); | 390 | mark::hit!(type_var_resolves_to_int_var); |
351 | } | 391 | } |
352 | match &*ty { | 392 | match &*ty { |
353 | Ty::Infer(tv) => { | 393 | Ty::InferenceVar(tv, _) => { |
354 | let inner = tv.to_inner(); | 394 | let inner = tv.to_inner(); |
355 | match self.var_unification_table.inlined_probe_value(inner).known() { | 395 | match self.var_unification_table.inlined_probe_value(inner).known() { |
356 | Some(known_ty) => { | 396 | Some(known_ty) => { |
@@ -373,12 +413,12 @@ impl InferenceTable { | |||
373 | /// known type. | 413 | /// known type. |
374 | fn resolve_ty_as_possible_inner(&mut self, tv_stack: &mut Vec<TypeVarId>, ty: Ty) -> Ty { | 414 | fn resolve_ty_as_possible_inner(&mut self, tv_stack: &mut Vec<TypeVarId>, ty: Ty) -> Ty { |
375 | ty.fold(&mut |ty| match ty { | 415 | ty.fold(&mut |ty| match ty { |
376 | Ty::Infer(tv) => { | 416 | Ty::InferenceVar(tv, kind) => { |
377 | let inner = tv.to_inner(); | 417 | let inner = tv.to_inner(); |
378 | if tv_stack.contains(&inner) { | 418 | if tv_stack.contains(&inner) { |
379 | mark::hit!(type_var_cycles_resolve_as_possible); | 419 | mark::hit!(type_var_cycles_resolve_as_possible); |
380 | // recursive type | 420 | // recursive type |
381 | return tv.fallback_value(); | 421 | return self.type_variable_table.fallback_value(tv, kind); |
382 | } | 422 | } |
383 | if let Some(known_ty) = | 423 | if let Some(known_ty) = |
384 | self.var_unification_table.inlined_probe_value(inner).known() | 424 | self.var_unification_table.inlined_probe_value(inner).known() |
@@ -400,12 +440,12 @@ impl InferenceTable { | |||
400 | /// replaced by Ty::Unknown. | 440 | /// replaced by Ty::Unknown. |
401 | fn resolve_ty_completely_inner(&mut self, tv_stack: &mut Vec<TypeVarId>, ty: Ty) -> Ty { | 441 | fn resolve_ty_completely_inner(&mut self, tv_stack: &mut Vec<TypeVarId>, ty: Ty) -> Ty { |
402 | ty.fold(&mut |ty| match ty { | 442 | ty.fold(&mut |ty| match ty { |
403 | Ty::Infer(tv) => { | 443 | Ty::InferenceVar(tv, kind) => { |
404 | let inner = tv.to_inner(); | 444 | let inner = tv.to_inner(); |
405 | if tv_stack.contains(&inner) { | 445 | if tv_stack.contains(&inner) { |
406 | mark::hit!(type_var_cycles_resolve_completely); | 446 | mark::hit!(type_var_cycles_resolve_completely); |
407 | // recursive type | 447 | // recursive type |
408 | return tv.fallback_value(); | 448 | return self.type_variable_table.fallback_value(tv, kind); |
409 | } | 449 | } |
410 | if let Some(known_ty) = | 450 | if let Some(known_ty) = |
411 | self.var_unification_table.inlined_probe_value(inner).known() | 451 | self.var_unification_table.inlined_probe_value(inner).known() |
@@ -416,7 +456,7 @@ impl InferenceTable { | |||
416 | tv_stack.pop(); | 456 | tv_stack.pop(); |
417 | result | 457 | result |
418 | } else { | 458 | } else { |
419 | tv.fallback_value() | 459 | self.type_variable_table.fallback_value(tv, kind) |
420 | } | 460 | } |
421 | } | 461 | } |
422 | _ => ty, | 462 | _ => ty, |
@@ -426,7 +466,7 @@ impl InferenceTable { | |||
426 | 466 | ||
427 | /// The ID of a type variable. | 467 | /// The ID of a type variable. |
428 | #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] | 468 | #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] |
429 | pub struct TypeVarId(pub(super) u32); | 469 | pub(super) struct TypeVarId(pub(super) u32); |
430 | 470 | ||
431 | impl UnifyKey for TypeVarId { | 471 | impl UnifyKey for TypeVarId { |
432 | type Value = TypeVarValue; | 472 | type Value = TypeVarValue; |
@@ -447,7 +487,7 @@ impl UnifyKey for TypeVarId { | |||
447 | /// The value of a type variable: either we already know the type, or we don't | 487 | /// The value of a type variable: either we already know the type, or we don't |
448 | /// know it yet. | 488 | /// know it yet. |
449 | #[derive(Clone, PartialEq, Eq, Debug)] | 489 | #[derive(Clone, PartialEq, Eq, Debug)] |
450 | pub enum TypeVarValue { | 490 | pub(super) enum TypeVarValue { |
451 | Known(Ty), | 491 | Known(Ty), |
452 | Unknown, | 492 | Unknown, |
453 | } | 493 | } |
diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 50d248674..c2a20c480 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs | |||
@@ -25,32 +25,29 @@ mod test_db; | |||
25 | 25 | ||
26 | use std::{iter, mem, ops::Deref, sync::Arc}; | 26 | use std::{iter, mem, ops::Deref, sync::Arc}; |
27 | 27 | ||
28 | use base_db::{salsa, CrateId}; | 28 | use base_db::salsa; |
29 | use hir_def::{ | 29 | use hir_def::{ |
30 | builtin_type::BuiltinType, | 30 | builtin_type::BuiltinType, expr::ExprId, type_ref::Rawness, AdtId, AssocContainerId, |
31 | expr::ExprId, | 31 | DefWithBodyId, FunctionId, GenericDefId, HasModule, LifetimeParamId, Lookup, TraitId, |
32 | type_ref::{Mutability, Rawness}, | 32 | TypeAliasId, TypeParamId, |
33 | AdtId, AssocContainerId, DefWithBodyId, FunctionId, GenericDefId, HasModule, LifetimeParamId, | ||
34 | Lookup, TraitId, TypeAliasId, TypeParamId, | ||
35 | }; | 33 | }; |
36 | use itertools::Itertools; | 34 | use itertools::Itertools; |
37 | 35 | ||
38 | use crate::{ | 36 | use crate::{ |
39 | db::HirDatabase, | 37 | db::HirDatabase, |
40 | display::HirDisplay, | 38 | display::HirDisplay, |
41 | primitive::{FloatTy, IntTy}, | ||
42 | utils::{generics, make_mut_slice, Generics}, | 39 | utils::{generics, make_mut_slice, Generics}, |
43 | }; | 40 | }; |
44 | 41 | ||
45 | pub use autoderef::autoderef; | 42 | pub use autoderef::autoderef; |
46 | pub use infer::{InferTy, InferenceResult}; | 43 | pub use infer::{InferenceResult, InferenceVar}; |
47 | pub use lower::{ | 44 | pub use lower::{ |
48 | associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode, | 45 | associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode, |
49 | TyDefId, TyLoweringContext, ValueTyDefId, | 46 | TyDefId, TyLoweringContext, ValueTyDefId, |
50 | }; | 47 | }; |
51 | pub use traits::{InEnvironment, Obligation, ProjectionPredicate, TraitEnvironment}; | 48 | pub use traits::{InEnvironment, Obligation, ProjectionPredicate, TraitEnvironment}; |
52 | 49 | ||
53 | pub use chalk_ir::{BoundVar, DebruijnIndex}; | 50 | pub use chalk_ir::{BoundVar, DebruijnIndex, Mutability, Scalar, TyVariableKind}; |
54 | 51 | ||
55 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] | 52 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] |
56 | pub enum Lifetime { | 53 | pub enum Lifetime { |
@@ -58,211 +55,6 @@ pub enum Lifetime { | |||
58 | Static, | 55 | Static, |
59 | } | 56 | } |
60 | 57 | ||
61 | /// A type constructor or type name: this might be something like the primitive | ||
62 | /// type `bool`, a struct like `Vec`, or things like function pointers or | ||
63 | /// tuples. | ||
64 | #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] | ||
65 | pub enum TypeCtor { | ||
66 | /// The primitive boolean type. Written as `bool`. | ||
67 | Bool, | ||
68 | |||
69 | /// The primitive character type; holds a Unicode scalar value | ||
70 | /// (a non-surrogate code point). Written as `char`. | ||
71 | Char, | ||
72 | |||
73 | /// A primitive integer type. For example, `i32`. | ||
74 | Int(IntTy), | ||
75 | |||
76 | /// A primitive floating-point type. For example, `f64`. | ||
77 | Float(FloatTy), | ||
78 | |||
79 | /// Structures, enumerations and unions. | ||
80 | Adt(AdtId), | ||
81 | |||
82 | /// The pointee of a string slice. Written as `str`. | ||
83 | Str, | ||
84 | |||
85 | /// The pointee of an array slice. Written as `[T]`. | ||
86 | Slice, | ||
87 | |||
88 | /// An array with the given length. Written as `[T; n]`. | ||
89 | Array, | ||
90 | |||
91 | /// A raw pointer. Written as `*mut T` or `*const T` | ||
92 | RawPtr(Mutability), | ||
93 | |||
94 | /// A reference; a pointer with an associated lifetime. Written as | ||
95 | /// `&'a mut T` or `&'a T`. | ||
96 | Ref(Mutability), | ||
97 | |||
98 | /// The anonymous type of a function declaration/definition. Each | ||
99 | /// function has a unique type, which is output (for a function | ||
100 | /// named `foo` returning an `i32`) as `fn() -> i32 {foo}`. | ||
101 | /// | ||
102 | /// This includes tuple struct / enum variant constructors as well. | ||
103 | /// | ||
104 | /// For example the type of `bar` here: | ||
105 | /// | ||
106 | /// ``` | ||
107 | /// fn foo() -> i32 { 1 } | ||
108 | /// let bar = foo; // bar: fn() -> i32 {foo} | ||
109 | /// ``` | ||
110 | FnDef(CallableDefId), | ||
111 | |||
112 | /// A pointer to a function. Written as `fn() -> i32`. | ||
113 | /// | ||
114 | /// For example the type of `bar` here: | ||
115 | /// | ||
116 | /// ``` | ||
117 | /// fn foo() -> i32 { 1 } | ||
118 | /// let bar: fn() -> i32 = foo; | ||
119 | /// ``` | ||
120 | // FIXME make this a Ty variant like in Chalk | ||
121 | FnPtr { num_args: u16, is_varargs: bool }, | ||
122 | |||
123 | /// The never type `!`. | ||
124 | Never, | ||
125 | |||
126 | /// A tuple type. For example, `(i32, bool)`. | ||
127 | Tuple { cardinality: u16 }, | ||
128 | |||
129 | /// Represents an associated item like `Iterator::Item`. This is used | ||
130 | /// when we have tried to normalize a projection like `T::Item` but | ||
131 | /// couldn't find a better representation. In that case, we generate | ||
132 | /// an **application type** like `(Iterator::Item)<T>`. | ||
133 | AssociatedType(TypeAliasId), | ||
134 | |||
135 | /// This represents a placeholder for an opaque type in situations where we | ||
136 | /// don't know the hidden type (i.e. currently almost always). This is | ||
137 | /// analogous to the `AssociatedType` type constructor. | ||
138 | /// It is also used as the type of async block, with one type parameter | ||
139 | /// representing the Future::Output type. | ||
140 | OpaqueType(OpaqueTyId), | ||
141 | |||
142 | /// Represents a foreign type declared in external blocks. | ||
143 | ForeignType(TypeAliasId), | ||
144 | |||
145 | /// The type of a specific closure. | ||
146 | /// | ||
147 | /// The closure signature is stored in a `FnPtr` type in the first type | ||
148 | /// parameter. | ||
149 | Closure { def: DefWithBodyId, expr: ExprId }, | ||
150 | } | ||
151 | |||
152 | impl TypeCtor { | ||
153 | pub fn num_ty_params(self, db: &dyn HirDatabase) -> usize { | ||
154 | match self { | ||
155 | TypeCtor::Bool | ||
156 | | TypeCtor::Char | ||
157 | | TypeCtor::Int(_) | ||
158 | | TypeCtor::Float(_) | ||
159 | | TypeCtor::Str | ||
160 | | TypeCtor::Never => 0, | ||
161 | TypeCtor::Slice | ||
162 | | TypeCtor::Array | ||
163 | | TypeCtor::RawPtr(_) | ||
164 | | TypeCtor::Ref(_) | ||
165 | | TypeCtor::Closure { .. } // 1 param representing the signature of the closure | ||
166 | => 1, | ||
167 | TypeCtor::Adt(adt) => { | ||
168 | let generic_params = generics(db.upcast(), adt.into()); | ||
169 | generic_params.len() | ||
170 | } | ||
171 | TypeCtor::FnDef(callable) => { | ||
172 | let generic_params = generics(db.upcast(), callable.into()); | ||
173 | generic_params.len() | ||
174 | } | ||
175 | TypeCtor::AssociatedType(type_alias) => { | ||
176 | let generic_params = generics(db.upcast(), type_alias.into()); | ||
177 | generic_params.len() | ||
178 | } | ||
179 | TypeCtor::ForeignType(type_alias) => { | ||
180 | let generic_params = generics(db.upcast(), type_alias.into()); | ||
181 | generic_params.len() | ||
182 | } | ||
183 | TypeCtor::OpaqueType(opaque_ty_id) => { | ||
184 | match opaque_ty_id { | ||
185 | OpaqueTyId::ReturnTypeImplTrait(func, _) => { | ||
186 | let generic_params = generics(db.upcast(), func.into()); | ||
187 | generic_params.len() | ||
188 | } | ||
189 | // 1 param representing Future::Output type. | ||
190 | OpaqueTyId::AsyncBlockTypeImplTrait(..) => 1, | ||
191 | } | ||
192 | } | ||
193 | TypeCtor::FnPtr { num_args, is_varargs: _ } => num_args as usize + 1, | ||
194 | TypeCtor::Tuple { cardinality } => cardinality as usize, | ||
195 | } | ||
196 | } | ||
197 | |||
198 | pub fn krate(self, db: &dyn HirDatabase) -> Option<CrateId> { | ||
199 | match self { | ||
200 | TypeCtor::Bool | ||
201 | | TypeCtor::Char | ||
202 | | TypeCtor::Int(_) | ||
203 | | TypeCtor::Float(_) | ||
204 | | TypeCtor::Str | ||
205 | | TypeCtor::Never | ||
206 | | TypeCtor::Slice | ||
207 | | TypeCtor::Array | ||
208 | | TypeCtor::RawPtr(_) | ||
209 | | TypeCtor::Ref(_) | ||
210 | | TypeCtor::FnPtr { .. } | ||
211 | | TypeCtor::Tuple { .. } => None, | ||
212 | // Closure's krate is irrelevant for coherence I would think? | ||
213 | TypeCtor::Closure { .. } => None, | ||
214 | TypeCtor::Adt(adt) => Some(adt.module(db.upcast()).krate()), | ||
215 | TypeCtor::FnDef(callable) => Some(callable.krate(db)), | ||
216 | TypeCtor::AssociatedType(type_alias) => { | ||
217 | Some(type_alias.lookup(db.upcast()).module(db.upcast()).krate()) | ||
218 | } | ||
219 | TypeCtor::ForeignType(type_alias) => { | ||
220 | Some(type_alias.lookup(db.upcast()).module(db.upcast()).krate()) | ||
221 | } | ||
222 | TypeCtor::OpaqueType(opaque_ty_id) => match opaque_ty_id { | ||
223 | OpaqueTyId::ReturnTypeImplTrait(func, _) => { | ||
224 | Some(func.lookup(db.upcast()).module(db.upcast()).krate()) | ||
225 | } | ||
226 | OpaqueTyId::AsyncBlockTypeImplTrait(def, _) => { | ||
227 | Some(def.module(db.upcast()).krate()) | ||
228 | } | ||
229 | }, | ||
230 | } | ||
231 | } | ||
232 | |||
233 | pub fn as_generic_def(self) -> Option<GenericDefId> { | ||
234 | match self { | ||
235 | TypeCtor::Bool | ||
236 | | TypeCtor::Char | ||
237 | | TypeCtor::Int(_) | ||
238 | | TypeCtor::Float(_) | ||
239 | | TypeCtor::Str | ||
240 | | TypeCtor::Never | ||
241 | | TypeCtor::Slice | ||
242 | | TypeCtor::Array | ||
243 | | TypeCtor::RawPtr(_) | ||
244 | | TypeCtor::Ref(_) | ||
245 | | TypeCtor::FnPtr { .. } | ||
246 | | TypeCtor::Tuple { .. } | ||
247 | | TypeCtor::Closure { .. } => None, | ||
248 | TypeCtor::Adt(adt) => Some(adt.into()), | ||
249 | TypeCtor::FnDef(callable) => Some(callable.into()), | ||
250 | TypeCtor::AssociatedType(type_alias) => Some(type_alias.into()), | ||
251 | TypeCtor::ForeignType(type_alias) => Some(type_alias.into()), | ||
252 | TypeCtor::OpaqueType(_impl_trait_id) => None, | ||
253 | } | ||
254 | } | ||
255 | } | ||
256 | |||
257 | /// A nominal type with (maybe 0) type parameters. This might be a primitive | ||
258 | /// type like `bool`, a struct, tuple, function pointer, reference or | ||
259 | /// several other things. | ||
260 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] | ||
261 | pub struct ApplicationTy { | ||
262 | pub ctor: TypeCtor, | ||
263 | pub parameters: Substs, | ||
264 | } | ||
265 | |||
266 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] | 58 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] |
267 | pub struct OpaqueTy { | 59 | pub struct OpaqueTy { |
268 | pub opaque_ty_id: OpaqueTyId, | 60 | pub opaque_ty_id: OpaqueTyId, |
@@ -305,29 +97,118 @@ impl TypeWalk for ProjectionTy { | |||
305 | } | 97 | } |
306 | } | 98 | } |
307 | 99 | ||
308 | /// A type. | 100 | #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)] |
309 | /// | 101 | pub struct FnSig { |
310 | /// See also the `TyKind` enum in rustc (librustc/ty/sty.rs), which represents | 102 | pub variadic: bool, |
311 | /// the same thing (but in a different way). | 103 | } |
312 | /// | 104 | |
313 | /// This should be cheap to clone. | ||
314 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] | 105 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] |
315 | pub enum Ty { | 106 | pub struct FnPointer { |
316 | /// A nominal type with (maybe 0) type parameters. This might be a primitive | 107 | pub num_args: usize, |
317 | /// type like `bool`, a struct, tuple, function pointer, reference or | 108 | pub sig: FnSig, |
318 | /// several other things. | 109 | pub substs: Substs, |
319 | Apply(ApplicationTy), | 110 | } |
320 | 111 | ||
112 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] | ||
113 | pub enum AliasTy { | ||
321 | /// A "projection" type corresponds to an (unnormalized) | 114 | /// A "projection" type corresponds to an (unnormalized) |
322 | /// projection like `<P0 as Trait<P1..Pn>>::Foo`. Note that the | 115 | /// projection like `<P0 as Trait<P1..Pn>>::Foo`. Note that the |
323 | /// trait and all its parameters are fully known. | 116 | /// trait and all its parameters are fully known. |
324 | Projection(ProjectionTy), | 117 | Projection(ProjectionTy), |
325 | |||
326 | /// An opaque type (`impl Trait`). | 118 | /// An opaque type (`impl Trait`). |
327 | /// | 119 | /// |
328 | /// This is currently only used for return type impl trait; each instance of | 120 | /// This is currently only used for return type impl trait; each instance of |
329 | /// `impl Trait` in a return type gets its own ID. | 121 | /// `impl Trait` in a return type gets its own ID. |
330 | Opaque(OpaqueTy), | 122 | Opaque(OpaqueTy), |
123 | } | ||
124 | |||
125 | /// A type. | ||
126 | /// | ||
127 | /// See also the `TyKind` enum in rustc (librustc/ty/sty.rs), which represents | ||
128 | /// the same thing (but in a different way). | ||
129 | /// | ||
130 | /// This should be cheap to clone. | ||
131 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] | ||
132 | pub enum Ty { | ||
133 | /// Structures, enumerations and unions. | ||
134 | Adt(AdtId, Substs), | ||
135 | |||
136 | /// Represents an associated item like `Iterator::Item`. This is used | ||
137 | /// when we have tried to normalize a projection like `T::Item` but | ||
138 | /// couldn't find a better representation. In that case, we generate | ||
139 | /// an **application type** like `(Iterator::Item)<T>`. | ||
140 | AssociatedType(TypeAliasId, Substs), | ||
141 | |||
142 | /// a scalar type like `bool` or `u32` | ||
143 | Scalar(Scalar), | ||
144 | |||
145 | /// A tuple type. For example, `(i32, bool)`. | ||
146 | Tuple(usize, Substs), | ||
147 | |||
148 | /// An array with the given length. Written as `[T; n]`. | ||
149 | Array(Substs), | ||
150 | |||
151 | /// The pointee of an array slice. Written as `[T]`. | ||
152 | Slice(Substs), | ||
153 | |||
154 | /// A raw pointer. Written as `*mut T` or `*const T` | ||
155 | Raw(Mutability, Substs), | ||
156 | |||
157 | /// A reference; a pointer with an associated lifetime. Written as | ||
158 | /// `&'a mut T` or `&'a T`. | ||
159 | Ref(Mutability, Substs), | ||
160 | |||
161 | /// This represents a placeholder for an opaque type in situations where we | ||
162 | /// don't know the hidden type (i.e. currently almost always). This is | ||
163 | /// analogous to the `AssociatedType` type constructor. | ||
164 | /// It is also used as the type of async block, with one type parameter | ||
165 | /// representing the Future::Output type. | ||
166 | OpaqueType(OpaqueTyId, Substs), | ||
167 | |||
168 | /// The anonymous type of a function declaration/definition. Each | ||
169 | /// function has a unique type, which is output (for a function | ||
170 | /// named `foo` returning an `i32`) as `fn() -> i32 {foo}`. | ||
171 | /// | ||
172 | /// This includes tuple struct / enum variant constructors as well. | ||
173 | /// | ||
174 | /// For example the type of `bar` here: | ||
175 | /// | ||
176 | /// ``` | ||
177 | /// fn foo() -> i32 { 1 } | ||
178 | /// let bar = foo; // bar: fn() -> i32 {foo} | ||
179 | /// ``` | ||
180 | FnDef(CallableDefId, Substs), | ||
181 | |||
182 | /// The pointee of a string slice. Written as `str`. | ||
183 | Str, | ||
184 | |||
185 | /// The never type `!`. | ||
186 | Never, | ||
187 | |||
188 | /// The type of a specific closure. | ||
189 | /// | ||
190 | /// The closure signature is stored in a `FnPtr` type in the first type | ||
191 | /// parameter. | ||
192 | Closure(DefWithBodyId, ExprId, Substs), | ||
193 | |||
194 | /// Represents a foreign type declared in external blocks. | ||
195 | ForeignType(TypeAliasId), | ||
196 | |||
197 | /// A pointer to a function. Written as `fn() -> i32`. | ||
198 | /// | ||
199 | /// For example the type of `bar` here: | ||
200 | /// | ||
201 | /// ``` | ||
202 | /// fn foo() -> i32 { 1 } | ||
203 | /// let bar: fn() -> i32 = foo; | ||
204 | /// ``` | ||
205 | Function(FnPointer), | ||
206 | |||
207 | /// An "alias" type represents some form of type alias, such as: | ||
208 | /// - An associated type projection like `<T as Iterator>::Item` | ||
209 | /// - `impl Trait` types | ||
210 | /// - Named type aliases like `type Foo<X> = Vec<X>` | ||
211 | Alias(AliasTy), | ||
331 | 212 | ||
332 | /// A placeholder for a type parameter; for example, `T` in `fn f<T>(x: T) | 213 | /// A placeholder for a type parameter; for example, `T` in `fn f<T>(x: T) |
333 | /// {}` when we're type-checking the body of that function. In this | 214 | /// {}` when we're type-checking the body of that function. In this |
@@ -340,10 +221,10 @@ pub enum Ty { | |||
340 | /// parameters get turned into variables; during trait resolution, inference | 221 | /// parameters get turned into variables; during trait resolution, inference |
341 | /// variables get turned into bound variables and back; and in `Dyn` the | 222 | /// variables get turned into bound variables and back; and in `Dyn` the |
342 | /// `Self` type is represented with a bound variable as well. | 223 | /// `Self` type is represented with a bound variable as well. |
343 | Bound(BoundVar), | 224 | BoundVar(BoundVar), |
344 | 225 | ||
345 | /// A type variable used during type checking. | 226 | /// A type variable used during type checking. |
346 | Infer(InferTy), | 227 | InferenceVar(InferenceVar, TyVariableKind), |
347 | 228 | ||
348 | /// A trait object (`dyn Trait` or bare `Trait` in pre-2018 Rust). | 229 | /// A trait object (`dyn Trait` or bare `Trait` in pre-2018 Rust). |
349 | /// | 230 | /// |
@@ -424,7 +305,7 @@ impl Substs { | |||
424 | generic_params | 305 | generic_params |
425 | .iter() | 306 | .iter() |
426 | .enumerate() | 307 | .enumerate() |
427 | .map(|(idx, _)| Ty::Bound(BoundVar::new(debruijn, idx))) | 308 | .map(|(idx, _)| Ty::BoundVar(BoundVar::new(debruijn, idx))) |
428 | .collect(), | 309 | .collect(), |
429 | ) | 310 | ) |
430 | } | 311 | } |
@@ -440,10 +321,6 @@ impl Substs { | |||
440 | Substs::builder(generic_params.len()) | 321 | Substs::builder(generic_params.len()) |
441 | } | 322 | } |
442 | 323 | ||
443 | pub fn build_for_type_ctor(db: &dyn HirDatabase, type_ctor: TypeCtor) -> SubstsBuilder { | ||
444 | Substs::builder(type_ctor.num_ty_params(db)) | ||
445 | } | ||
446 | |||
447 | fn builder(param_count: usize) -> SubstsBuilder { | 324 | fn builder(param_count: usize) -> SubstsBuilder { |
448 | SubstsBuilder { vec: Vec::with_capacity(param_count), param_count } | 325 | SubstsBuilder { vec: Vec::with_capacity(param_count), param_count } |
449 | } | 326 | } |
@@ -476,7 +353,7 @@ impl SubstsBuilder { | |||
476 | } | 353 | } |
477 | 354 | ||
478 | pub fn fill_with_bound_vars(self, debruijn: DebruijnIndex, starting_from: usize) -> Self { | 355 | pub fn fill_with_bound_vars(self, debruijn: DebruijnIndex, starting_from: usize) -> Self { |
479 | self.fill((starting_from..).map(|idx| Ty::Bound(BoundVar::new(debruijn, idx)))) | 356 | self.fill((starting_from..).map(|idx| Ty::BoundVar(BoundVar::new(debruijn, idx)))) |
480 | } | 357 | } |
481 | 358 | ||
482 | pub fn fill_with_unknown(self) -> Self { | 359 | pub fn fill_with_unknown(self) -> Self { |
@@ -656,41 +533,41 @@ impl TypeWalk for GenericPredicate { | |||
656 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 533 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
657 | pub struct Canonical<T> { | 534 | pub struct Canonical<T> { |
658 | pub value: T, | 535 | pub value: T, |
659 | pub kinds: Arc<[TyKind]>, | 536 | pub kinds: Arc<[TyVariableKind]>, |
660 | } | 537 | } |
661 | 538 | ||
662 | impl<T> Canonical<T> { | 539 | impl<T> Canonical<T> { |
663 | pub fn new(value: T, kinds: impl IntoIterator<Item = TyKind>) -> Self { | 540 | pub fn new(value: T, kinds: impl IntoIterator<Item = TyVariableKind>) -> Self { |
664 | Self { value, kinds: kinds.into_iter().collect() } | 541 | Self { value, kinds: kinds.into_iter().collect() } |
665 | } | 542 | } |
666 | } | 543 | } |
667 | 544 | ||
668 | #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] | ||
669 | pub enum TyKind { | ||
670 | General, | ||
671 | Integer, | ||
672 | Float, | ||
673 | } | ||
674 | |||
675 | /// A function signature as seen by type inference: Several parameter types and | 545 | /// A function signature as seen by type inference: Several parameter types and |
676 | /// one return type. | 546 | /// one return type. |
677 | #[derive(Clone, PartialEq, Eq, Debug)] | 547 | #[derive(Clone, PartialEq, Eq, Debug)] |
678 | pub struct FnSig { | 548 | pub struct CallableSig { |
679 | params_and_return: Arc<[Ty]>, | 549 | params_and_return: Arc<[Ty]>, |
680 | is_varargs: bool, | 550 | is_varargs: bool, |
681 | } | 551 | } |
682 | 552 | ||
683 | /// A polymorphic function signature. | 553 | /// A polymorphic function signature. |
684 | pub type PolyFnSig = Binders<FnSig>; | 554 | pub type PolyFnSig = Binders<CallableSig>; |
685 | 555 | ||
686 | impl FnSig { | 556 | impl CallableSig { |
687 | pub fn from_params_and_return(mut params: Vec<Ty>, ret: Ty, is_varargs: bool) -> FnSig { | 557 | pub fn from_params_and_return(mut params: Vec<Ty>, ret: Ty, is_varargs: bool) -> CallableSig { |
688 | params.push(ret); | 558 | params.push(ret); |
689 | FnSig { params_and_return: params.into(), is_varargs } | 559 | CallableSig { params_and_return: params.into(), is_varargs } |
690 | } | 560 | } |
691 | 561 | ||
692 | pub fn from_fn_ptr_substs(substs: &Substs, is_varargs: bool) -> FnSig { | 562 | pub fn from_fn_ptr(fn_ptr: &FnPointer) -> CallableSig { |
693 | FnSig { params_and_return: Arc::clone(&substs.0), is_varargs } | 563 | CallableSig { |
564 | params_and_return: Arc::clone(&fn_ptr.substs.0), | ||
565 | is_varargs: fn_ptr.sig.variadic, | ||
566 | } | ||
567 | } | ||
568 | |||
569 | pub fn from_substs(substs: &Substs) -> CallableSig { | ||
570 | CallableSig { params_and_return: Arc::clone(&substs.0), is_varargs: false } | ||
694 | } | 571 | } |
695 | 572 | ||
696 | pub fn params(&self) -> &[Ty] { | 573 | pub fn params(&self) -> &[Ty] { |
@@ -702,7 +579,7 @@ impl FnSig { | |||
702 | } | 579 | } |
703 | } | 580 | } |
704 | 581 | ||
705 | impl TypeWalk for FnSig { | 582 | impl TypeWalk for CallableSig { |
706 | fn walk(&self, f: &mut impl FnMut(&Ty)) { | 583 | fn walk(&self, f: &mut impl FnMut(&Ty)) { |
707 | for t in self.params_and_return.iter() { | 584 | for t in self.params_and_return.iter() { |
708 | t.walk(f); | 585 | t.walk(f); |
@@ -721,49 +598,42 @@ impl TypeWalk for FnSig { | |||
721 | } | 598 | } |
722 | 599 | ||
723 | impl Ty { | 600 | impl Ty { |
724 | pub fn simple(ctor: TypeCtor) -> Ty { | ||
725 | Ty::Apply(ApplicationTy { ctor, parameters: Substs::empty() }) | ||
726 | } | ||
727 | pub fn apply_one(ctor: TypeCtor, param: Ty) -> Ty { | ||
728 | Ty::Apply(ApplicationTy { ctor, parameters: Substs::single(param) }) | ||
729 | } | ||
730 | pub fn apply(ctor: TypeCtor, parameters: Substs) -> Ty { | ||
731 | Ty::Apply(ApplicationTy { ctor, parameters }) | ||
732 | } | ||
733 | pub fn unit() -> Self { | 601 | pub fn unit() -> Self { |
734 | Ty::apply(TypeCtor::Tuple { cardinality: 0 }, Substs::empty()) | 602 | Ty::Tuple(0, Substs::empty()) |
735 | } | 603 | } |
736 | pub fn fn_ptr(sig: FnSig) -> Self { | 604 | |
737 | Ty::apply( | 605 | pub fn fn_ptr(sig: CallableSig) -> Self { |
738 | TypeCtor::FnPtr { num_args: sig.params().len() as u16, is_varargs: sig.is_varargs }, | 606 | Ty::Function(FnPointer { |
739 | Substs(sig.params_and_return), | 607 | num_args: sig.params().len(), |
740 | ) | 608 | sig: FnSig { variadic: sig.is_varargs }, |
609 | substs: Substs(sig.params_and_return), | ||
610 | }) | ||
741 | } | 611 | } |
612 | |||
742 | pub fn builtin(builtin: BuiltinType) -> Self { | 613 | pub fn builtin(builtin: BuiltinType) -> Self { |
743 | Ty::simple(match builtin { | 614 | match builtin { |
744 | BuiltinType::Char => TypeCtor::Char, | 615 | BuiltinType::Char => Ty::Scalar(Scalar::Char), |
745 | BuiltinType::Bool => TypeCtor::Bool, | 616 | BuiltinType::Bool => Ty::Scalar(Scalar::Bool), |
746 | BuiltinType::Str => TypeCtor::Str, | 617 | BuiltinType::Str => Ty::Str, |
747 | BuiltinType::Int(t) => TypeCtor::Int(IntTy::from(t).into()), | 618 | BuiltinType::Int(t) => Ty::Scalar(Scalar::Int(primitive::int_ty_from_builtin(t))), |
748 | BuiltinType::Float(t) => TypeCtor::Float(FloatTy::from(t).into()), | 619 | BuiltinType::Uint(t) => Ty::Scalar(Scalar::Uint(primitive::uint_ty_from_builtin(t))), |
749 | }) | 620 | BuiltinType::Float(t) => Ty::Scalar(Scalar::Float(primitive::float_ty_from_builtin(t))), |
621 | } | ||
750 | } | 622 | } |
751 | 623 | ||
752 | pub fn as_reference(&self) -> Option<(&Ty, Mutability)> { | 624 | pub fn as_reference(&self) -> Option<(&Ty, Mutability)> { |
753 | match self { | 625 | match self { |
754 | Ty::Apply(ApplicationTy { ctor: TypeCtor::Ref(mutability), parameters }) => { | 626 | Ty::Ref(mutability, parameters) => Some((parameters.as_single(), *mutability)), |
755 | Some((parameters.as_single(), *mutability)) | ||
756 | } | ||
757 | _ => None, | 627 | _ => None, |
758 | } | 628 | } |
759 | } | 629 | } |
760 | 630 | ||
761 | pub fn as_reference_or_ptr(&self) -> Option<(&Ty, Rawness, Mutability)> { | 631 | pub fn as_reference_or_ptr(&self) -> Option<(&Ty, Rawness, Mutability)> { |
762 | match self { | 632 | match self { |
763 | Ty::Apply(ApplicationTy { ctor: TypeCtor::Ref(mutability), parameters }) => { | 633 | Ty::Ref(mutability, parameters) => { |
764 | Some((parameters.as_single(), Rawness::Ref, *mutability)) | 634 | Some((parameters.as_single(), Rawness::Ref, *mutability)) |
765 | } | 635 | } |
766 | Ty::Apply(ApplicationTy { ctor: TypeCtor::RawPtr(mutability), parameters }) => { | 636 | Ty::Raw(mutability, parameters) => { |
767 | Some((parameters.as_single(), Rawness::RawPtr, *mutability)) | 637 | Some((parameters.as_single(), Rawness::RawPtr, *mutability)) |
768 | } | 638 | } |
769 | _ => None, | 639 | _ => None, |
@@ -773,7 +643,7 @@ impl Ty { | |||
773 | pub fn strip_references(&self) -> &Ty { | 643 | pub fn strip_references(&self) -> &Ty { |
774 | let mut t: &Ty = self; | 644 | let mut t: &Ty = self; |
775 | 645 | ||
776 | while let Ty::Apply(ApplicationTy { ctor: TypeCtor::Ref(_mutability), parameters }) = t { | 646 | while let Ty::Ref(_mutability, parameters) = t { |
777 | t = parameters.as_single(); | 647 | t = parameters.as_single(); |
778 | } | 648 | } |
779 | 649 | ||
@@ -782,30 +652,60 @@ impl Ty { | |||
782 | 652 | ||
783 | pub fn as_adt(&self) -> Option<(AdtId, &Substs)> { | 653 | pub fn as_adt(&self) -> Option<(AdtId, &Substs)> { |
784 | match self { | 654 | match self { |
785 | Ty::Apply(ApplicationTy { ctor: TypeCtor::Adt(adt_def), parameters }) => { | 655 | Ty::Adt(adt_def, parameters) => Some((*adt_def, parameters)), |
786 | Some((*adt_def, parameters)) | ||
787 | } | ||
788 | _ => None, | 656 | _ => None, |
789 | } | 657 | } |
790 | } | 658 | } |
791 | 659 | ||
792 | pub fn as_tuple(&self) -> Option<&Substs> { | 660 | pub fn as_tuple(&self) -> Option<&Substs> { |
793 | match self { | 661 | match self { |
794 | Ty::Apply(ApplicationTy { ctor: TypeCtor::Tuple { .. }, parameters }) => { | 662 | Ty::Tuple(_, substs) => Some(substs), |
795 | Some(parameters) | 663 | _ => None, |
796 | } | 664 | } |
665 | } | ||
666 | |||
667 | pub fn as_generic_def(&self) -> Option<GenericDefId> { | ||
668 | match *self { | ||
669 | Ty::Adt(adt, ..) => Some(adt.into()), | ||
670 | Ty::FnDef(callable, ..) => Some(callable.into()), | ||
671 | Ty::AssociatedType(type_alias, ..) => Some(type_alias.into()), | ||
672 | Ty::ForeignType(type_alias, ..) => Some(type_alias.into()), | ||
797 | _ => None, | 673 | _ => None, |
798 | } | 674 | } |
799 | } | 675 | } |
800 | 676 | ||
801 | pub fn is_never(&self) -> bool { | 677 | pub fn is_never(&self) -> bool { |
802 | matches!(self, Ty::Apply(ApplicationTy { ctor: TypeCtor::Never, .. })) | 678 | matches!(self, Ty::Never) |
803 | } | 679 | } |
804 | 680 | ||
805 | pub fn is_unknown(&self) -> bool { | 681 | pub fn is_unknown(&self) -> bool { |
806 | matches!(self, Ty::Unknown) | 682 | matches!(self, Ty::Unknown) |
807 | } | 683 | } |
808 | 684 | ||
685 | pub fn equals_ctor(&self, other: &Ty) -> bool { | ||
686 | match (self, other) { | ||
687 | (Ty::Adt(adt, ..), Ty::Adt(adt2, ..)) => adt == adt2, | ||
688 | (Ty::Slice(_), Ty::Slice(_)) | (Ty::Array(_), Ty::Array(_)) => true, | ||
689 | (Ty::FnDef(def_id, ..), Ty::FnDef(def_id2, ..)) => def_id == def_id2, | ||
690 | (Ty::OpaqueType(ty_id, ..), Ty::OpaqueType(ty_id2, ..)) => ty_id == ty_id2, | ||
691 | (Ty::AssociatedType(ty_id, ..), Ty::AssociatedType(ty_id2, ..)) | ||
692 | | (Ty::ForeignType(ty_id, ..), Ty::ForeignType(ty_id2, ..)) => ty_id == ty_id2, | ||
693 | (Ty::Closure(def, expr, _), Ty::Closure(def2, expr2, _)) => { | ||
694 | expr == expr2 && def == def2 | ||
695 | } | ||
696 | (Ty::Ref(mutability, ..), Ty::Ref(mutability2, ..)) | ||
697 | | (Ty::Raw(mutability, ..), Ty::Raw(mutability2, ..)) => mutability == mutability2, | ||
698 | ( | ||
699 | Ty::Function(FnPointer { num_args, sig, .. }), | ||
700 | Ty::Function(FnPointer { num_args: num_args2, sig: sig2, .. }), | ||
701 | ) => num_args == num_args2 && sig == sig2, | ||
702 | (Ty::Tuple(cardinality, _), Ty::Tuple(cardinality2, _)) => cardinality == cardinality2, | ||
703 | (Ty::Str, Ty::Str) | (Ty::Never, Ty::Never) => true, | ||
704 | (Ty::Scalar(scalar), Ty::Scalar(scalar2)) => scalar == scalar2, | ||
705 | _ => false, | ||
706 | } | ||
707 | } | ||
708 | |||
809 | /// If this is a `dyn Trait` type, this returns the `Trait` part. | 709 | /// If this is a `dyn Trait` type, this returns the `Trait` part. |
810 | pub fn dyn_trait_ref(&self) -> Option<&TraitRef> { | 710 | pub fn dyn_trait_ref(&self) -> Option<&TraitRef> { |
811 | match self { | 711 | match self { |
@@ -824,41 +724,30 @@ impl Ty { | |||
824 | 724 | ||
825 | fn builtin_deref(&self) -> Option<Ty> { | 725 | fn builtin_deref(&self) -> Option<Ty> { |
826 | match self { | 726 | match self { |
827 | Ty::Apply(a_ty) => match a_ty.ctor { | 727 | Ty::Ref(.., parameters) => Some(Ty::clone(parameters.as_single())), |
828 | TypeCtor::Ref(..) => Some(Ty::clone(a_ty.parameters.as_single())), | 728 | Ty::Raw(.., parameters) => Some(Ty::clone(parameters.as_single())), |
829 | TypeCtor::RawPtr(..) => Some(Ty::clone(a_ty.parameters.as_single())), | ||
830 | _ => None, | ||
831 | }, | ||
832 | _ => None, | 729 | _ => None, |
833 | } | 730 | } |
834 | } | 731 | } |
835 | 732 | ||
836 | pub fn as_fn_def(&self) -> Option<FunctionId> { | 733 | pub fn as_fn_def(&self) -> Option<FunctionId> { |
837 | match self { | 734 | match self { |
838 | &Ty::Apply(ApplicationTy { | 735 | &Ty::FnDef(CallableDefId::FunctionId(func), ..) => Some(func), |
839 | ctor: TypeCtor::FnDef(CallableDefId::FunctionId(func)), | ||
840 | .. | ||
841 | }) => Some(func), | ||
842 | _ => None, | 736 | _ => None, |
843 | } | 737 | } |
844 | } | 738 | } |
845 | 739 | ||
846 | pub fn callable_sig(&self, db: &dyn HirDatabase) -> Option<FnSig> { | 740 | pub fn callable_sig(&self, db: &dyn HirDatabase) -> Option<CallableSig> { |
847 | match self { | 741 | match self { |
848 | Ty::Apply(a_ty) => match a_ty.ctor { | 742 | Ty::Function(fn_ptr) => Some(CallableSig::from_fn_ptr(fn_ptr)), |
849 | TypeCtor::FnPtr { is_varargs, .. } => { | 743 | Ty::FnDef(def, parameters) => { |
850 | Some(FnSig::from_fn_ptr_substs(&a_ty.parameters, is_varargs)) | 744 | let sig = db.callable_item_signature(*def); |
851 | } | 745 | Some(sig.subst(¶meters)) |
852 | TypeCtor::FnDef(def) => { | 746 | } |
853 | let sig = db.callable_item_signature(def); | 747 | Ty::Closure(.., substs) => { |
854 | Some(sig.subst(&a_ty.parameters)) | 748 | let sig_param = &substs[0]; |
855 | } | 749 | sig_param.callable_sig(db) |
856 | TypeCtor::Closure { .. } => { | 750 | } |
857 | let sig_param = &a_ty.parameters[0]; | ||
858 | sig_param.callable_sig(db) | ||
859 | } | ||
860 | _ => None, | ||
861 | }, | ||
862 | _ => None, | 751 | _ => None, |
863 | } | 752 | } |
864 | } | 753 | } |
@@ -867,28 +756,66 @@ impl Ty { | |||
867 | /// the `Substs` for these type parameters with the given ones. (So e.g. if | 756 | /// the `Substs` for these type parameters with the given ones. (So e.g. if |
868 | /// `self` is `Option<_>` and the substs contain `u32`, we'll have | 757 | /// `self` is `Option<_>` and the substs contain `u32`, we'll have |
869 | /// `Option<u32>` afterwards.) | 758 | /// `Option<u32>` afterwards.) |
870 | pub fn apply_substs(self, substs: Substs) -> Ty { | 759 | pub fn apply_substs(mut self, new_substs: Substs) -> Ty { |
871 | match self { | 760 | match &mut self { |
872 | Ty::Apply(ApplicationTy { ctor, parameters: previous_substs }) => { | 761 | Ty::Adt(_, substs) |
873 | assert_eq!(previous_substs.len(), substs.len()); | 762 | | Ty::Slice(substs) |
874 | Ty::Apply(ApplicationTy { ctor, parameters: substs }) | 763 | | Ty::Array(substs) |
764 | | Ty::Raw(_, substs) | ||
765 | | Ty::Ref(_, substs) | ||
766 | | Ty::FnDef(_, substs) | ||
767 | | Ty::Function(FnPointer { substs, .. }) | ||
768 | | Ty::Tuple(_, substs) | ||
769 | | Ty::OpaqueType(_, substs) | ||
770 | | Ty::AssociatedType(_, substs) | ||
771 | | Ty::Closure(.., substs) => { | ||
772 | assert_eq!(substs.len(), new_substs.len()); | ||
773 | *substs = new_substs; | ||
875 | } | 774 | } |
876 | _ => self, | 775 | _ => (), |
877 | } | 776 | } |
777 | self | ||
878 | } | 778 | } |
879 | 779 | ||
880 | /// Returns the type parameters of this type if it has some (i.e. is an ADT | 780 | /// Returns the type parameters of this type if it has some (i.e. is an ADT |
881 | /// or function); so if `self` is `Option<u32>`, this returns the `u32`. | 781 | /// or function); so if `self` is `Option<u32>`, this returns the `u32`. |
882 | pub fn substs(&self) -> Option<Substs> { | 782 | pub fn substs(&self) -> Option<&Substs> { |
883 | match self { | 783 | match self { |
884 | Ty::Apply(ApplicationTy { parameters, .. }) => Some(parameters.clone()), | 784 | Ty::Adt(_, substs) |
785 | | Ty::Slice(substs) | ||
786 | | Ty::Array(substs) | ||
787 | | Ty::Raw(_, substs) | ||
788 | | Ty::Ref(_, substs) | ||
789 | | Ty::FnDef(_, substs) | ||
790 | | Ty::Function(FnPointer { substs, .. }) | ||
791 | | Ty::Tuple(_, substs) | ||
792 | | Ty::OpaqueType(_, substs) | ||
793 | | Ty::AssociatedType(_, substs) | ||
794 | | Ty::Closure(.., substs) => Some(substs), | ||
795 | _ => None, | ||
796 | } | ||
797 | } | ||
798 | |||
799 | pub fn substs_mut(&mut self) -> Option<&mut Substs> { | ||
800 | match self { | ||
801 | Ty::Adt(_, substs) | ||
802 | | Ty::Slice(substs) | ||
803 | | Ty::Array(substs) | ||
804 | | Ty::Raw(_, substs) | ||
805 | | Ty::Ref(_, substs) | ||
806 | | Ty::FnDef(_, substs) | ||
807 | | Ty::Function(FnPointer { substs, .. }) | ||
808 | | Ty::Tuple(_, substs) | ||
809 | | Ty::OpaqueType(_, substs) | ||
810 | | Ty::AssociatedType(_, substs) | ||
811 | | Ty::Closure(.., substs) => Some(substs), | ||
885 | _ => None, | 812 | _ => None, |
886 | } | 813 | } |
887 | } | 814 | } |
888 | 815 | ||
889 | pub fn impl_trait_bounds(&self, db: &dyn HirDatabase) -> Option<Vec<GenericPredicate>> { | 816 | pub fn impl_trait_bounds(&self, db: &dyn HirDatabase) -> Option<Vec<GenericPredicate>> { |
890 | match self { | 817 | match self { |
891 | Ty::Apply(ApplicationTy { ctor: TypeCtor::OpaqueType(opaque_ty_id), .. }) => { | 818 | Ty::OpaqueType(opaque_ty_id, ..) => { |
892 | match opaque_ty_id { | 819 | match opaque_ty_id { |
893 | OpaqueTyId::AsyncBlockTypeImplTrait(def, _expr) => { | 820 | OpaqueTyId::AsyncBlockTypeImplTrait(def, _expr) => { |
894 | let krate = def.module(db.upcast()).krate(); | 821 | let krate = def.module(db.upcast()).krate(); |
@@ -911,7 +838,7 @@ impl Ty { | |||
911 | OpaqueTyId::ReturnTypeImplTrait(..) => None, | 838 | OpaqueTyId::ReturnTypeImplTrait(..) => None, |
912 | } | 839 | } |
913 | } | 840 | } |
914 | Ty::Opaque(opaque_ty) => { | 841 | Ty::Alias(AliasTy::Opaque(opaque_ty)) => { |
915 | let predicates = match opaque_ty.opaque_ty_id { | 842 | let predicates = match opaque_ty.opaque_ty_id { |
916 | OpaqueTyId::ReturnTypeImplTrait(func, idx) => { | 843 | OpaqueTyId::ReturnTypeImplTrait(func, idx) => { |
917 | db.return_type_impl_traits(func).map(|it| { | 844 | db.return_type_impl_traits(func).map(|it| { |
@@ -949,13 +876,13 @@ impl Ty { | |||
949 | 876 | ||
950 | pub fn associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option<TraitId> { | 877 | pub fn associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option<TraitId> { |
951 | match self { | 878 | match self { |
952 | Ty::Apply(ApplicationTy { ctor: TypeCtor::AssociatedType(type_alias_id), .. }) => { | 879 | Ty::AssociatedType(type_alias_id, ..) => { |
953 | match type_alias_id.lookup(db.upcast()).container { | 880 | match type_alias_id.lookup(db.upcast()).container { |
954 | AssocContainerId::TraitId(trait_id) => Some(trait_id), | 881 | AssocContainerId::TraitId(trait_id) => Some(trait_id), |
955 | _ => None, | 882 | _ => None, |
956 | } | 883 | } |
957 | } | 884 | } |
958 | Ty::Projection(projection_ty) => { | 885 | Ty::Alias(AliasTy::Projection(projection_ty)) => { |
959 | match projection_ty.associated_ty.lookup(db.upcast()).container { | 886 | match projection_ty.associated_ty.lookup(db.upcast()).container { |
960 | AssocContainerId::TraitId(trait_id) => Some(trait_id), | 887 | AssocContainerId::TraitId(trait_id) => Some(trait_id), |
961 | _ => None, | 888 | _ => None, |
@@ -1033,7 +960,7 @@ pub trait TypeWalk { | |||
1033 | { | 960 | { |
1034 | self.walk_mut_binders( | 961 | self.walk_mut_binders( |
1035 | &mut |ty, binders| { | 962 | &mut |ty, binders| { |
1036 | if let &mut Ty::Bound(bound) = ty { | 963 | if let &mut Ty::BoundVar(bound) = ty { |
1037 | if bound.debruijn >= binders { | 964 | if bound.debruijn >= binders { |
1038 | *ty = substs.0[bound.index].clone().shift_bound_vars(binders); | 965 | *ty = substs.0[bound.index].clone().shift_bound_vars(binders); |
1039 | } | 966 | } |
@@ -1051,8 +978,8 @@ pub trait TypeWalk { | |||
1051 | { | 978 | { |
1052 | self.fold_binders( | 979 | self.fold_binders( |
1053 | &mut |ty, binders| match ty { | 980 | &mut |ty, binders| match ty { |
1054 | Ty::Bound(bound) if bound.debruijn >= binders => { | 981 | Ty::BoundVar(bound) if bound.debruijn >= binders => { |
1055 | Ty::Bound(bound.shifted_in_from(n)) | 982 | Ty::BoundVar(bound.shifted_in_from(n)) |
1056 | } | 983 | } |
1057 | ty => ty, | 984 | ty => ty, |
1058 | }, | 985 | }, |
@@ -1064,13 +991,13 @@ pub trait TypeWalk { | |||
1064 | impl TypeWalk for Ty { | 991 | impl TypeWalk for Ty { |
1065 | fn walk(&self, f: &mut impl FnMut(&Ty)) { | 992 | fn walk(&self, f: &mut impl FnMut(&Ty)) { |
1066 | match self { | 993 | match self { |
1067 | Ty::Apply(a_ty) => { | 994 | Ty::Alias(AliasTy::Projection(p_ty)) => { |
1068 | for t in a_ty.parameters.iter() { | 995 | for t in p_ty.parameters.iter() { |
1069 | t.walk(f); | 996 | t.walk(f); |
1070 | } | 997 | } |
1071 | } | 998 | } |
1072 | Ty::Projection(p_ty) => { | 999 | Ty::Alias(AliasTy::Opaque(o_ty)) => { |
1073 | for t in p_ty.parameters.iter() { | 1000 | for t in o_ty.parameters.iter() { |
1074 | t.walk(f); | 1001 | t.walk(f); |
1075 | } | 1002 | } |
1076 | } | 1003 | } |
@@ -1079,12 +1006,13 @@ impl TypeWalk for Ty { | |||
1079 | p.walk(f); | 1006 | p.walk(f); |
1080 | } | 1007 | } |
1081 | } | 1008 | } |
1082 | Ty::Opaque(o_ty) => { | 1009 | _ => { |
1083 | for t in o_ty.parameters.iter() { | 1010 | if let Some(substs) = self.substs() { |
1084 | t.walk(f); | 1011 | for t in substs.iter() { |
1012 | t.walk(f); | ||
1013 | } | ||
1085 | } | 1014 | } |
1086 | } | 1015 | } |
1087 | Ty::Placeholder { .. } | Ty::Bound(_) | Ty::Infer(_) | Ty::Unknown => {} | ||
1088 | } | 1016 | } |
1089 | f(self); | 1017 | f(self); |
1090 | } | 1018 | } |
@@ -1095,10 +1023,7 @@ impl TypeWalk for Ty { | |||
1095 | binders: DebruijnIndex, | 1023 | binders: DebruijnIndex, |
1096 | ) { | 1024 | ) { |
1097 | match self { | 1025 | match self { |
1098 | Ty::Apply(a_ty) => { | 1026 | Ty::Alias(AliasTy::Projection(p_ty)) => { |
1099 | a_ty.parameters.walk_mut_binders(f, binders); | ||
1100 | } | ||
1101 | Ty::Projection(p_ty) => { | ||
1102 | p_ty.parameters.walk_mut_binders(f, binders); | 1027 | p_ty.parameters.walk_mut_binders(f, binders); |
1103 | } | 1028 | } |
1104 | Ty::Dyn(predicates) => { | 1029 | Ty::Dyn(predicates) => { |
@@ -1106,10 +1031,14 @@ impl TypeWalk for Ty { | |||
1106 | p.walk_mut_binders(f, binders.shifted_in()); | 1031 | p.walk_mut_binders(f, binders.shifted_in()); |
1107 | } | 1032 | } |
1108 | } | 1033 | } |
1109 | Ty::Opaque(o_ty) => { | 1034 | Ty::Alias(AliasTy::Opaque(o_ty)) => { |
1110 | o_ty.parameters.walk_mut_binders(f, binders); | 1035 | o_ty.parameters.walk_mut_binders(f, binders); |
1111 | } | 1036 | } |
1112 | Ty::Placeholder { .. } | Ty::Bound(_) | Ty::Infer(_) | Ty::Unknown => {} | 1037 | _ => { |
1038 | if let Some(substs) = self.substs_mut() { | ||
1039 | substs.walk_mut_binders(f, binders); | ||
1040 | } | ||
1041 | } | ||
1113 | } | 1042 | } |
1114 | f(self, binders); | 1043 | f(self, binders); |
1115 | } | 1044 | } |
diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index 99b0ecf3b..1b5843d48 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs | |||
@@ -8,6 +8,7 @@ | |||
8 | use std::{iter, sync::Arc}; | 8 | use std::{iter, sync::Arc}; |
9 | 9 | ||
10 | use base_db::CrateId; | 10 | use base_db::CrateId; |
11 | use chalk_ir::Mutability; | ||
11 | use hir_def::{ | 12 | use hir_def::{ |
12 | adt::StructKind, | 13 | adt::StructKind, |
13 | builtin_type::BuiltinType, | 14 | builtin_type::BuiltinType, |
@@ -31,9 +32,9 @@ use crate::{ | |||
31 | all_super_trait_refs, associated_type_by_name_including_super_traits, generics, | 32 | all_super_trait_refs, associated_type_by_name_including_super_traits, generics, |
32 | make_mut_slice, variant_data, | 33 | make_mut_slice, variant_data, |
33 | }, | 34 | }, |
34 | Binders, BoundVar, DebruijnIndex, FnSig, GenericPredicate, OpaqueTy, OpaqueTyId, PolyFnSig, | 35 | AliasTy, Binders, BoundVar, CallableSig, DebruijnIndex, FnPointer, FnSig, GenericPredicate, |
35 | ProjectionPredicate, ProjectionTy, ReturnTypeImplTrait, ReturnTypeImplTraits, Substs, | 36 | OpaqueTy, OpaqueTyId, PolyFnSig, ProjectionPredicate, ProjectionTy, ReturnTypeImplTrait, |
36 | TraitEnvironment, TraitRef, Ty, TypeCtor, TypeWalk, | 37 | ReturnTypeImplTraits, Substs, TraitEnvironment, TraitRef, Ty, TypeWalk, |
37 | }; | 38 | }; |
38 | 39 | ||
39 | #[derive(Debug)] | 40 | #[derive(Debug)] |
@@ -145,13 +146,10 @@ impl Ty { | |||
145 | pub fn from_hir_ext(ctx: &TyLoweringContext<'_>, type_ref: &TypeRef) -> (Self, Option<TypeNs>) { | 146 | pub fn from_hir_ext(ctx: &TyLoweringContext<'_>, type_ref: &TypeRef) -> (Self, Option<TypeNs>) { |
146 | let mut res = None; | 147 | let mut res = None; |
147 | let ty = match type_ref { | 148 | let ty = match type_ref { |
148 | TypeRef::Never => Ty::simple(TypeCtor::Never), | 149 | TypeRef::Never => Ty::Never, |
149 | TypeRef::Tuple(inner) => { | 150 | TypeRef::Tuple(inner) => { |
150 | let inner_tys: Arc<[Ty]> = inner.iter().map(|tr| Ty::from_hir(ctx, tr)).collect(); | 151 | let inner_tys: Arc<[Ty]> = inner.iter().map(|tr| Ty::from_hir(ctx, tr)).collect(); |
151 | Ty::apply( | 152 | Ty::Tuple(inner_tys.len(), Substs(inner_tys)) |
152 | TypeCtor::Tuple { cardinality: inner_tys.len() as u16 }, | ||
153 | Substs(inner_tys), | ||
154 | ) | ||
155 | } | 153 | } |
156 | TypeRef::Path(path) => { | 154 | TypeRef::Path(path) => { |
157 | let (ty, res_) = Ty::from_hir_path(ctx, path); | 155 | let (ty, res_) = Ty::from_hir_path(ctx, path); |
@@ -160,30 +158,31 @@ impl Ty { | |||
160 | } | 158 | } |
161 | TypeRef::RawPtr(inner, mutability) => { | 159 | TypeRef::RawPtr(inner, mutability) => { |
162 | let inner_ty = Ty::from_hir(ctx, inner); | 160 | let inner_ty = Ty::from_hir(ctx, inner); |
163 | Ty::apply_one(TypeCtor::RawPtr(*mutability), inner_ty) | 161 | Ty::Raw(lower_to_chalk_mutability(*mutability), Substs::single(inner_ty)) |
164 | } | 162 | } |
165 | TypeRef::Array(inner) => { | 163 | TypeRef::Array(inner) => { |
166 | let inner_ty = Ty::from_hir(ctx, inner); | 164 | let inner_ty = Ty::from_hir(ctx, inner); |
167 | Ty::apply_one(TypeCtor::Array, inner_ty) | 165 | Ty::Array(Substs::single(inner_ty)) |
168 | } | 166 | } |
169 | TypeRef::Slice(inner) => { | 167 | TypeRef::Slice(inner) => { |
170 | let inner_ty = Ty::from_hir(ctx, inner); | 168 | let inner_ty = Ty::from_hir(ctx, inner); |
171 | Ty::apply_one(TypeCtor::Slice, inner_ty) | 169 | Ty::Slice(Substs::single(inner_ty)) |
172 | } | 170 | } |
173 | TypeRef::Reference(inner, _, mutability) => { | 171 | TypeRef::Reference(inner, _, mutability) => { |
174 | let inner_ty = Ty::from_hir(ctx, inner); | 172 | let inner_ty = Ty::from_hir(ctx, inner); |
175 | Ty::apply_one(TypeCtor::Ref(*mutability), inner_ty) | 173 | Ty::Ref(lower_to_chalk_mutability(*mutability), Substs::single(inner_ty)) |
176 | } | 174 | } |
177 | TypeRef::Placeholder => Ty::Unknown, | 175 | TypeRef::Placeholder => Ty::Unknown, |
178 | TypeRef::Fn(params, is_varargs) => { | 176 | TypeRef::Fn(params, is_varargs) => { |
179 | let sig = Substs(params.iter().map(|tr| Ty::from_hir(ctx, tr)).collect()); | 177 | let substs = Substs(params.iter().map(|tr| Ty::from_hir(ctx, tr)).collect()); |
180 | Ty::apply( | 178 | Ty::Function(FnPointer { |
181 | TypeCtor::FnPtr { num_args: sig.len() as u16 - 1, is_varargs: *is_varargs }, | 179 | num_args: substs.len() - 1, |
182 | sig, | 180 | sig: FnSig { variadic: *is_varargs }, |
183 | ) | 181 | substs, |
182 | }) | ||
184 | } | 183 | } |
185 | TypeRef::DynTrait(bounds) => { | 184 | TypeRef::DynTrait(bounds) => { |
186 | let self_ty = Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0)); | 185 | let self_ty = Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)); |
187 | let predicates = ctx.with_shifted_in(DebruijnIndex::ONE, |ctx| { | 186 | let predicates = ctx.with_shifted_in(DebruijnIndex::ONE, |ctx| { |
188 | bounds | 187 | bounds |
189 | .iter() | 188 | .iter() |
@@ -227,7 +226,10 @@ impl Ty { | |||
227 | let impl_trait_id = OpaqueTyId::ReturnTypeImplTrait(func, idx); | 226 | let impl_trait_id = OpaqueTyId::ReturnTypeImplTrait(func, idx); |
228 | let generics = generics(ctx.db.upcast(), func.into()); | 227 | let generics = generics(ctx.db.upcast(), func.into()); |
229 | let parameters = Substs::bound_vars(&generics, ctx.in_binders); | 228 | let parameters = Substs::bound_vars(&generics, ctx.in_binders); |
230 | Ty::Opaque(OpaqueTy { opaque_ty_id: impl_trait_id, parameters }) | 229 | Ty::Alias(AliasTy::Opaque(OpaqueTy { |
230 | opaque_ty_id: impl_trait_id, | ||
231 | parameters, | ||
232 | })) | ||
231 | } | 233 | } |
232 | ImplTraitLoweringMode::Param => { | 234 | ImplTraitLoweringMode::Param => { |
233 | let idx = ctx.impl_trait_counter.get(); | 235 | let idx = ctx.impl_trait_counter.get(); |
@@ -258,7 +260,7 @@ impl Ty { | |||
258 | } else { | 260 | } else { |
259 | (0, 0, 0, 0) | 261 | (0, 0, 0, 0) |
260 | }; | 262 | }; |
261 | Ty::Bound(BoundVar::new( | 263 | Ty::BoundVar(BoundVar::new( |
262 | ctx.in_binders, | 264 | ctx.in_binders, |
263 | idx as usize + parent_params + self_params + list_params, | 265 | idx as usize + parent_params + self_params + list_params, |
264 | )) | 266 | )) |
@@ -330,7 +332,7 @@ impl Ty { | |||
330 | TypeNs::TraitId(trait_) => { | 332 | TypeNs::TraitId(trait_) => { |
331 | // if this is a bare dyn Trait, we'll directly put the required ^0 for the self type in there | 333 | // if this is a bare dyn Trait, we'll directly put the required ^0 for the self type in there |
332 | let self_ty = if remaining_segments.len() == 0 { | 334 | let self_ty = if remaining_segments.len() == 0 { |
333 | Some(Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0))) | 335 | Some(Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0))) |
334 | } else { | 336 | } else { |
335 | None | 337 | None |
336 | }; | 338 | }; |
@@ -346,10 +348,10 @@ impl Ty { | |||
346 | match found { | 348 | match found { |
347 | Some((super_trait_ref, associated_ty)) => { | 349 | Some((super_trait_ref, associated_ty)) => { |
348 | // FIXME handle type parameters on the segment | 350 | // FIXME handle type parameters on the segment |
349 | Ty::Projection(ProjectionTy { | 351 | Ty::Alias(AliasTy::Projection(ProjectionTy { |
350 | associated_ty, | 352 | associated_ty, |
351 | parameters: super_trait_ref.substs, | 353 | parameters: super_trait_ref.substs, |
352 | }) | 354 | })) |
353 | } | 355 | } |
354 | None => { | 356 | None => { |
355 | // FIXME: report error (associated type not found) | 357 | // FIXME: report error (associated type not found) |
@@ -373,7 +375,7 @@ impl Ty { | |||
373 | TypeParamLoweringMode::Placeholder => Ty::Placeholder(param_id), | 375 | TypeParamLoweringMode::Placeholder => Ty::Placeholder(param_id), |
374 | TypeParamLoweringMode::Variable => { | 376 | TypeParamLoweringMode::Variable => { |
375 | let idx = generics.param_idx(param_id).expect("matching generics"); | 377 | let idx = generics.param_idx(param_id).expect("matching generics"); |
376 | Ty::Bound(BoundVar::new(ctx.in_binders, idx)) | 378 | Ty::BoundVar(BoundVar::new(ctx.in_binders, idx)) |
377 | } | 379 | } |
378 | } | 380 | } |
379 | } | 381 | } |
@@ -414,7 +416,6 @@ impl Ty { | |||
414 | // FIXME: report error | 416 | // FIXME: report error |
415 | TypeNs::EnumVariantId(_) => return (Ty::Unknown, None), | 417 | TypeNs::EnumVariantId(_) => return (Ty::Unknown, None), |
416 | }; | 418 | }; |
417 | |||
418 | Ty::from_type_relative_path(ctx, ty, Some(resolution), remaining_segments) | 419 | Ty::from_type_relative_path(ctx, ty, Some(resolution), remaining_segments) |
419 | } | 420 | } |
420 | 421 | ||
@@ -472,10 +473,10 @@ impl Ty { | |||
472 | // associated_type_shorthand_candidates does not do that | 473 | // associated_type_shorthand_candidates does not do that |
473 | let substs = substs.shift_bound_vars(ctx.in_binders); | 474 | let substs = substs.shift_bound_vars(ctx.in_binders); |
474 | // FIXME handle type parameters on the segment | 475 | // FIXME handle type parameters on the segment |
475 | return Some(Ty::Projection(ProjectionTy { | 476 | return Some(Ty::Alias(AliasTy::Projection(ProjectionTy { |
476 | associated_ty, | 477 | associated_ty, |
477 | parameters: substs, | 478 | parameters: substs, |
478 | })); | 479 | }))); |
479 | } | 480 | } |
480 | 481 | ||
481 | None | 482 | None |
@@ -676,7 +677,7 @@ impl GenericPredicate { | |||
676 | TypeParamLoweringMode::Placeholder => Ty::Placeholder(param_id), | 677 | TypeParamLoweringMode::Placeholder => Ty::Placeholder(param_id), |
677 | TypeParamLoweringMode::Variable => { | 678 | TypeParamLoweringMode::Variable => { |
678 | let idx = generics.param_idx(param_id).expect("matching generics"); | 679 | let idx = generics.param_idx(param_id).expect("matching generics"); |
679 | Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, idx)) | 680 | Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, idx)) |
680 | } | 681 | } |
681 | } | 682 | } |
682 | } | 683 | } |
@@ -750,7 +751,7 @@ fn assoc_type_bindings_from_type_bound<'a>( | |||
750 | preds.extend(GenericPredicate::from_type_bound( | 751 | preds.extend(GenericPredicate::from_type_bound( |
751 | ctx, | 752 | ctx, |
752 | bound, | 753 | bound, |
753 | Ty::Projection(projection_ty.clone()), | 754 | Ty::Alias(AliasTy::Projection(projection_ty.clone())), |
754 | )); | 755 | )); |
755 | } | 756 | } |
756 | preds | 757 | preds |
@@ -760,7 +761,7 @@ fn assoc_type_bindings_from_type_bound<'a>( | |||
760 | impl ReturnTypeImplTrait { | 761 | impl ReturnTypeImplTrait { |
761 | fn from_hir(ctx: &TyLoweringContext, bounds: &[TypeBound]) -> Self { | 762 | fn from_hir(ctx: &TyLoweringContext, bounds: &[TypeBound]) -> Self { |
762 | mark::hit!(lower_rpit); | 763 | mark::hit!(lower_rpit); |
763 | let self_ty = Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0)); | 764 | let self_ty = Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)); |
764 | let predicates = ctx.with_shifted_in(DebruijnIndex::ONE, |ctx| { | 765 | let predicates = ctx.with_shifted_in(DebruijnIndex::ONE, |ctx| { |
765 | bounds | 766 | bounds |
766 | .iter() | 767 | .iter() |
@@ -984,7 +985,7 @@ pub(crate) fn generic_defaults_query( | |||
984 | // Each default can only refer to previous parameters. | 985 | // Each default can only refer to previous parameters. |
985 | ty.walk_mut_binders( | 986 | ty.walk_mut_binders( |
986 | &mut |ty, binders| match ty { | 987 | &mut |ty, binders| match ty { |
987 | Ty::Bound(BoundVar { debruijn, index }) if *debruijn == binders => { | 988 | Ty::BoundVar(BoundVar { debruijn, index }) if *debruijn == binders => { |
988 | if *index >= idx { | 989 | if *index >= idx { |
989 | // type variable default referring to parameter coming | 990 | // type variable default referring to parameter coming |
990 | // after it. This is forbidden (FIXME: report | 991 | // after it. This is forbidden (FIXME: report |
@@ -1017,7 +1018,7 @@ fn fn_sig_for_fn(db: &dyn HirDatabase, def: FunctionId) -> PolyFnSig { | |||
1017 | let ret = Ty::from_hir(&ctx_ret, &data.ret_type); | 1018 | let ret = Ty::from_hir(&ctx_ret, &data.ret_type); |
1018 | let generics = generics(db.upcast(), def.into()); | 1019 | let generics = generics(db.upcast(), def.into()); |
1019 | let num_binders = generics.len(); | 1020 | let num_binders = generics.len(); |
1020 | Binders::new(num_binders, FnSig::from_params_and_return(params, ret, data.is_varargs)) | 1021 | Binders::new(num_binders, CallableSig::from_params_and_return(params, ret, data.is_varargs)) |
1021 | } | 1022 | } |
1022 | 1023 | ||
1023 | /// Build the declared type of a function. This should not need to look at the | 1024 | /// Build the declared type of a function. This should not need to look at the |
@@ -1025,7 +1026,7 @@ fn fn_sig_for_fn(db: &dyn HirDatabase, def: FunctionId) -> PolyFnSig { | |||
1025 | fn type_for_fn(db: &dyn HirDatabase, def: FunctionId) -> Binders<Ty> { | 1026 | fn type_for_fn(db: &dyn HirDatabase, def: FunctionId) -> Binders<Ty> { |
1026 | let generics = generics(db.upcast(), def.into()); | 1027 | let generics = generics(db.upcast(), def.into()); |
1027 | let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); | 1028 | let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); |
1028 | Binders::new(substs.len(), Ty::apply(TypeCtor::FnDef(def.into()), substs)) | 1029 | Binders::new(substs.len(), Ty::FnDef(def.into(), substs)) |
1029 | } | 1030 | } |
1030 | 1031 | ||
1031 | /// Build the declared type of a const. | 1032 | /// Build the declared type of a const. |
@@ -1057,7 +1058,7 @@ fn fn_sig_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> PolyFnS | |||
1057 | let params = | 1058 | let params = |
1058 | fields.iter().map(|(_, field)| Ty::from_hir(&ctx, &field.type_ref)).collect::<Vec<_>>(); | 1059 | fields.iter().map(|(_, field)| Ty::from_hir(&ctx, &field.type_ref)).collect::<Vec<_>>(); |
1059 | let ret = type_for_adt(db, def.into()); | 1060 | let ret = type_for_adt(db, def.into()); |
1060 | Binders::new(ret.num_binders, FnSig::from_params_and_return(params, ret.value, false)) | 1061 | Binders::new(ret.num_binders, CallableSig::from_params_and_return(params, ret.value, false)) |
1061 | } | 1062 | } |
1062 | 1063 | ||
1063 | /// Build the type of a tuple struct constructor. | 1064 | /// Build the type of a tuple struct constructor. |
@@ -1068,7 +1069,7 @@ fn type_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> Binders<T | |||
1068 | } | 1069 | } |
1069 | let generics = generics(db.upcast(), def.into()); | 1070 | let generics = generics(db.upcast(), def.into()); |
1070 | let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); | 1071 | let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); |
1071 | Binders::new(substs.len(), Ty::apply(TypeCtor::FnDef(def.into()), substs)) | 1072 | Binders::new(substs.len(), Ty::FnDef(def.into(), substs)) |
1072 | } | 1073 | } |
1073 | 1074 | ||
1074 | fn fn_sig_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) -> PolyFnSig { | 1075 | fn fn_sig_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) -> PolyFnSig { |
@@ -1081,7 +1082,7 @@ fn fn_sig_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) | |||
1081 | let params = | 1082 | let params = |
1082 | fields.iter().map(|(_, field)| Ty::from_hir(&ctx, &field.type_ref)).collect::<Vec<_>>(); | 1083 | fields.iter().map(|(_, field)| Ty::from_hir(&ctx, &field.type_ref)).collect::<Vec<_>>(); |
1083 | let ret = type_for_adt(db, def.parent.into()); | 1084 | let ret = type_for_adt(db, def.parent.into()); |
1084 | Binders::new(ret.num_binders, FnSig::from_params_and_return(params, ret.value, false)) | 1085 | Binders::new(ret.num_binders, CallableSig::from_params_and_return(params, ret.value, false)) |
1085 | } | 1086 | } |
1086 | 1087 | ||
1087 | /// Build the type of a tuple enum variant constructor. | 1088 | /// Build the type of a tuple enum variant constructor. |
@@ -1093,13 +1094,13 @@ fn type_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) - | |||
1093 | } | 1094 | } |
1094 | let generics = generics(db.upcast(), def.parent.into()); | 1095 | let generics = generics(db.upcast(), def.parent.into()); |
1095 | let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); | 1096 | let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); |
1096 | Binders::new(substs.len(), Ty::apply(TypeCtor::FnDef(def.into()), substs)) | 1097 | Binders::new(substs.len(), Ty::FnDef(def.into(), substs)) |
1097 | } | 1098 | } |
1098 | 1099 | ||
1099 | fn type_for_adt(db: &dyn HirDatabase, adt: AdtId) -> Binders<Ty> { | 1100 | fn type_for_adt(db: &dyn HirDatabase, adt: AdtId) -> Binders<Ty> { |
1100 | let generics = generics(db.upcast(), adt.into()); | 1101 | let generics = generics(db.upcast(), adt.into()); |
1101 | let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); | 1102 | let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); |
1102 | Binders::new(substs.len(), Ty::apply(TypeCtor::Adt(adt), substs)) | 1103 | Binders::new(substs.len(), Ty::Adt(adt, substs)) |
1103 | } | 1104 | } |
1104 | 1105 | ||
1105 | fn type_for_type_alias(db: &dyn HirDatabase, t: TypeAliasId) -> Binders<Ty> { | 1106 | fn type_for_type_alias(db: &dyn HirDatabase, t: TypeAliasId) -> Binders<Ty> { |
@@ -1107,10 +1108,10 @@ fn type_for_type_alias(db: &dyn HirDatabase, t: TypeAliasId) -> Binders<Ty> { | |||
1107 | let resolver = t.resolver(db.upcast()); | 1108 | let resolver = t.resolver(db.upcast()); |
1108 | let ctx = | 1109 | let ctx = |
1109 | TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); | 1110 | TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); |
1110 | let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); | ||
1111 | if db.type_alias_data(t).is_extern { | 1111 | if db.type_alias_data(t).is_extern { |
1112 | Binders::new(substs.len(), Ty::apply(TypeCtor::ForeignType(t), substs)) | 1112 | Binders::new(0, Ty::ForeignType(t)) |
1113 | } else { | 1113 | } else { |
1114 | let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); | ||
1114 | let type_ref = &db.type_alias_data(t).type_ref; | 1115 | let type_ref = &db.type_alias_data(t).type_ref; |
1115 | let inner = Ty::from_hir(&ctx, type_ref.as_ref().unwrap_or(&TypeRef::Error)); | 1116 | let inner = Ty::from_hir(&ctx, type_ref.as_ref().unwrap_or(&TypeRef::Error)); |
1116 | Binders::new(substs.len(), inner) | 1117 | Binders::new(substs.len(), inner) |
@@ -1259,3 +1260,10 @@ pub(crate) fn return_type_impl_traits( | |||
1259 | Some(Arc::new(Binders::new(num_binders, return_type_impl_traits))) | 1260 | Some(Arc::new(Binders::new(num_binders, return_type_impl_traits))) |
1260 | } | 1261 | } |
1261 | } | 1262 | } |
1263 | |||
1264 | pub(crate) fn lower_to_chalk_mutability(m: hir_def::type_ref::Mutability) -> Mutability { | ||
1265 | match m { | ||
1266 | hir_def::type_ref::Mutability::Shared => Mutability::Not, | ||
1267 | hir_def::type_ref::Mutability::Mut => Mutability::Mut, | ||
1268 | } | ||
1269 | } | ||
diff --git a/crates/hir_ty/src/method_resolution.rs b/crates/hir_ty/src/method_resolution.rs index b3d1fe9a4..f301a8477 100644 --- a/crates/hir_ty/src/method_resolution.rs +++ b/crates/hir_ty/src/method_resolution.rs | |||
@@ -6,12 +6,10 @@ use std::{iter, sync::Arc}; | |||
6 | 6 | ||
7 | use arrayvec::ArrayVec; | 7 | use arrayvec::ArrayVec; |
8 | use base_db::CrateId; | 8 | use base_db::CrateId; |
9 | use chalk_ir::Mutability; | ||
9 | use hir_def::{ | 10 | use hir_def::{ |
10 | builtin_type::{IntBitness, Signedness}, | 11 | lang_item::LangItemTarget, AdtId, AssocContainerId, AssocItemId, FunctionId, GenericDefId, |
11 | lang_item::LangItemTarget, | 12 | HasModule, ImplId, Lookup, ModuleId, TraitId, TypeAliasId, |
12 | type_ref::Mutability, | ||
13 | AssocContainerId, AssocItemId, FunctionId, GenericDefId, HasModule, ImplId, Lookup, ModuleId, | ||
14 | TraitId, | ||
15 | }; | 13 | }; |
16 | use hir_expand::name::Name; | 14 | use hir_expand::name::Name; |
17 | use rustc_hash::{FxHashMap, FxHashSet}; | 15 | use rustc_hash::{FxHashMap, FxHashSet}; |
@@ -19,17 +17,26 @@ use rustc_hash::{FxHashMap, FxHashSet}; | |||
19 | use crate::{ | 17 | use crate::{ |
20 | autoderef, | 18 | autoderef, |
21 | db::HirDatabase, | 19 | db::HirDatabase, |
22 | primitive::{FloatBitness, FloatTy, IntTy}, | 20 | primitive::{self, FloatTy, IntTy, UintTy}, |
23 | utils::all_super_traits, | 21 | utils::all_super_traits, |
24 | ApplicationTy, Canonical, DebruijnIndex, InEnvironment, Substs, TraitEnvironment, TraitRef, Ty, | 22 | Canonical, DebruijnIndex, FnPointer, FnSig, InEnvironment, Scalar, Substs, TraitEnvironment, |
25 | TyKind, TypeCtor, TypeWalk, | 23 | TraitRef, Ty, TypeWalk, |
26 | }; | 24 | }; |
27 | 25 | ||
28 | /// This is used as a key for indexing impls. | 26 | /// This is used as a key for indexing impls. |
29 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] | 27 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] |
30 | pub enum TyFingerprint { | 28 | pub enum TyFingerprint { |
31 | Apply(TypeCtor), | 29 | Str, |
30 | Slice, | ||
31 | Array, | ||
32 | Never, | ||
33 | RawPtr(Mutability), | ||
34 | Scalar(Scalar), | ||
35 | Adt(AdtId), | ||
32 | Dyn(TraitId), | 36 | Dyn(TraitId), |
37 | Tuple(usize), | ||
38 | ForeignType(TypeAliasId), | ||
39 | FnPtr(usize, FnSig), | ||
33 | } | 40 | } |
34 | 41 | ||
35 | impl TyFingerprint { | 42 | impl TyFingerprint { |
@@ -37,68 +44,42 @@ impl TyFingerprint { | |||
37 | /// have impls: if we have some `struct S`, we can have an `impl S`, but not | 44 | /// have impls: if we have some `struct S`, we can have an `impl S`, but not |
38 | /// `impl &S`. Hence, this will return `None` for reference types and such. | 45 | /// `impl &S`. Hence, this will return `None` for reference types and such. |
39 | pub(crate) fn for_impl(ty: &Ty) -> Option<TyFingerprint> { | 46 | pub(crate) fn for_impl(ty: &Ty) -> Option<TyFingerprint> { |
40 | match ty { | 47 | let fp = match ty { |
41 | Ty::Apply(a_ty) => Some(TyFingerprint::Apply(a_ty.ctor)), | 48 | &Ty::Str => TyFingerprint::Str, |
42 | Ty::Dyn(_) => ty.dyn_trait().map(|trait_| TyFingerprint::Dyn(trait_)), | 49 | &Ty::Never => TyFingerprint::Never, |
43 | _ => None, | 50 | &Ty::Slice(..) => TyFingerprint::Slice, |
44 | } | 51 | &Ty::Array(..) => TyFingerprint::Array, |
52 | &Ty::Scalar(scalar) => TyFingerprint::Scalar(scalar), | ||
53 | &Ty::Adt(adt, _) => TyFingerprint::Adt(adt), | ||
54 | &Ty::Tuple(cardinality, _) => TyFingerprint::Tuple(cardinality), | ||
55 | &Ty::Raw(mutability, ..) => TyFingerprint::RawPtr(mutability), | ||
56 | &Ty::ForeignType(alias_id, ..) => TyFingerprint::ForeignType(alias_id), | ||
57 | &Ty::Function(FnPointer { num_args, sig, .. }) => TyFingerprint::FnPtr(num_args, sig), | ||
58 | Ty::Dyn(_) => ty.dyn_trait().map(|trait_| TyFingerprint::Dyn(trait_))?, | ||
59 | _ => return None, | ||
60 | }; | ||
61 | Some(fp) | ||
45 | } | 62 | } |
46 | } | 63 | } |
47 | 64 | ||
48 | pub(crate) const ALL_INT_FPS: [TyFingerprint; 12] = [ | 65 | pub(crate) const ALL_INT_FPS: [TyFingerprint; 12] = [ |
49 | TyFingerprint::Apply(TypeCtor::Int(IntTy { | 66 | TyFingerprint::Scalar(Scalar::Int(IntTy::I8)), |
50 | signedness: Signedness::Unsigned, | 67 | TyFingerprint::Scalar(Scalar::Int(IntTy::I16)), |
51 | bitness: IntBitness::X8, | 68 | TyFingerprint::Scalar(Scalar::Int(IntTy::I32)), |
52 | })), | 69 | TyFingerprint::Scalar(Scalar::Int(IntTy::I64)), |
53 | TyFingerprint::Apply(TypeCtor::Int(IntTy { | 70 | TyFingerprint::Scalar(Scalar::Int(IntTy::I128)), |
54 | signedness: Signedness::Unsigned, | 71 | TyFingerprint::Scalar(Scalar::Int(IntTy::Isize)), |
55 | bitness: IntBitness::X16, | 72 | TyFingerprint::Scalar(Scalar::Uint(UintTy::U8)), |
56 | })), | 73 | TyFingerprint::Scalar(Scalar::Uint(UintTy::U16)), |
57 | TyFingerprint::Apply(TypeCtor::Int(IntTy { | 74 | TyFingerprint::Scalar(Scalar::Uint(UintTy::U32)), |
58 | signedness: Signedness::Unsigned, | 75 | TyFingerprint::Scalar(Scalar::Uint(UintTy::U64)), |
59 | bitness: IntBitness::X32, | 76 | TyFingerprint::Scalar(Scalar::Uint(UintTy::U128)), |
60 | })), | 77 | TyFingerprint::Scalar(Scalar::Uint(UintTy::Usize)), |
61 | TyFingerprint::Apply(TypeCtor::Int(IntTy { | ||
62 | signedness: Signedness::Unsigned, | ||
63 | bitness: IntBitness::X64, | ||
64 | })), | ||
65 | TyFingerprint::Apply(TypeCtor::Int(IntTy { | ||
66 | signedness: Signedness::Unsigned, | ||
67 | bitness: IntBitness::X128, | ||
68 | })), | ||
69 | TyFingerprint::Apply(TypeCtor::Int(IntTy { | ||
70 | signedness: Signedness::Unsigned, | ||
71 | bitness: IntBitness::Xsize, | ||
72 | })), | ||
73 | TyFingerprint::Apply(TypeCtor::Int(IntTy { | ||
74 | signedness: Signedness::Signed, | ||
75 | bitness: IntBitness::X8, | ||
76 | })), | ||
77 | TyFingerprint::Apply(TypeCtor::Int(IntTy { | ||
78 | signedness: Signedness::Signed, | ||
79 | bitness: IntBitness::X16, | ||
80 | })), | ||
81 | TyFingerprint::Apply(TypeCtor::Int(IntTy { | ||
82 | signedness: Signedness::Signed, | ||
83 | bitness: IntBitness::X32, | ||
84 | })), | ||
85 | TyFingerprint::Apply(TypeCtor::Int(IntTy { | ||
86 | signedness: Signedness::Signed, | ||
87 | bitness: IntBitness::X64, | ||
88 | })), | ||
89 | TyFingerprint::Apply(TypeCtor::Int(IntTy { | ||
90 | signedness: Signedness::Signed, | ||
91 | bitness: IntBitness::X128, | ||
92 | })), | ||
93 | TyFingerprint::Apply(TypeCtor::Int(IntTy { | ||
94 | signedness: Signedness::Signed, | ||
95 | bitness: IntBitness::Xsize, | ||
96 | })), | ||
97 | ]; | 78 | ]; |
98 | 79 | ||
99 | pub(crate) const ALL_FLOAT_FPS: [TyFingerprint; 2] = [ | 80 | pub(crate) const ALL_FLOAT_FPS: [TyFingerprint; 2] = [ |
100 | TyFingerprint::Apply(TypeCtor::Float(FloatTy { bitness: FloatBitness::X32 })), | 81 | TyFingerprint::Scalar(Scalar::Float(FloatTy::F32)), |
101 | TyFingerprint::Apply(TypeCtor::Float(FloatTy { bitness: FloatBitness::X64 })), | 82 | TyFingerprint::Scalar(Scalar::Float(FloatTy::F64)), |
102 | ]; | 83 | ]; |
103 | 84 | ||
104 | /// Trait impls defined or available in some crate. | 85 | /// Trait impls defined or available in some crate. |
@@ -250,27 +231,29 @@ impl Ty { | |||
250 | let mod_to_crate_ids = |module: ModuleId| Some(std::iter::once(module.krate()).collect()); | 231 | let mod_to_crate_ids = |module: ModuleId| Some(std::iter::once(module.krate()).collect()); |
251 | 232 | ||
252 | let lang_item_targets = match self { | 233 | let lang_item_targets = match self { |
253 | Ty::Apply(a_ty) => match a_ty.ctor { | 234 | Ty::Adt(def_id, _) => { |
254 | TypeCtor::Adt(def_id) => { | 235 | return mod_to_crate_ids(def_id.module(db.upcast())); |
255 | return mod_to_crate_ids(def_id.module(db.upcast())); | 236 | } |
256 | } | 237 | Ty::ForeignType(type_alias_id) => { |
257 | TypeCtor::ForeignType(type_alias_id) => { | 238 | return mod_to_crate_ids(type_alias_id.lookup(db.upcast()).module(db.upcast())); |
258 | return mod_to_crate_ids(type_alias_id.lookup(db.upcast()).module(db.upcast())); | 239 | } |
259 | } | 240 | Ty::Scalar(Scalar::Bool) => lang_item_crate!("bool"), |
260 | TypeCtor::Bool => lang_item_crate!("bool"), | 241 | Ty::Scalar(Scalar::Char) => lang_item_crate!("char"), |
261 | TypeCtor::Char => lang_item_crate!("char"), | 242 | Ty::Scalar(Scalar::Float(f)) => match f { |
262 | TypeCtor::Float(f) => match f.bitness { | 243 | // There are two lang items: one in libcore (fXX) and one in libstd (fXX_runtime) |
263 | // There are two lang items: one in libcore (fXX) and one in libstd (fXX_runtime) | 244 | FloatTy::F32 => lang_item_crate!("f32", "f32_runtime"), |
264 | FloatBitness::X32 => lang_item_crate!("f32", "f32_runtime"), | 245 | FloatTy::F64 => lang_item_crate!("f64", "f64_runtime"), |
265 | FloatBitness::X64 => lang_item_crate!("f64", "f64_runtime"), | ||
266 | }, | ||
267 | TypeCtor::Int(i) => lang_item_crate!(i.ty_to_string()), | ||
268 | TypeCtor::Str => lang_item_crate!("str_alloc", "str"), | ||
269 | TypeCtor::Slice => lang_item_crate!("slice_alloc", "slice"), | ||
270 | TypeCtor::RawPtr(Mutability::Shared) => lang_item_crate!("const_ptr"), | ||
271 | TypeCtor::RawPtr(Mutability::Mut) => lang_item_crate!("mut_ptr"), | ||
272 | _ => return None, | ||
273 | }, | 246 | }, |
247 | &Ty::Scalar(Scalar::Int(t)) => { | ||
248 | lang_item_crate!(primitive::int_ty_to_string(t)) | ||
249 | } | ||
250 | &Ty::Scalar(Scalar::Uint(t)) => { | ||
251 | lang_item_crate!(primitive::uint_ty_to_string(t)) | ||
252 | } | ||
253 | Ty::Str => lang_item_crate!("str_alloc", "str"), | ||
254 | Ty::Slice(_) => lang_item_crate!("slice_alloc", "slice"), | ||
255 | Ty::Raw(Mutability::Not, _) => lang_item_crate!("const_ptr"), | ||
256 | Ty::Raw(Mutability::Mut, _) => lang_item_crate!("mut_ptr"), | ||
274 | Ty::Dyn(_) => { | 257 | Ty::Dyn(_) => { |
275 | return self.dyn_trait().and_then(|trait_| { | 258 | return self.dyn_trait().and_then(|trait_| { |
276 | mod_to_crate_ids(GenericDefId::TraitId(trait_).module(db.upcast())) | 259 | mod_to_crate_ids(GenericDefId::TraitId(trait_).module(db.upcast())) |
@@ -447,7 +430,7 @@ fn iterate_method_candidates_with_autoref( | |||
447 | } | 430 | } |
448 | let refed = Canonical { | 431 | let refed = Canonical { |
449 | kinds: deref_chain[0].kinds.clone(), | 432 | kinds: deref_chain[0].kinds.clone(), |
450 | value: Ty::apply_one(TypeCtor::Ref(Mutability::Shared), deref_chain[0].value.clone()), | 433 | value: Ty::Ref(Mutability::Not, Substs::single(deref_chain[0].value.clone())), |
451 | }; | 434 | }; |
452 | if iterate_method_candidates_by_receiver( | 435 | if iterate_method_candidates_by_receiver( |
453 | &refed, | 436 | &refed, |
@@ -463,7 +446,7 @@ fn iterate_method_candidates_with_autoref( | |||
463 | } | 446 | } |
464 | let ref_muted = Canonical { | 447 | let ref_muted = Canonical { |
465 | kinds: deref_chain[0].kinds.clone(), | 448 | kinds: deref_chain[0].kinds.clone(), |
466 | value: Ty::apply_one(TypeCtor::Ref(Mutability::Mut), deref_chain[0].value.clone()), | 449 | value: Ty::Ref(Mutability::Mut, Substs::single(deref_chain[0].value.clone())), |
467 | }; | 450 | }; |
468 | if iterate_method_candidates_by_receiver( | 451 | if iterate_method_candidates_by_receiver( |
469 | &ref_muted, | 452 | &ref_muted, |
@@ -685,7 +668,7 @@ pub(crate) fn inherent_impl_substs( | |||
685 | .build(); | 668 | .build(); |
686 | let self_ty_with_vars = db.impl_self_ty(impl_id).subst(&vars); | 669 | let self_ty_with_vars = db.impl_self_ty(impl_id).subst(&vars); |
687 | let mut kinds = self_ty.kinds.to_vec(); | 670 | let mut kinds = self_ty.kinds.to_vec(); |
688 | kinds.extend(iter::repeat(TyKind::General).take(vars.len())); | 671 | kinds.extend(iter::repeat(chalk_ir::TyVariableKind::General).take(vars.len())); |
689 | let tys = Canonical { kinds: kinds.into(), value: (self_ty_with_vars, self_ty.value.clone()) }; | 672 | let tys = Canonical { kinds: kinds.into(), value: (self_ty_with_vars, self_ty.value.clone()) }; |
690 | let substs = super::infer::unify(&tys); | 673 | let substs = super::infer::unify(&tys); |
691 | // We only want the substs for the vars we added, not the ones from self_ty. | 674 | // We only want the substs for the vars we added, not the ones from self_ty. |
@@ -701,7 +684,7 @@ pub(crate) fn inherent_impl_substs( | |||
701 | fn fallback_bound_vars(s: Substs, num_vars_to_keep: usize) -> Substs { | 684 | fn fallback_bound_vars(s: Substs, num_vars_to_keep: usize) -> Substs { |
702 | s.fold_binders( | 685 | s.fold_binders( |
703 | &mut |ty, binders| { | 686 | &mut |ty, binders| { |
704 | if let Ty::Bound(bound) = &ty { | 687 | if let Ty::BoundVar(bound) = &ty { |
705 | if bound.index >= num_vars_to_keep && bound.debruijn >= binders { | 688 | if bound.index >= num_vars_to_keep && bound.debruijn >= binders { |
706 | Ty::Unknown | 689 | Ty::Unknown |
707 | } else { | 690 | } else { |
@@ -777,7 +760,7 @@ fn generic_implements_goal( | |||
777 | .push(self_ty.value) | 760 | .push(self_ty.value) |
778 | .fill_with_bound_vars(DebruijnIndex::INNERMOST, kinds.len()) | 761 | .fill_with_bound_vars(DebruijnIndex::INNERMOST, kinds.len()) |
779 | .build(); | 762 | .build(); |
780 | kinds.extend(iter::repeat(TyKind::General).take(substs.len() - 1)); | 763 | kinds.extend(iter::repeat(chalk_ir::TyVariableKind::General).take(substs.len() - 1)); |
781 | let trait_ref = TraitRef { trait_, substs }; | 764 | let trait_ref = TraitRef { trait_, substs }; |
782 | let obligation = super::Obligation::Trait(trait_ref); | 765 | let obligation = super::Obligation::Trait(trait_ref); |
783 | Canonical { kinds: kinds.into(), value: InEnvironment::new(env, obligation) } | 766 | Canonical { kinds: kinds.into(), value: InEnvironment::new(env, obligation) } |
@@ -790,11 +773,9 @@ fn autoderef_method_receiver( | |||
790 | ) -> Vec<Canonical<Ty>> { | 773 | ) -> Vec<Canonical<Ty>> { |
791 | let mut deref_chain: Vec<_> = autoderef::autoderef(db, Some(krate), ty).collect(); | 774 | let mut deref_chain: Vec<_> = autoderef::autoderef(db, Some(krate), ty).collect(); |
792 | // As a last step, we can do array unsizing (that's the only unsizing that rustc does for method receivers!) | 775 | // As a last step, we can do array unsizing (that's the only unsizing that rustc does for method receivers!) |
793 | if let Some(Ty::Apply(ApplicationTy { ctor: TypeCtor::Array, parameters })) = | 776 | if let Some(Ty::Array(parameters)) = deref_chain.last().map(|ty| &ty.value) { |
794 | deref_chain.last().map(|ty| &ty.value) | ||
795 | { | ||
796 | let kinds = deref_chain.last().unwrap().kinds.clone(); | 777 | let kinds = deref_chain.last().unwrap().kinds.clone(); |
797 | let unsized_ty = Ty::apply(TypeCtor::Slice, parameters.clone()); | 778 | let unsized_ty = Ty::Slice(parameters.clone()); |
798 | deref_chain.push(Canonical { value: unsized_ty, kinds }) | 779 | deref_chain.push(Canonical { value: unsized_ty, kinds }) |
799 | } | 780 | } |
800 | deref_chain | 781 | deref_chain |
diff --git a/crates/hir_ty/src/op.rs b/crates/hir_ty/src/op.rs index 0870874fc..bb9b8bbfc 100644 --- a/crates/hir_ty/src/op.rs +++ b/crates/hir_ty/src/op.rs | |||
@@ -1,27 +1,27 @@ | |||
1 | //! Helper functions for binary operator type inference. | 1 | //! Helper functions for binary operator type inference. |
2 | use chalk_ir::TyVariableKind; | ||
2 | use hir_def::expr::{ArithOp, BinaryOp, CmpOp}; | 3 | use hir_def::expr::{ArithOp, BinaryOp, CmpOp}; |
3 | 4 | ||
4 | use super::{InferTy, Ty, TypeCtor}; | 5 | use crate::{Scalar, Ty}; |
5 | use crate::ApplicationTy; | ||
6 | 6 | ||
7 | pub(super) fn binary_op_return_ty(op: BinaryOp, lhs_ty: Ty, rhs_ty: Ty) -> Ty { | 7 | pub(super) fn binary_op_return_ty(op: BinaryOp, lhs_ty: Ty, rhs_ty: Ty) -> Ty { |
8 | match op { | 8 | match op { |
9 | BinaryOp::LogicOp(_) | BinaryOp::CmpOp(_) => Ty::simple(TypeCtor::Bool), | 9 | BinaryOp::LogicOp(_) | BinaryOp::CmpOp(_) => Ty::Scalar(Scalar::Bool), |
10 | BinaryOp::Assignment { .. } => Ty::unit(), | 10 | BinaryOp::Assignment { .. } => Ty::unit(), |
11 | BinaryOp::ArithOp(ArithOp::Shl) | BinaryOp::ArithOp(ArithOp::Shr) => match lhs_ty { | 11 | BinaryOp::ArithOp(ArithOp::Shl) | BinaryOp::ArithOp(ArithOp::Shr) => match lhs_ty { |
12 | Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { | 12 | Ty::Scalar(Scalar::Int(_)) |
13 | TypeCtor::Int(..) | TypeCtor::Float(..) => lhs_ty, | 13 | | Ty::Scalar(Scalar::Uint(_)) |
14 | _ => Ty::Unknown, | 14 | | Ty::Scalar(Scalar::Float(_)) => lhs_ty, |
15 | }, | 15 | Ty::InferenceVar(_, TyVariableKind::Integer) |
16 | Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty, | 16 | | Ty::InferenceVar(_, TyVariableKind::Float) => lhs_ty, |
17 | _ => Ty::Unknown, | 17 | _ => Ty::Unknown, |
18 | }, | 18 | }, |
19 | BinaryOp::ArithOp(_) => match rhs_ty { | 19 | BinaryOp::ArithOp(_) => match rhs_ty { |
20 | Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { | 20 | Ty::Scalar(Scalar::Int(_)) |
21 | TypeCtor::Int(..) | TypeCtor::Float(..) => rhs_ty, | 21 | | Ty::Scalar(Scalar::Uint(_)) |
22 | _ => Ty::Unknown, | 22 | | Ty::Scalar(Scalar::Float(_)) => rhs_ty, |
23 | }, | 23 | Ty::InferenceVar(_, TyVariableKind::Integer) |
24 | Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => rhs_ty, | 24 | | Ty::InferenceVar(_, TyVariableKind::Float) => rhs_ty, |
25 | _ => Ty::Unknown, | 25 | _ => Ty::Unknown, |
26 | }, | 26 | }, |
27 | } | 27 | } |
@@ -29,29 +29,23 @@ pub(super) fn binary_op_return_ty(op: BinaryOp, lhs_ty: Ty, rhs_ty: Ty) -> Ty { | |||
29 | 29 | ||
30 | pub(super) fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty { | 30 | pub(super) fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty { |
31 | match op { | 31 | match op { |
32 | BinaryOp::LogicOp(..) => Ty::simple(TypeCtor::Bool), | 32 | BinaryOp::LogicOp(..) => Ty::Scalar(Scalar::Bool), |
33 | BinaryOp::Assignment { op: None } => lhs_ty, | 33 | BinaryOp::Assignment { op: None } => lhs_ty, |
34 | BinaryOp::CmpOp(CmpOp::Eq { .. }) => match lhs_ty { | 34 | BinaryOp::CmpOp(CmpOp::Eq { .. }) => match lhs_ty { |
35 | Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { | 35 | Ty::Scalar(_) | Ty::Str => lhs_ty, |
36 | TypeCtor::Int(..) | 36 | Ty::InferenceVar(_, TyVariableKind::Integer) |
37 | | TypeCtor::Float(..) | 37 | | Ty::InferenceVar(_, TyVariableKind::Float) => lhs_ty, |
38 | | TypeCtor::Str | ||
39 | | TypeCtor::Char | ||
40 | | TypeCtor::Bool => lhs_ty, | ||
41 | _ => Ty::Unknown, | ||
42 | }, | ||
43 | Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty, | ||
44 | _ => Ty::Unknown, | 38 | _ => Ty::Unknown, |
45 | }, | 39 | }, |
46 | BinaryOp::ArithOp(ArithOp::Shl) | BinaryOp::ArithOp(ArithOp::Shr) => Ty::Unknown, | 40 | BinaryOp::ArithOp(ArithOp::Shl) | BinaryOp::ArithOp(ArithOp::Shr) => Ty::Unknown, |
47 | BinaryOp::CmpOp(CmpOp::Ord { .. }) | 41 | BinaryOp::CmpOp(CmpOp::Ord { .. }) |
48 | | BinaryOp::Assignment { op: Some(_) } | 42 | | BinaryOp::Assignment { op: Some(_) } |
49 | | BinaryOp::ArithOp(_) => match lhs_ty { | 43 | | BinaryOp::ArithOp(_) => match lhs_ty { |
50 | Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { | 44 | Ty::Scalar(Scalar::Int(_)) |
51 | TypeCtor::Int(..) | TypeCtor::Float(..) => lhs_ty, | 45 | | Ty::Scalar(Scalar::Uint(_)) |
52 | _ => Ty::Unknown, | 46 | | Ty::Scalar(Scalar::Float(_)) => lhs_ty, |
53 | }, | 47 | Ty::InferenceVar(_, TyVariableKind::Integer) |
54 | Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty, | 48 | | Ty::InferenceVar(_, TyVariableKind::Float) => lhs_ty, |
55 | _ => Ty::Unknown, | 49 | _ => Ty::Unknown, |
56 | }, | 50 | }, |
57 | } | 51 | } |
diff --git a/crates/hir_ty/src/primitive.rs b/crates/hir_ty/src/primitive.rs index 37966b709..2449addfb 100644 --- a/crates/hir_ty/src/primitive.rs +++ b/crates/hir_ty/src/primitive.rs | |||
@@ -3,137 +3,63 @@ | |||
3 | //! * during type inference, they can be uncertain (ie, `let x = 92;`) | 3 | //! * during type inference, they can be uncertain (ie, `let x = 92;`) |
4 | //! * they don't belong to any particular crate. | 4 | //! * they don't belong to any particular crate. |
5 | 5 | ||
6 | use std::fmt; | 6 | pub use chalk_ir::{FloatTy, IntTy, UintTy}; |
7 | 7 | pub use hir_def::builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint}; | |
8 | pub use hir_def::builtin_type::{BuiltinFloat, BuiltinInt, FloatBitness, IntBitness, Signedness}; | 8 | |
9 | 9 | pub fn int_ty_to_string(ty: IntTy) -> &'static str { | |
10 | #[derive(Copy, Clone, Eq, PartialEq, Hash)] | 10 | match ty { |
11 | pub struct IntTy { | 11 | IntTy::Isize => "isize", |
12 | pub signedness: Signedness, | 12 | IntTy::I8 => "i8", |
13 | pub bitness: IntBitness, | 13 | IntTy::I16 => "i16", |
14 | } | 14 | IntTy::I32 => "i32", |
15 | 15 | IntTy::I64 => "i64", | |
16 | impl fmt::Debug for IntTy { | 16 | IntTy::I128 => "i128", |
17 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
18 | fmt::Display::fmt(self, f) | ||
19 | } | ||
20 | } | ||
21 | |||
22 | impl fmt::Display for IntTy { | ||
23 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
24 | write!(f, "{}", self.ty_to_string()) | ||
25 | } | ||
26 | } | ||
27 | |||
28 | impl IntTy { | ||
29 | pub fn isize() -> IntTy { | ||
30 | IntTy { signedness: Signedness::Signed, bitness: IntBitness::Xsize } | ||
31 | } | ||
32 | |||
33 | pub fn i8() -> IntTy { | ||
34 | IntTy { signedness: Signedness::Signed, bitness: IntBitness::X8 } | ||
35 | } | ||
36 | |||
37 | pub fn i16() -> IntTy { | ||
38 | IntTy { signedness: Signedness::Signed, bitness: IntBitness::X16 } | ||
39 | } | ||
40 | |||
41 | pub fn i32() -> IntTy { | ||
42 | IntTy { signedness: Signedness::Signed, bitness: IntBitness::X32 } | ||
43 | } | ||
44 | |||
45 | pub fn i64() -> IntTy { | ||
46 | IntTy { signedness: Signedness::Signed, bitness: IntBitness::X64 } | ||
47 | } | ||
48 | |||
49 | pub fn i128() -> IntTy { | ||
50 | IntTy { signedness: Signedness::Signed, bitness: IntBitness::X128 } | ||
51 | } | 17 | } |
52 | |||
53 | pub fn usize() -> IntTy { | ||
54 | IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::Xsize } | ||
55 | } | ||
56 | |||
57 | pub fn u8() -> IntTy { | ||
58 | IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X8 } | ||
59 | } | ||
60 | |||
61 | pub fn u16() -> IntTy { | ||
62 | IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X16 } | ||
63 | } | ||
64 | |||
65 | pub fn u32() -> IntTy { | ||
66 | IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X32 } | ||
67 | } | ||
68 | |||
69 | pub fn u64() -> IntTy { | ||
70 | IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X64 } | ||
71 | } | ||
72 | |||
73 | pub fn u128() -> IntTy { | ||
74 | IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X128 } | ||
75 | } | ||
76 | |||
77 | pub fn ty_to_string(self) -> &'static str { | ||
78 | match (self.signedness, self.bitness) { | ||
79 | (Signedness::Signed, IntBitness::Xsize) => "isize", | ||
80 | (Signedness::Signed, IntBitness::X8) => "i8", | ||
81 | (Signedness::Signed, IntBitness::X16) => "i16", | ||
82 | (Signedness::Signed, IntBitness::X32) => "i32", | ||
83 | (Signedness::Signed, IntBitness::X64) => "i64", | ||
84 | (Signedness::Signed, IntBitness::X128) => "i128", | ||
85 | (Signedness::Unsigned, IntBitness::Xsize) => "usize", | ||
86 | (Signedness::Unsigned, IntBitness::X8) => "u8", | ||
87 | (Signedness::Unsigned, IntBitness::X16) => "u16", | ||
88 | (Signedness::Unsigned, IntBitness::X32) => "u32", | ||
89 | (Signedness::Unsigned, IntBitness::X64) => "u64", | ||
90 | (Signedness::Unsigned, IntBitness::X128) => "u128", | ||
91 | } | ||
92 | } | ||
93 | } | ||
94 | |||
95 | #[derive(Copy, Clone, PartialEq, Eq, Hash)] | ||
96 | pub struct FloatTy { | ||
97 | pub bitness: FloatBitness, | ||
98 | } | 18 | } |
99 | 19 | ||
100 | impl fmt::Debug for FloatTy { | 20 | pub fn uint_ty_to_string(ty: UintTy) -> &'static str { |
101 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | 21 | match ty { |
102 | fmt::Display::fmt(self, f) | 22 | UintTy::Usize => "usize", |
23 | UintTy::U8 => "u8", | ||
24 | UintTy::U16 => "u16", | ||
25 | UintTy::U32 => "u32", | ||
26 | UintTy::U64 => "u64", | ||
27 | UintTy::U128 => "u128", | ||
103 | } | 28 | } |
104 | } | 29 | } |
105 | 30 | ||
106 | impl fmt::Display for FloatTy { | 31 | pub fn float_ty_to_string(ty: FloatTy) -> &'static str { |
107 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | 32 | match ty { |
108 | write!(f, "{}", self.ty_to_string()) | 33 | FloatTy::F32 => "f32", |
34 | FloatTy::F64 => "f64", | ||
109 | } | 35 | } |
110 | } | 36 | } |
111 | 37 | ||
112 | impl FloatTy { | 38 | pub(super) fn int_ty_from_builtin(t: BuiltinInt) -> IntTy { |
113 | pub fn f32() -> FloatTy { | 39 | match t { |
114 | FloatTy { bitness: FloatBitness::X32 } | 40 | BuiltinInt::Isize => IntTy::Isize, |
115 | } | 41 | BuiltinInt::I8 => IntTy::I8, |
116 | 42 | BuiltinInt::I16 => IntTy::I16, | |
117 | pub fn f64() -> FloatTy { | 43 | BuiltinInt::I32 => IntTy::I32, |
118 | FloatTy { bitness: FloatBitness::X64 } | 44 | BuiltinInt::I64 => IntTy::I64, |
119 | } | 45 | BuiltinInt::I128 => IntTy::I128, |
120 | |||
121 | pub fn ty_to_string(self) -> &'static str { | ||
122 | match self.bitness { | ||
123 | FloatBitness::X32 => "f32", | ||
124 | FloatBitness::X64 => "f64", | ||
125 | } | ||
126 | } | 46 | } |
127 | } | 47 | } |
128 | 48 | ||
129 | impl From<BuiltinInt> for IntTy { | 49 | pub(super) fn uint_ty_from_builtin(t: BuiltinUint) -> UintTy { |
130 | fn from(t: BuiltinInt) -> Self { | 50 | match t { |
131 | IntTy { signedness: t.signedness, bitness: t.bitness } | 51 | BuiltinUint::Usize => UintTy::Usize, |
52 | BuiltinUint::U8 => UintTy::U8, | ||
53 | BuiltinUint::U16 => UintTy::U16, | ||
54 | BuiltinUint::U32 => UintTy::U32, | ||
55 | BuiltinUint::U64 => UintTy::U64, | ||
56 | BuiltinUint::U128 => UintTy::U128, | ||
132 | } | 57 | } |
133 | } | 58 | } |
134 | 59 | ||
135 | impl From<BuiltinFloat> for FloatTy { | 60 | pub(super) fn float_ty_from_builtin(t: BuiltinFloat) -> FloatTy { |
136 | fn from(t: BuiltinFloat) -> Self { | 61 | match t { |
137 | FloatTy { bitness: t.bitness } | 62 | BuiltinFloat::F32 => FloatTy::F32, |
63 | BuiltinFloat::F64 => FloatTy::F64, | ||
138 | } | 64 | } |
139 | } | 65 | } |
diff --git a/crates/hir_ty/src/tests/simple.rs b/crates/hir_ty/src/tests/simple.rs index 12ec4657b..2947857a5 100644 --- a/crates/hir_ty/src/tests/simple.rs +++ b/crates/hir_ty/src/tests/simple.rs | |||
@@ -2491,3 +2491,58 @@ fn inner_use_enum_rename() { | |||
2491 | "#]], | 2491 | "#]], |
2492 | ) | 2492 | ) |
2493 | } | 2493 | } |
2494 | |||
2495 | #[test] | ||
2496 | fn box_into_vec() { | ||
2497 | check_infer( | ||
2498 | r#" | ||
2499 | #[lang = "sized"] | ||
2500 | pub trait Sized {} | ||
2501 | |||
2502 | #[lang = "unsize"] | ||
2503 | pub trait Unsize<T: ?Sized> {} | ||
2504 | |||
2505 | #[lang = "coerce_unsized"] | ||
2506 | pub trait CoerceUnsized<T> {} | ||
2507 | |||
2508 | pub unsafe trait Allocator {} | ||
2509 | |||
2510 | pub struct Global; | ||
2511 | unsafe impl Allocator for Global {} | ||
2512 | |||
2513 | #[lang = "owned_box"] | ||
2514 | #[fundamental] | ||
2515 | pub struct Box<T: ?Sized, A: Allocator = Global>; | ||
2516 | |||
2517 | impl<T: ?Sized + Unsize<U>, U: ?Sized, A: Allocator> CoerceUnsized<Box<U, A>> for Box<T, A> {} | ||
2518 | |||
2519 | pub struct Vec<T, A: Allocator = Global> {} | ||
2520 | |||
2521 | #[lang = "slice"] | ||
2522 | impl<T> [T] {} | ||
2523 | |||
2524 | #[lang = "slice_alloc"] | ||
2525 | impl<T> [T] { | ||
2526 | pub fn into_vec<A: Allocator>(self: Box<Self, A>) -> Vec<T, A> { | ||
2527 | unimplemented!() | ||
2528 | } | ||
2529 | } | ||
2530 | |||
2531 | fn test() { | ||
2532 | let vec = <[_]>::into_vec(box [1i32]); | ||
2533 | } | ||
2534 | "#, | ||
2535 | expect![[r#" | ||
2536 | 569..573 'self': Box<[T], A> | ||
2537 | 602..634 '{ ... }': Vec<T, A> | ||
2538 | 612..628 'unimpl...ted!()': Vec<T, A> | ||
2539 | 648..694 '{ ...2]); }': () | ||
2540 | 658..661 'vec': Vec<i32, Global> | ||
2541 | 664..679 '<[_]>::into_vec': fn into_vec<i32, Global>(Box<[i32], Global>) -> Vec<i32, Global> | ||
2542 | 664..691 '<[_]>:...1i32])': Vec<i32, Global> | ||
2543 | 680..690 'box [1i32]': Box<[i32; _], Global> | ||
2544 | 684..690 '[1i32]': [i32; _] | ||
2545 | 685..689 '1i32': i32 | ||
2546 | "#]], | ||
2547 | ) | ||
2548 | } | ||
diff --git a/crates/hir_ty/src/traits.rs b/crates/hir_ty/src/traits.rs index dfa51896b..e4cdb6d53 100644 --- a/crates/hir_ty/src/traits.rs +++ b/crates/hir_ty/src/traits.rs | |||
@@ -129,7 +129,7 @@ pub(crate) fn trait_solve_query( | |||
129 | log::info!("trait_solve_query({})", goal.value.value.display(db)); | 129 | log::info!("trait_solve_query({})", goal.value.value.display(db)); |
130 | 130 | ||
131 | if let Obligation::Projection(pred) = &goal.value.value { | 131 | if let Obligation::Projection(pred) = &goal.value.value { |
132 | if let Ty::Bound(_) = &pred.projection_ty.parameters[0] { | 132 | if let Ty::BoundVar(_) = &pred.projection_ty.parameters[0] { |
133 | // Hack: don't ask Chalk to normalize with an unknown self type, it'll say that's impossible | 133 | // Hack: don't ask Chalk to normalize with an unknown self type, it'll say that's impossible |
134 | return Some(Solution::Ambig(Guidance::Unknown)); | 134 | return Some(Solution::Ambig(Guidance::Unknown)); |
135 | } | 135 | } |
diff --git a/crates/hir_ty/src/traits/chalk.rs b/crates/hir_ty/src/traits/chalk.rs index d74c83737..e513fa8f4 100644 --- a/crates/hir_ty/src/traits/chalk.rs +++ b/crates/hir_ty/src/traits/chalk.rs | |||
@@ -19,8 +19,8 @@ use crate::{ | |||
19 | display::HirDisplay, | 19 | display::HirDisplay, |
20 | method_resolution::{TyFingerprint, ALL_FLOAT_FPS, ALL_INT_FPS}, | 20 | method_resolution::{TyFingerprint, ALL_FLOAT_FPS, ALL_INT_FPS}, |
21 | utils::generics, | 21 | utils::generics, |
22 | BoundVar, CallableDefId, DebruijnIndex, FnSig, GenericPredicate, ProjectionPredicate, | 22 | BoundVar, CallableDefId, CallableSig, DebruijnIndex, GenericPredicate, ProjectionPredicate, |
23 | ProjectionTy, Substs, TraitRef, Ty, TypeCtor, | 23 | ProjectionTy, Substs, TraitRef, Ty, |
24 | }; | 24 | }; |
25 | use mapping::{ | 25 | use mapping::{ |
26 | convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsAssocType, | 26 | convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsAssocType, |
@@ -90,7 +90,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
90 | ty: &Ty, | 90 | ty: &Ty, |
91 | binders: &CanonicalVarKinds<Interner>, | 91 | binders: &CanonicalVarKinds<Interner>, |
92 | ) -> Option<chalk_ir::TyVariableKind> { | 92 | ) -> Option<chalk_ir::TyVariableKind> { |
93 | if let Ty::Bound(bv) = ty { | 93 | if let Ty::BoundVar(bv) = ty { |
94 | let binders = binders.as_slice(&Interner); | 94 | let binders = binders.as_slice(&Interner); |
95 | if bv.debruijn == DebruijnIndex::INNERMOST { | 95 | if bv.debruijn == DebruijnIndex::INNERMOST { |
96 | if let chalk_ir::VariableKind::Ty(tk) = binders[bv.index].kind { | 96 | if let chalk_ir::VariableKind::Ty(tk) = binders[bv.index].kind { |
@@ -220,18 +220,18 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
220 | let impl_bound = GenericPredicate::Implemented(TraitRef { | 220 | let impl_bound = GenericPredicate::Implemented(TraitRef { |
221 | trait_: future_trait, | 221 | trait_: future_trait, |
222 | // Self type as the first parameter. | 222 | // Self type as the first parameter. |
223 | substs: Substs::single(Ty::Bound(BoundVar { | 223 | substs: Substs::single(Ty::BoundVar(BoundVar { |
224 | debruijn: DebruijnIndex::INNERMOST, | 224 | debruijn: DebruijnIndex::INNERMOST, |
225 | index: 0, | 225 | index: 0, |
226 | })), | 226 | })), |
227 | }); | 227 | }); |
228 | let proj_bound = GenericPredicate::Projection(ProjectionPredicate { | 228 | let proj_bound = GenericPredicate::Projection(ProjectionPredicate { |
229 | // The parameter of the opaque type. | 229 | // The parameter of the opaque type. |
230 | ty: Ty::Bound(BoundVar { debruijn: DebruijnIndex::ONE, index: 0 }), | 230 | ty: Ty::BoundVar(BoundVar { debruijn: DebruijnIndex::ONE, index: 0 }), |
231 | projection_ty: ProjectionTy { | 231 | projection_ty: ProjectionTy { |
232 | associated_ty: future_output, | 232 | associated_ty: future_output, |
233 | // Self type as the first parameter. | 233 | // Self type as the first parameter. |
234 | parameters: Substs::single(Ty::Bound(BoundVar::new( | 234 | parameters: Substs::single(Ty::BoundVar(BoundVar::new( |
235 | DebruijnIndex::INNERMOST, | 235 | DebruijnIndex::INNERMOST, |
236 | 0, | 236 | 0, |
237 | ))), | 237 | ))), |
@@ -286,9 +286,8 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
286 | ) -> chalk_ir::Binders<rust_ir::FnDefInputsAndOutputDatum<Interner>> { | 286 | ) -> chalk_ir::Binders<rust_ir::FnDefInputsAndOutputDatum<Interner>> { |
287 | let sig_ty: Ty = | 287 | let sig_ty: Ty = |
288 | from_chalk(self.db, substs.at(&Interner, 0).assert_ty_ref(&Interner).clone()); | 288 | from_chalk(self.db, substs.at(&Interner, 0).assert_ty_ref(&Interner).clone()); |
289 | let sig = FnSig::from_fn_ptr_substs( | 289 | let sig = CallableSig::from_substs( |
290 | &sig_ty.substs().expect("first closure param should be fn ptr"), | 290 | &sig_ty.substs().expect("first closure param should be fn ptr"), |
291 | false, | ||
292 | ); | 291 | ); |
293 | let io = rust_ir::FnDefInputsAndOutputDatum { | 292 | let io = rust_ir::FnDefInputsAndOutputDatum { |
294 | argument_types: sig.params().iter().map(|ty| ty.clone().to_chalk(self.db)).collect(), | 293 | argument_types: sig.params().iter().map(|ty| ty.clone().to_chalk(self.db)).collect(), |
@@ -393,7 +392,7 @@ pub(crate) fn associated_ty_data_query( | |||
393 | let resolver = hir_def::resolver::HasResolver::resolver(type_alias, db.upcast()); | 392 | let resolver = hir_def::resolver::HasResolver::resolver(type_alias, db.upcast()); |
394 | let ctx = crate::TyLoweringContext::new(db, &resolver) | 393 | let ctx = crate::TyLoweringContext::new(db, &resolver) |
395 | .with_type_param_mode(crate::lower::TypeParamLoweringMode::Variable); | 394 | .with_type_param_mode(crate::lower::TypeParamLoweringMode::Variable); |
396 | let self_ty = Ty::Bound(crate::BoundVar::new(crate::DebruijnIndex::INNERMOST, 0)); | 395 | let self_ty = Ty::BoundVar(crate::BoundVar::new(crate::DebruijnIndex::INNERMOST, 0)); |
397 | let bounds = type_alias_data | 396 | let bounds = type_alias_data |
398 | .bounds | 397 | .bounds |
399 | .iter() | 398 | .iter() |
@@ -489,10 +488,11 @@ pub(crate) fn struct_datum_query( | |||
489 | struct_id: AdtId, | 488 | struct_id: AdtId, |
490 | ) -> Arc<StructDatum> { | 489 | ) -> Arc<StructDatum> { |
491 | debug!("struct_datum {:?}", struct_id); | 490 | debug!("struct_datum {:?}", struct_id); |
492 | let type_ctor = TypeCtor::Adt(from_chalk(db, struct_id)); | 491 | let adt_id = from_chalk(db, struct_id); |
492 | let type_ctor = Ty::Adt(adt_id, Substs::empty()); | ||
493 | debug!("struct {:?} = {:?}", struct_id, type_ctor); | 493 | debug!("struct {:?} = {:?}", struct_id, type_ctor); |
494 | let num_params = type_ctor.num_ty_params(db); | 494 | let num_params = generics(db.upcast(), adt_id.into()).len(); |
495 | let upstream = type_ctor.krate(db) != Some(krate); | 495 | let upstream = adt_id.module(db.upcast()).krate() != krate; |
496 | let where_clauses = type_ctor | 496 | let where_clauses = type_ctor |
497 | .as_generic_def() | 497 | .as_generic_def() |
498 | .map(|generic_def| { | 498 | .map(|generic_def| { |
diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index 8700d664e..db1760e6c 100644 --- a/crates/hir_ty/src/traits/chalk/mapping.rs +++ b/crates/hir_ty/src/traits/chalk/mapping.rs | |||
@@ -4,20 +4,20 @@ | |||
4 | //! conversions. | 4 | //! conversions. |
5 | 5 | ||
6 | use chalk_ir::{ | 6 | use chalk_ir::{ |
7 | cast::Cast, fold::shift::Shift, interner::HasInterner, LifetimeData, PlaceholderIndex, Scalar, | 7 | cast::Cast, fold::shift::Shift, interner::HasInterner, LifetimeData, PlaceholderIndex, |
8 | UniverseIndex, | 8 | UniverseIndex, |
9 | }; | 9 | }; |
10 | use chalk_solve::rust_ir; | 10 | use chalk_solve::rust_ir; |
11 | 11 | ||
12 | use base_db::salsa::InternKey; | 12 | use base_db::salsa::InternKey; |
13 | use hir_def::{type_ref::Mutability, AssocContainerId, GenericDefId, Lookup, TypeAliasId}; | 13 | use hir_def::{AssocContainerId, GenericDefId, Lookup, TypeAliasId}; |
14 | 14 | ||
15 | use crate::{ | 15 | use crate::{ |
16 | db::HirDatabase, | 16 | db::HirDatabase, |
17 | primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness}, | 17 | primitive::UintTy, |
18 | traits::{Canonical, Obligation}, | 18 | traits::{Canonical, Obligation}, |
19 | ApplicationTy, CallableDefId, GenericPredicate, InEnvironment, OpaqueTy, OpaqueTyId, | 19 | AliasTy, CallableDefId, FnPointer, FnSig, GenericPredicate, InEnvironment, OpaqueTy, |
20 | ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TyKind, TypeCtor, | 20 | OpaqueTyId, ProjectionPredicate, ProjectionTy, Scalar, Substs, TraitEnvironment, TraitRef, Ty, |
21 | }; | 21 | }; |
22 | 22 | ||
23 | use super::interner::*; | 23 | use super::interner::*; |
@@ -27,88 +27,68 @@ impl ToChalk for Ty { | |||
27 | type Chalk = chalk_ir::Ty<Interner>; | 27 | type Chalk = chalk_ir::Ty<Interner>; |
28 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Ty<Interner> { | 28 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Ty<Interner> { |
29 | match self { | 29 | match self { |
30 | Ty::Apply(apply_ty) => match apply_ty.ctor { | 30 | Ty::Ref(m, parameters) => ref_to_chalk(db, m, parameters), |
31 | TypeCtor::Ref(m) => ref_to_chalk(db, m, apply_ty.parameters), | 31 | Ty::Array(parameters) => array_to_chalk(db, parameters), |
32 | TypeCtor::Array => array_to_chalk(db, apply_ty.parameters), | 32 | Ty::Function(FnPointer { sig: FnSig { variadic }, substs, .. }) => { |
33 | TypeCtor::FnPtr { num_args: _, is_varargs } => { | 33 | let substitution = chalk_ir::FnSubst(substs.to_chalk(db).shifted_in(&Interner)); |
34 | let substitution = | 34 | chalk_ir::TyKind::Function(chalk_ir::FnPointer { |
35 | chalk_ir::FnSubst(apply_ty.parameters.to_chalk(db).shifted_in(&Interner)); | 35 | num_binders: 0, |
36 | chalk_ir::TyKind::Function(chalk_ir::FnPointer { | 36 | sig: chalk_ir::FnSig { abi: (), safety: chalk_ir::Safety::Safe, variadic }, |
37 | num_binders: 0, | 37 | substitution, |
38 | sig: chalk_ir::FnSig { | 38 | }) |
39 | abi: (), | 39 | .intern(&Interner) |
40 | safety: chalk_ir::Safety::Safe, | 40 | } |
41 | variadic: is_varargs, | 41 | Ty::AssociatedType(type_alias, substs) => { |
42 | }, | 42 | let assoc_type = TypeAliasAsAssocType(type_alias); |
43 | substitution, | 43 | let assoc_type_id = assoc_type.to_chalk(db); |
44 | }) | 44 | let substitution = substs.to_chalk(db); |
45 | .intern(&Interner) | 45 | chalk_ir::TyKind::AssociatedType(assoc_type_id, substitution).intern(&Interner) |
46 | } | 46 | } |
47 | TypeCtor::AssociatedType(type_alias) => { | ||
48 | let assoc_type = TypeAliasAsAssocType(type_alias); | ||
49 | let assoc_type_id = assoc_type.to_chalk(db); | ||
50 | let substitution = apply_ty.parameters.to_chalk(db); | ||
51 | chalk_ir::TyKind::AssociatedType(assoc_type_id, substitution).intern(&Interner) | ||
52 | } | ||
53 | 47 | ||
54 | TypeCtor::OpaqueType(impl_trait_id) => { | 48 | Ty::OpaqueType(impl_trait_id, substs) => { |
55 | let id = impl_trait_id.to_chalk(db); | 49 | let id = impl_trait_id.to_chalk(db); |
56 | let substitution = apply_ty.parameters.to_chalk(db); | 50 | let substitution = substs.to_chalk(db); |
57 | chalk_ir::TyKind::OpaqueType(id, substitution).intern(&Interner) | 51 | chalk_ir::TyKind::OpaqueType(id, substitution).intern(&Interner) |
58 | } | 52 | } |
59 | 53 | ||
60 | TypeCtor::ForeignType(type_alias) => { | 54 | Ty::ForeignType(type_alias) => { |
61 | let foreign_type = TypeAliasAsForeignType(type_alias); | 55 | let foreign_type = TypeAliasAsForeignType(type_alias); |
62 | let foreign_type_id = foreign_type.to_chalk(db); | 56 | let foreign_type_id = foreign_type.to_chalk(db); |
63 | chalk_ir::TyKind::Foreign(foreign_type_id).intern(&Interner) | 57 | chalk_ir::TyKind::Foreign(foreign_type_id).intern(&Interner) |
64 | } | 58 | } |
65 | 59 | ||
66 | TypeCtor::Bool => chalk_ir::TyKind::Scalar(Scalar::Bool).intern(&Interner), | 60 | Ty::Scalar(scalar) => chalk_ir::TyKind::Scalar(scalar).intern(&Interner), |
67 | TypeCtor::Char => chalk_ir::TyKind::Scalar(Scalar::Char).intern(&Interner), | ||
68 | TypeCtor::Int(int_ty) => { | ||
69 | chalk_ir::TyKind::Scalar(int_ty_to_chalk(int_ty)).intern(&Interner) | ||
70 | } | ||
71 | TypeCtor::Float(FloatTy { bitness: FloatBitness::X32 }) => { | ||
72 | chalk_ir::TyKind::Scalar(Scalar::Float(chalk_ir::FloatTy::F32)) | ||
73 | .intern(&Interner) | ||
74 | } | ||
75 | TypeCtor::Float(FloatTy { bitness: FloatBitness::X64 }) => { | ||
76 | chalk_ir::TyKind::Scalar(Scalar::Float(chalk_ir::FloatTy::F64)) | ||
77 | .intern(&Interner) | ||
78 | } | ||
79 | 61 | ||
80 | TypeCtor::Tuple { cardinality } => { | 62 | Ty::Tuple(cardinality, substs) => { |
81 | let substitution = apply_ty.parameters.to_chalk(db); | 63 | let substitution = substs.to_chalk(db); |
82 | chalk_ir::TyKind::Tuple(cardinality.into(), substitution).intern(&Interner) | 64 | chalk_ir::TyKind::Tuple(cardinality.into(), substitution).intern(&Interner) |
83 | } | 65 | } |
84 | TypeCtor::RawPtr(mutability) => { | 66 | Ty::Raw(mutability, substs) => { |
85 | let ty = apply_ty.parameters[0].clone().to_chalk(db); | 67 | let ty = substs[0].clone().to_chalk(db); |
86 | chalk_ir::TyKind::Raw(mutability.to_chalk(db), ty).intern(&Interner) | 68 | chalk_ir::TyKind::Raw(mutability, ty).intern(&Interner) |
87 | } | 69 | } |
88 | TypeCtor::Slice => { | 70 | Ty::Slice(substs) => { |
89 | chalk_ir::TyKind::Slice(apply_ty.parameters[0].clone().to_chalk(db)) | 71 | chalk_ir::TyKind::Slice(substs[0].clone().to_chalk(db)).intern(&Interner) |
90 | .intern(&Interner) | 72 | } |
91 | } | 73 | Ty::Str => chalk_ir::TyKind::Str.intern(&Interner), |
92 | TypeCtor::Str => chalk_ir::TyKind::Str.intern(&Interner), | 74 | Ty::FnDef(callable_def, substs) => { |
93 | TypeCtor::FnDef(callable_def) => { | 75 | let id = callable_def.to_chalk(db); |
94 | let id = callable_def.to_chalk(db); | 76 | let substitution = substs.to_chalk(db); |
95 | let substitution = apply_ty.parameters.to_chalk(db); | 77 | chalk_ir::TyKind::FnDef(id, substitution).intern(&Interner) |
96 | chalk_ir::TyKind::FnDef(id, substitution).intern(&Interner) | 78 | } |
97 | } | 79 | Ty::Never => chalk_ir::TyKind::Never.intern(&Interner), |
98 | TypeCtor::Never => chalk_ir::TyKind::Never.intern(&Interner), | ||
99 | 80 | ||
100 | TypeCtor::Closure { def, expr } => { | 81 | Ty::Closure(def, expr, substs) => { |
101 | let closure_id = db.intern_closure((def, expr)); | 82 | let closure_id = db.intern_closure((def, expr)); |
102 | let substitution = apply_ty.parameters.to_chalk(db); | 83 | let substitution = substs.to_chalk(db); |
103 | chalk_ir::TyKind::Closure(closure_id.into(), substitution).intern(&Interner) | 84 | chalk_ir::TyKind::Closure(closure_id.into(), substitution).intern(&Interner) |
104 | } | 85 | } |
105 | 86 | ||
106 | TypeCtor::Adt(adt_id) => { | 87 | Ty::Adt(adt_id, substs) => { |
107 | let substitution = apply_ty.parameters.to_chalk(db); | 88 | let substitution = substs.to_chalk(db); |
108 | chalk_ir::TyKind::Adt(chalk_ir::AdtId(adt_id), substitution).intern(&Interner) | 89 | chalk_ir::TyKind::Adt(chalk_ir::AdtId(adt_id), substitution).intern(&Interner) |
109 | } | 90 | } |
110 | }, | 91 | Ty::Alias(AliasTy::Projection(proj_ty)) => { |
111 | Ty::Projection(proj_ty) => { | ||
112 | let associated_ty_id = TypeAliasAsAssocType(proj_ty.associated_ty).to_chalk(db); | 92 | let associated_ty_id = TypeAliasAsAssocType(proj_ty.associated_ty).to_chalk(db); |
113 | let substitution = proj_ty.parameters.to_chalk(db); | 93 | let substitution = proj_ty.parameters.to_chalk(db); |
114 | chalk_ir::AliasTy::Projection(chalk_ir::ProjectionTy { | 94 | chalk_ir::AliasTy::Projection(chalk_ir::ProjectionTy { |
@@ -126,8 +106,8 @@ impl ToChalk for Ty { | |||
126 | } | 106 | } |
127 | .to_ty::<Interner>(&Interner) | 107 | .to_ty::<Interner>(&Interner) |
128 | } | 108 | } |
129 | Ty::Bound(idx) => chalk_ir::TyKind::BoundVar(idx).intern(&Interner), | 109 | Ty::BoundVar(idx) => chalk_ir::TyKind::BoundVar(idx).intern(&Interner), |
130 | Ty::Infer(_infer_ty) => panic!("uncanonicalized infer ty"), | 110 | Ty::InferenceVar(..) => panic!("uncanonicalized infer ty"), |
131 | Ty::Dyn(predicates) => { | 111 | Ty::Dyn(predicates) => { |
132 | let where_clauses = chalk_ir::QuantifiedWhereClauses::from_iter( | 112 | let where_clauses = chalk_ir::QuantifiedWhereClauses::from_iter( |
133 | &Interner, | 113 | &Interner, |
@@ -139,7 +119,7 @@ impl ToChalk for Ty { | |||
139 | }; | 119 | }; |
140 | chalk_ir::TyKind::Dyn(bounded_ty).intern(&Interner) | 120 | chalk_ir::TyKind::Dyn(bounded_ty).intern(&Interner) |
141 | } | 121 | } |
142 | Ty::Opaque(opaque_ty) => { | 122 | Ty::Alias(AliasTy::Opaque(opaque_ty)) => { |
143 | let opaque_ty_id = opaque_ty.opaque_ty_id.to_chalk(db); | 123 | let opaque_ty_id = opaque_ty.opaque_ty_id.to_chalk(db); |
144 | let substitution = opaque_ty.parameters.to_chalk(db); | 124 | let substitution = opaque_ty.parameters.to_chalk(db); |
145 | chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Opaque(chalk_ir::OpaqueTy { | 125 | chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Opaque(chalk_ir::OpaqueTy { |
@@ -154,9 +134,7 @@ impl ToChalk for Ty { | |||
154 | fn from_chalk(db: &dyn HirDatabase, chalk: chalk_ir::Ty<Interner>) -> Self { | 134 | fn from_chalk(db: &dyn HirDatabase, chalk: chalk_ir::Ty<Interner>) -> Self { |
155 | match chalk.data(&Interner).kind.clone() { | 135 | match chalk.data(&Interner).kind.clone() { |
156 | chalk_ir::TyKind::Error => Ty::Unknown, | 136 | chalk_ir::TyKind::Error => Ty::Unknown, |
157 | chalk_ir::TyKind::Array(ty, _size) => { | 137 | chalk_ir::TyKind::Array(ty, _size) => Ty::Array(Substs::single(from_chalk(db, ty))), |
158 | Ty::apply(TypeCtor::Array, Substs::single(from_chalk(db, ty))) | ||
159 | } | ||
160 | chalk_ir::TyKind::Placeholder(idx) => { | 138 | chalk_ir::TyKind::Placeholder(idx) => { |
161 | assert_eq!(idx.ui, UniverseIndex::ROOT); | 139 | assert_eq!(idx.ui, UniverseIndex::ROOT); |
162 | let interned_id = crate::db::GlobalTypeParamId::from_intern_id( | 140 | let interned_id = crate::db::GlobalTypeParamId::from_intern_id( |
@@ -168,12 +146,12 @@ impl ToChalk for Ty { | |||
168 | let associated_ty = | 146 | let associated_ty = |
169 | from_chalk::<TypeAliasAsAssocType, _>(db, proj.associated_ty_id).0; | 147 | from_chalk::<TypeAliasAsAssocType, _>(db, proj.associated_ty_id).0; |
170 | let parameters = from_chalk(db, proj.substitution); | 148 | let parameters = from_chalk(db, proj.substitution); |
171 | Ty::Projection(ProjectionTy { associated_ty, parameters }) | 149 | Ty::Alias(AliasTy::Projection(ProjectionTy { associated_ty, parameters })) |
172 | } | 150 | } |
173 | chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Opaque(opaque_ty)) => { | 151 | chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Opaque(opaque_ty)) => { |
174 | let impl_trait_id = from_chalk(db, opaque_ty.opaque_ty_id); | 152 | let impl_trait_id = from_chalk(db, opaque_ty.opaque_ty_id); |
175 | let parameters = from_chalk(db, opaque_ty.substitution); | 153 | let parameters = from_chalk(db, opaque_ty.substitution); |
176 | Ty::Opaque(OpaqueTy { opaque_ty_id: impl_trait_id, parameters }) | 154 | Ty::Alias(AliasTy::Opaque(OpaqueTy { opaque_ty_id: impl_trait_id, parameters })) |
177 | } | 155 | } |
178 | chalk_ir::TyKind::Function(chalk_ir::FnPointer { | 156 | chalk_ir::TyKind::Function(chalk_ir::FnPointer { |
179 | num_binders, | 157 | num_binders, |
@@ -182,19 +160,17 @@ impl ToChalk for Ty { | |||
182 | .. | 160 | .. |
183 | }) => { | 161 | }) => { |
184 | assert_eq!(num_binders, 0); | 162 | assert_eq!(num_binders, 0); |
185 | let parameters: Substs = from_chalk( | 163 | let substs: Substs = from_chalk( |
186 | db, | 164 | db, |
187 | substitution.0.shifted_out(&Interner).expect("fn ptr should have no binders"), | 165 | substitution.0.shifted_out(&Interner).expect("fn ptr should have no binders"), |
188 | ); | 166 | ); |
189 | Ty::Apply(ApplicationTy { | 167 | Ty::Function(FnPointer { |
190 | ctor: TypeCtor::FnPtr { | 168 | num_args: (substs.len() - 1), |
191 | num_args: (parameters.len() - 1) as u16, | 169 | sig: FnSig { variadic }, |
192 | is_varargs: variadic, | 170 | substs, |
193 | }, | ||
194 | parameters, | ||
195 | }) | 171 | }) |
196 | } | 172 | } |
197 | chalk_ir::TyKind::BoundVar(idx) => Ty::Bound(idx), | 173 | chalk_ir::TyKind::BoundVar(idx) => Ty::BoundVar(idx), |
198 | chalk_ir::TyKind::InferenceVar(_iv, _kind) => Ty::Unknown, | 174 | chalk_ir::TyKind::InferenceVar(_iv, _kind) => Ty::Unknown, |
199 | chalk_ir::TyKind::Dyn(where_clauses) => { | 175 | chalk_ir::TyKind::Dyn(where_clauses) => { |
200 | assert_eq!(where_clauses.bounds.binders.len(&Interner), 1); | 176 | assert_eq!(where_clauses.bounds.binders.len(&Interner), 1); |
@@ -207,93 +183,66 @@ impl ToChalk for Ty { | |||
207 | Ty::Dyn(predicates) | 183 | Ty::Dyn(predicates) |
208 | } | 184 | } |
209 | 185 | ||
210 | chalk_ir::TyKind::Adt(struct_id, subst) => { | 186 | chalk_ir::TyKind::Adt(struct_id, subst) => Ty::Adt(struct_id.0, from_chalk(db, subst)), |
211 | apply_ty_from_chalk(db, TypeCtor::Adt(struct_id.0), subst) | 187 | chalk_ir::TyKind::AssociatedType(type_id, subst) => Ty::AssociatedType( |
212 | } | 188 | from_chalk::<TypeAliasAsAssocType, _>(db, type_id).0, |
213 | chalk_ir::TyKind::AssociatedType(type_id, subst) => apply_ty_from_chalk( | 189 | from_chalk(db, subst), |
214 | db, | ||
215 | TypeCtor::AssociatedType(from_chalk::<TypeAliasAsAssocType, _>(db, type_id).0), | ||
216 | subst, | ||
217 | ), | 190 | ), |
191 | |||
218 | chalk_ir::TyKind::OpaqueType(opaque_type_id, subst) => { | 192 | chalk_ir::TyKind::OpaqueType(opaque_type_id, subst) => { |
219 | apply_ty_from_chalk(db, TypeCtor::OpaqueType(from_chalk(db, opaque_type_id)), subst) | 193 | Ty::OpaqueType(from_chalk(db, opaque_type_id), from_chalk(db, subst)) |
220 | } | 194 | } |
221 | 195 | ||
222 | chalk_ir::TyKind::Scalar(Scalar::Bool) => Ty::simple(TypeCtor::Bool), | 196 | chalk_ir::TyKind::Scalar(scalar) => Ty::Scalar(scalar), |
223 | chalk_ir::TyKind::Scalar(Scalar::Char) => Ty::simple(TypeCtor::Char), | ||
224 | chalk_ir::TyKind::Scalar(Scalar::Int(int_ty)) => Ty::simple(TypeCtor::Int(IntTy { | ||
225 | signedness: Signedness::Signed, | ||
226 | bitness: bitness_from_chalk_int(int_ty), | ||
227 | })), | ||
228 | chalk_ir::TyKind::Scalar(Scalar::Uint(uint_ty)) => Ty::simple(TypeCtor::Int(IntTy { | ||
229 | signedness: Signedness::Unsigned, | ||
230 | bitness: bitness_from_chalk_uint(uint_ty), | ||
231 | })), | ||
232 | chalk_ir::TyKind::Scalar(Scalar::Float(chalk_ir::FloatTy::F32)) => { | ||
233 | Ty::simple(TypeCtor::Float(FloatTy { bitness: FloatBitness::X32 })) | ||
234 | } | ||
235 | chalk_ir::TyKind::Scalar(Scalar::Float(chalk_ir::FloatTy::F64)) => { | ||
236 | Ty::simple(TypeCtor::Float(FloatTy { bitness: FloatBitness::X64 })) | ||
237 | } | ||
238 | chalk_ir::TyKind::Tuple(cardinality, subst) => { | 197 | chalk_ir::TyKind::Tuple(cardinality, subst) => { |
239 | apply_ty_from_chalk(db, TypeCtor::Tuple { cardinality: cardinality as u16 }, subst) | 198 | Ty::Tuple(cardinality, from_chalk(db, subst)) |
240 | } | 199 | } |
241 | chalk_ir::TyKind::Raw(mutability, ty) => { | 200 | chalk_ir::TyKind::Raw(mutability, ty) => { |
242 | Ty::apply_one(TypeCtor::RawPtr(from_chalk(db, mutability)), from_chalk(db, ty)) | 201 | Ty::Raw(mutability, Substs::single(from_chalk(db, ty))) |
243 | } | 202 | } |
244 | chalk_ir::TyKind::Slice(ty) => Ty::apply_one(TypeCtor::Slice, from_chalk(db, ty)), | 203 | chalk_ir::TyKind::Slice(ty) => Ty::Slice(Substs::single(from_chalk(db, ty))), |
245 | chalk_ir::TyKind::Ref(mutability, _lifetime, ty) => { | 204 | chalk_ir::TyKind::Ref(mutability, _lifetime, ty) => { |
246 | Ty::apply_one(TypeCtor::Ref(from_chalk(db, mutability)), from_chalk(db, ty)) | 205 | Ty::Ref(mutability, Substs::single(from_chalk(db, ty))) |
247 | } | 206 | } |
248 | chalk_ir::TyKind::Str => Ty::simple(TypeCtor::Str), | 207 | chalk_ir::TyKind::Str => Ty::Str, |
249 | chalk_ir::TyKind::Never => Ty::simple(TypeCtor::Never), | 208 | chalk_ir::TyKind::Never => Ty::Never, |
250 | 209 | ||
251 | chalk_ir::TyKind::FnDef(fn_def_id, subst) => { | 210 | chalk_ir::TyKind::FnDef(fn_def_id, subst) => { |
252 | let callable_def = from_chalk(db, fn_def_id); | 211 | Ty::FnDef(from_chalk(db, fn_def_id), from_chalk(db, subst)) |
253 | apply_ty_from_chalk(db, TypeCtor::FnDef(callable_def), subst) | ||
254 | } | 212 | } |
255 | 213 | ||
256 | chalk_ir::TyKind::Closure(id, subst) => { | 214 | chalk_ir::TyKind::Closure(id, subst) => { |
257 | let id: crate::db::ClosureId = id.into(); | 215 | let id: crate::db::ClosureId = id.into(); |
258 | let (def, expr) = db.lookup_intern_closure(id); | 216 | let (def, expr) = db.lookup_intern_closure(id); |
259 | apply_ty_from_chalk(db, TypeCtor::Closure { def, expr }, subst) | 217 | Ty::Closure(def, expr, from_chalk(db, subst)) |
260 | } | 218 | } |
261 | 219 | ||
262 | chalk_ir::TyKind::Foreign(foreign_def_id) => Ty::simple(TypeCtor::ForeignType( | 220 | chalk_ir::TyKind::Foreign(foreign_def_id) => { |
263 | from_chalk::<TypeAliasAsForeignType, _>(db, foreign_def_id).0, | 221 | Ty::ForeignType(from_chalk::<TypeAliasAsForeignType, _>(db, foreign_def_id).0) |
264 | )), | 222 | } |
265 | chalk_ir::TyKind::Generator(_, _) => unimplemented!(), // FIXME | 223 | chalk_ir::TyKind::Generator(_, _) => unimplemented!(), // FIXME |
266 | chalk_ir::TyKind::GeneratorWitness(_, _) => unimplemented!(), // FIXME | 224 | chalk_ir::TyKind::GeneratorWitness(_, _) => unimplemented!(), // FIXME |
267 | } | 225 | } |
268 | } | 226 | } |
269 | } | 227 | } |
270 | 228 | ||
271 | fn apply_ty_from_chalk( | ||
272 | db: &dyn HirDatabase, | ||
273 | ctor: TypeCtor, | ||
274 | subst: chalk_ir::Substitution<Interner>, | ||
275 | ) -> Ty { | ||
276 | Ty::Apply(ApplicationTy { ctor, parameters: from_chalk(db, subst) }) | ||
277 | } | ||
278 | |||
279 | /// We currently don't model lifetimes, but Chalk does. So, we have to insert a | 229 | /// We currently don't model lifetimes, but Chalk does. So, we have to insert a |
280 | /// fake lifetime here, because Chalks built-in logic may expect it to be there. | 230 | /// fake lifetime here, because Chalks built-in logic may expect it to be there. |
281 | fn ref_to_chalk( | 231 | fn ref_to_chalk( |
282 | db: &dyn HirDatabase, | 232 | db: &dyn HirDatabase, |
283 | mutability: Mutability, | 233 | mutability: chalk_ir::Mutability, |
284 | subst: Substs, | 234 | subst: Substs, |
285 | ) -> chalk_ir::Ty<Interner> { | 235 | ) -> chalk_ir::Ty<Interner> { |
286 | let arg = subst[0].clone().to_chalk(db); | 236 | let arg = subst[0].clone().to_chalk(db); |
287 | let lifetime = LifetimeData::Static.intern(&Interner); | 237 | let lifetime = LifetimeData::Static.intern(&Interner); |
288 | chalk_ir::TyKind::Ref(mutability.to_chalk(db), lifetime, arg).intern(&Interner) | 238 | chalk_ir::TyKind::Ref(mutability, lifetime, arg).intern(&Interner) |
289 | } | 239 | } |
290 | 240 | ||
291 | /// We currently don't model constants, but Chalk does. So, we have to insert a | 241 | /// We currently don't model constants, but Chalk does. So, we have to insert a |
292 | /// fake constant here, because Chalks built-in logic may expect it to be there. | 242 | /// fake constant here, because Chalks built-in logic may expect it to be there. |
293 | fn array_to_chalk(db: &dyn HirDatabase, subst: Substs) -> chalk_ir::Ty<Interner> { | 243 | fn array_to_chalk(db: &dyn HirDatabase, subst: Substs) -> chalk_ir::Ty<Interner> { |
294 | let arg = subst[0].clone().to_chalk(db); | 244 | let arg = subst[0].clone().to_chalk(db); |
295 | let usize_ty = | 245 | let usize_ty = chalk_ir::TyKind::Scalar(Scalar::Uint(UintTy::Usize)).intern(&Interner); |
296 | chalk_ir::TyKind::Scalar(Scalar::Uint(chalk_ir::UintTy::Usize)).intern(&Interner); | ||
297 | let const_ = chalk_ir::ConstData { | 246 | let const_ = chalk_ir::ConstData { |
298 | ty: usize_ty, | 247 | ty: usize_ty, |
299 | value: chalk_ir::ConstValue::Concrete(chalk_ir::ConcreteConst { interned: () }), | 248 | value: chalk_ir::ConstValue::Concrete(chalk_ir::ConcreteConst { interned: () }), |
@@ -364,71 +313,6 @@ impl ToChalk for OpaqueTyId { | |||
364 | } | 313 | } |
365 | } | 314 | } |
366 | 315 | ||
367 | fn bitness_from_chalk_uint(uint_ty: chalk_ir::UintTy) -> IntBitness { | ||
368 | use chalk_ir::UintTy; | ||
369 | |||
370 | match uint_ty { | ||
371 | UintTy::Usize => IntBitness::Xsize, | ||
372 | UintTy::U8 => IntBitness::X8, | ||
373 | UintTy::U16 => IntBitness::X16, | ||
374 | UintTy::U32 => IntBitness::X32, | ||
375 | UintTy::U64 => IntBitness::X64, | ||
376 | UintTy::U128 => IntBitness::X128, | ||
377 | } | ||
378 | } | ||
379 | |||
380 | fn bitness_from_chalk_int(int_ty: chalk_ir::IntTy) -> IntBitness { | ||
381 | use chalk_ir::IntTy; | ||
382 | |||
383 | match int_ty { | ||
384 | IntTy::Isize => IntBitness::Xsize, | ||
385 | IntTy::I8 => IntBitness::X8, | ||
386 | IntTy::I16 => IntBitness::X16, | ||
387 | IntTy::I32 => IntBitness::X32, | ||
388 | IntTy::I64 => IntBitness::X64, | ||
389 | IntTy::I128 => IntBitness::X128, | ||
390 | } | ||
391 | } | ||
392 | |||
393 | fn int_ty_to_chalk(int_ty: IntTy) -> Scalar { | ||
394 | use chalk_ir::{IntTy, UintTy}; | ||
395 | |||
396 | match int_ty.signedness { | ||
397 | Signedness::Signed => Scalar::Int(match int_ty.bitness { | ||
398 | IntBitness::Xsize => IntTy::Isize, | ||
399 | IntBitness::X8 => IntTy::I8, | ||
400 | IntBitness::X16 => IntTy::I16, | ||
401 | IntBitness::X32 => IntTy::I32, | ||
402 | IntBitness::X64 => IntTy::I64, | ||
403 | IntBitness::X128 => IntTy::I128, | ||
404 | }), | ||
405 | Signedness::Unsigned => Scalar::Uint(match int_ty.bitness { | ||
406 | IntBitness::Xsize => UintTy::Usize, | ||
407 | IntBitness::X8 => UintTy::U8, | ||
408 | IntBitness::X16 => UintTy::U16, | ||
409 | IntBitness::X32 => UintTy::U32, | ||
410 | IntBitness::X64 => UintTy::U64, | ||
411 | IntBitness::X128 => UintTy::U128, | ||
412 | }), | ||
413 | } | ||
414 | } | ||
415 | |||
416 | impl ToChalk for Mutability { | ||
417 | type Chalk = chalk_ir::Mutability; | ||
418 | fn to_chalk(self, _db: &dyn HirDatabase) -> Self::Chalk { | ||
419 | match self { | ||
420 | Mutability::Shared => chalk_ir::Mutability::Not, | ||
421 | Mutability::Mut => chalk_ir::Mutability::Mut, | ||
422 | } | ||
423 | } | ||
424 | fn from_chalk(_db: &dyn HirDatabase, chalk: Self::Chalk) -> Self { | ||
425 | match chalk { | ||
426 | chalk_ir::Mutability::Mut => Mutability::Mut, | ||
427 | chalk_ir::Mutability::Not => Mutability::Shared, | ||
428 | } | ||
429 | } | ||
430 | } | ||
431 | |||
432 | impl ToChalk for hir_def::ImplId { | 316 | impl ToChalk for hir_def::ImplId { |
433 | type Chalk = ImplId; | 317 | type Chalk = ImplId; |
434 | 318 | ||
@@ -632,20 +516,12 @@ where | |||
632 | type Chalk = chalk_ir::Canonical<T::Chalk>; | 516 | type Chalk = chalk_ir::Canonical<T::Chalk>; |
633 | 517 | ||
634 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Canonical<T::Chalk> { | 518 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Canonical<T::Chalk> { |
635 | let kinds = self | 519 | let kinds = self.kinds.iter().map(|&tk| { |
636 | .kinds | 520 | chalk_ir::CanonicalVarKind::new( |
637 | .iter() | 521 | chalk_ir::VariableKind::Ty(tk), |
638 | .map(|k| match k { | 522 | chalk_ir::UniverseIndex::ROOT, |
639 | TyKind::General => chalk_ir::TyVariableKind::General, | 523 | ) |
640 | TyKind::Integer => chalk_ir::TyVariableKind::Integer, | 524 | }); |
641 | TyKind::Float => chalk_ir::TyVariableKind::Float, | ||
642 | }) | ||
643 | .map(|tk| { | ||
644 | chalk_ir::CanonicalVarKind::new( | ||
645 | chalk_ir::VariableKind::Ty(tk), | ||
646 | chalk_ir::UniverseIndex::ROOT, | ||
647 | ) | ||
648 | }); | ||
649 | let value = self.value.to_chalk(db); | 525 | let value = self.value.to_chalk(db); |
650 | chalk_ir::Canonical { | 526 | chalk_ir::Canonical { |
651 | value, | 527 | value, |
@@ -658,17 +534,13 @@ where | |||
658 | .binders | 534 | .binders |
659 | .iter(&Interner) | 535 | .iter(&Interner) |
660 | .map(|k| match k.kind { | 536 | .map(|k| match k.kind { |
661 | chalk_ir::VariableKind::Ty(tk) => match tk { | 537 | chalk_ir::VariableKind::Ty(tk) => tk, |
662 | chalk_ir::TyVariableKind::General => TyKind::General, | ||
663 | chalk_ir::TyVariableKind::Integer => TyKind::Integer, | ||
664 | chalk_ir::TyVariableKind::Float => TyKind::Float, | ||
665 | }, | ||
666 | // HACK: Chalk can sometimes return new lifetime variables. We | 538 | // HACK: Chalk can sometimes return new lifetime variables. We |
667 | // want to just skip them, but to not mess up the indices of | 539 | // want to just skip them, but to not mess up the indices of |
668 | // other variables, we'll just create a new type variable in | 540 | // other variables, we'll just create a new type variable in |
669 | // their place instead. This should not matter (we never see the | 541 | // their place instead. This should not matter (we never see the |
670 | // actual *uses* of the lifetime variable). | 542 | // actual *uses* of the lifetime variable). |
671 | chalk_ir::VariableKind::Lifetime => TyKind::General, | 543 | chalk_ir::VariableKind::Lifetime => chalk_ir::TyVariableKind::General, |
672 | chalk_ir::VariableKind::Const(_) => panic!("unexpected const from Chalk"), | 544 | chalk_ir::VariableKind::Const(_) => panic!("unexpected const from Chalk"), |
673 | }) | 545 | }) |
674 | .collect(); | 546 | .collect(); |
diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs index 8607139ba..fe32f39b6 100644 --- a/crates/ide/src/diagnostics.rs +++ b/crates/ide/src/diagnostics.rs | |||
@@ -10,15 +10,16 @@ mod field_shorthand; | |||
10 | use std::cell::RefCell; | 10 | use std::cell::RefCell; |
11 | 11 | ||
12 | use hir::{ | 12 | use hir::{ |
13 | db::AstDatabase, | ||
13 | diagnostics::{Diagnostic as _, DiagnosticCode, DiagnosticSinkBuilder}, | 14 | diagnostics::{Diagnostic as _, DiagnosticCode, DiagnosticSinkBuilder}, |
14 | Semantics, | 15 | InFile, Semantics, |
15 | }; | 16 | }; |
16 | use ide_db::{base_db::SourceDatabase, RootDatabase}; | 17 | use ide_db::{base_db::SourceDatabase, RootDatabase}; |
17 | use itertools::Itertools; | 18 | use itertools::Itertools; |
18 | use rustc_hash::FxHashSet; | 19 | use rustc_hash::FxHashSet; |
19 | use syntax::{ | 20 | use syntax::{ |
20 | ast::{self, AstNode}, | 21 | ast::{self, AstNode}, |
21 | SyntaxNode, TextRange, | 22 | SyntaxNode, SyntaxNodePtr, TextRange, |
22 | }; | 23 | }; |
23 | use text_edit::TextEdit; | 24 | use text_edit::TextEdit; |
24 | 25 | ||
@@ -147,20 +148,38 @@ pub(crate) fn diagnostics( | |||
147 | 148 | ||
148 | // Override severity and mark as unused. | 149 | // Override severity and mark as unused. |
149 | res.borrow_mut().push( | 150 | res.borrow_mut().push( |
150 | Diagnostic::hint(sema.diagnostics_display_range(d).range, d.message()) | 151 | Diagnostic::hint( |
151 | .with_unused(true) | 152 | sema.diagnostics_display_range(d.display_source()).range, |
152 | .with_code(Some(d.code())), | 153 | d.message(), |
154 | ) | ||
155 | .with_unused(true) | ||
156 | .with_code(Some(d.code())), | ||
153 | ); | 157 | ); |
154 | }) | 158 | }) |
155 | .on::<hir::diagnostics::UnresolvedProcMacro, _>(|d| { | 159 | .on::<hir::diagnostics::UnresolvedProcMacro, _>(|d| { |
156 | // Use more accurate position if available. | 160 | // Use more accurate position if available. |
157 | let display_range = | 161 | let display_range = d |
158 | d.precise_location.unwrap_or_else(|| sema.diagnostics_display_range(d).range); | 162 | .precise_location |
163 | .unwrap_or_else(|| sema.diagnostics_display_range(d.display_source()).range); | ||
159 | 164 | ||
160 | // FIXME: it would be nice to tell the user whether proc macros are currently disabled | 165 | // FIXME: it would be nice to tell the user whether proc macros are currently disabled |
161 | res.borrow_mut() | 166 | res.borrow_mut() |
162 | .push(Diagnostic::hint(display_range, d.message()).with_code(Some(d.code()))); | 167 | .push(Diagnostic::hint(display_range, d.message()).with_code(Some(d.code()))); |
163 | }) | 168 | }) |
169 | .on::<hir::diagnostics::UnresolvedMacroCall, _>(|d| { | ||
170 | let last_path_segment = sema.db.parse_or_expand(d.file).and_then(|root| { | ||
171 | d.node | ||
172 | .to_node(&root) | ||
173 | .path() | ||
174 | .and_then(|it| it.segment()) | ||
175 | .and_then(|it| it.name_ref()) | ||
176 | .map(|it| InFile::new(d.file, SyntaxNodePtr::new(it.syntax()))) | ||
177 | }); | ||
178 | let diagnostics = last_path_segment.unwrap_or_else(|| d.display_source()); | ||
179 | let display_range = sema.diagnostics_display_range(diagnostics).range; | ||
180 | res.borrow_mut() | ||
181 | .push(Diagnostic::error(display_range, d.message()).with_code(Some(d.code()))); | ||
182 | }) | ||
164 | // Only collect experimental diagnostics when they're enabled. | 183 | // Only collect experimental diagnostics when they're enabled. |
165 | .filter(|diag| !(diag.is_experimental() && config.disable_experimental)) | 184 | .filter(|diag| !(diag.is_experimental() && config.disable_experimental)) |
166 | .filter(|diag| !config.disabled.contains(diag.code().as_str())); | 185 | .filter(|diag| !config.disabled.contains(diag.code().as_str())); |
@@ -170,8 +189,11 @@ pub(crate) fn diagnostics( | |||
170 | // Diagnostics not handled above get no fix and default treatment. | 189 | // Diagnostics not handled above get no fix and default treatment. |
171 | .build(|d| { | 190 | .build(|d| { |
172 | res.borrow_mut().push( | 191 | res.borrow_mut().push( |
173 | Diagnostic::error(sema.diagnostics_display_range(d).range, d.message()) | 192 | Diagnostic::error( |
174 | .with_code(Some(d.code())), | 193 | sema.diagnostics_display_range(d.display_source()).range, |
194 | d.message(), | ||
195 | ) | ||
196 | .with_code(Some(d.code())), | ||
175 | ); | 197 | ); |
176 | }); | 198 | }); |
177 | 199 | ||
@@ -183,13 +205,13 @@ pub(crate) fn diagnostics( | |||
183 | } | 205 | } |
184 | 206 | ||
185 | fn diagnostic_with_fix<D: DiagnosticWithFix>(d: &D, sema: &Semantics<RootDatabase>) -> Diagnostic { | 207 | fn diagnostic_with_fix<D: DiagnosticWithFix>(d: &D, sema: &Semantics<RootDatabase>) -> Diagnostic { |
186 | Diagnostic::error(sema.diagnostics_display_range(d).range, d.message()) | 208 | Diagnostic::error(sema.diagnostics_display_range(d.display_source()).range, d.message()) |
187 | .with_fix(d.fix(&sema)) | 209 | .with_fix(d.fix(&sema)) |
188 | .with_code(Some(d.code())) | 210 | .with_code(Some(d.code())) |
189 | } | 211 | } |
190 | 212 | ||
191 | fn warning_with_fix<D: DiagnosticWithFix>(d: &D, sema: &Semantics<RootDatabase>) -> Diagnostic { | 213 | fn warning_with_fix<D: DiagnosticWithFix>(d: &D, sema: &Semantics<RootDatabase>) -> Diagnostic { |
192 | Diagnostic::hint(sema.diagnostics_display_range(d).range, d.message()) | 214 | Diagnostic::hint(sema.diagnostics_display_range(d.display_source()).range, d.message()) |
193 | .with_fix(d.fix(&sema)) | 215 | .with_fix(d.fix(&sema)) |
194 | .with_code(Some(d.code())) | 216 | .with_code(Some(d.code())) |
195 | } | 217 | } |
@@ -646,6 +668,29 @@ fn test_fn() { | |||
646 | } | 668 | } |
647 | 669 | ||
648 | #[test] | 670 | #[test] |
671 | fn test_unresolved_macro_range() { | ||
672 | check_expect( | ||
673 | r#"foo::bar!(92);"#, | ||
674 | expect![[r#" | ||
675 | [ | ||
676 | Diagnostic { | ||
677 | message: "unresolved macro call", | ||
678 | range: 5..8, | ||
679 | severity: Error, | ||
680 | fix: None, | ||
681 | unused: false, | ||
682 | code: Some( | ||
683 | DiagnosticCode( | ||
684 | "unresolved-macro-call", | ||
685 | ), | ||
686 | ), | ||
687 | }, | ||
688 | ] | ||
689 | "#]], | ||
690 | ); | ||
691 | } | ||
692 | |||
693 | #[test] | ||
649 | fn range_mapping_out_of_macros() { | 694 | fn range_mapping_out_of_macros() { |
650 | // FIXME: this is very wrong, but somewhat tricky to fix. | 695 | // FIXME: this is very wrong, but somewhat tricky to fix. |
651 | check_fix( | 696 | check_fix( |
diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 9a605b09d..20b799490 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs | |||
@@ -3496,4 +3496,33 @@ mod foo$0; | |||
3496 | "#]], | 3496 | "#]], |
3497 | ); | 3497 | ); |
3498 | } | 3498 | } |
3499 | |||
3500 | #[test] | ||
3501 | fn hover_self_in_use() { | ||
3502 | check( | ||
3503 | r#" | ||
3504 | //! This should not appear | ||
3505 | mod foo { | ||
3506 | /// But this should appear | ||
3507 | pub mod bar {} | ||
3508 | } | ||
3509 | use foo::bar::{self$0}; | ||
3510 | "#, | ||
3511 | expect![[r#" | ||
3512 | *self* | ||
3513 | |||
3514 | ```rust | ||
3515 | test::foo | ||
3516 | ``` | ||
3517 | |||
3518 | ```rust | ||
3519 | pub mod bar | ||
3520 | ``` | ||
3521 | |||
3522 | --- | ||
3523 | |||
3524 | But this should appear | ||
3525 | "#]], | ||
3526 | ); | ||
3527 | } | ||
3499 | } | 3528 | } |
diff --git a/crates/ide/src/references.rs b/crates/ide/src/references.rs index 55f95ebae..fef70533d 100644 --- a/crates/ide/src/references.rs +++ b/crates/ide/src/references.rs | |||
@@ -39,6 +39,15 @@ pub struct Declaration { | |||
39 | pub access: Option<ReferenceAccess>, | 39 | pub access: Option<ReferenceAccess>, |
40 | } | 40 | } |
41 | 41 | ||
42 | // Feature: Find All References | ||
43 | // | ||
44 | // Shows all references of the item at the cursor location | ||
45 | // | ||
46 | // |=== | ||
47 | // | Editor | Shortcut | ||
48 | // | ||
49 | // | VS Code | kbd:[Shift+Alt+F12] | ||
50 | // |=== | ||
42 | pub(crate) fn find_all_refs( | 51 | pub(crate) fn find_all_refs( |
43 | sema: &Semantics<RootDatabase>, | 52 | sema: &Semantics<RootDatabase>, |
44 | position: FilePosition, | 53 | position: FilePosition, |
@@ -1224,4 +1233,29 @@ fn test() {} | |||
1224 | "#]], | 1233 | "#]], |
1225 | ); | 1234 | ); |
1226 | } | 1235 | } |
1236 | |||
1237 | #[test] | ||
1238 | fn test_const_in_pattern() { | ||
1239 | check( | ||
1240 | r#" | ||
1241 | const A$0: i32 = 42; | ||
1242 | |||
1243 | fn main() { | ||
1244 | match A { | ||
1245 | A => (), | ||
1246 | _ => (), | ||
1247 | } | ||
1248 | if let A = A {} | ||
1249 | } | ||
1250 | "#, | ||
1251 | expect![[r#" | ||
1252 | A Const FileId(0) 0..18 6..7 | ||
1253 | |||
1254 | FileId(0) 42..43 | ||
1255 | FileId(0) 54..55 | ||
1256 | FileId(0) 97..98 | ||
1257 | FileId(0) 101..102 | ||
1258 | "#]], | ||
1259 | ); | ||
1260 | } | ||
1227 | } | 1261 | } |
diff --git a/crates/ide/src/references/rename.rs b/crates/ide/src/references/rename.rs index 175ddd759..22ddeeae3 100644 --- a/crates/ide/src/references/rename.rs +++ b/crates/ide/src/references/rename.rs | |||
@@ -59,6 +59,15 @@ pub(crate) fn prepare_rename( | |||
59 | Ok(RangeInfo::new(range, ())) | 59 | Ok(RangeInfo::new(range, ())) |
60 | } | 60 | } |
61 | 61 | ||
62 | // Feature: Rename | ||
63 | // | ||
64 | // Renames the item below the cursor and all of its references | ||
65 | // | ||
66 | // |=== | ||
67 | // | Editor | Shortcut | ||
68 | // | ||
69 | // | VS Code | kbd:[F2] | ||
70 | // |=== | ||
62 | pub(crate) fn rename( | 71 | pub(crate) fn rename( |
63 | db: &RootDatabase, | 72 | db: &RootDatabase, |
64 | position: FilePosition, | 73 | position: FilePosition, |
diff --git a/crates/ide_assists/src/handlers/apply_demorgan.rs b/crates/ide_assists/src/handlers/apply_demorgan.rs index ed4d11455..6997ea048 100644 --- a/crates/ide_assists/src/handlers/apply_demorgan.rs +++ b/crates/ide_assists/src/handlers/apply_demorgan.rs | |||
@@ -7,18 +7,17 @@ use crate::{utils::invert_boolean_expression, AssistContext, AssistId, AssistKin | |||
7 | // Apply https://en.wikipedia.org/wiki/De_Morgan%27s_laws[De Morgan's law]. | 7 | // Apply https://en.wikipedia.org/wiki/De_Morgan%27s_laws[De Morgan's law]. |
8 | // This transforms expressions of the form `!l || !r` into `!(l && r)`. | 8 | // This transforms expressions of the form `!l || !r` into `!(l && r)`. |
9 | // This also works with `&&`. This assist can only be applied with the cursor | 9 | // This also works with `&&`. This assist can only be applied with the cursor |
10 | // on either `||` or `&&`, with both operands being a negation of some kind. | 10 | // on either `||` or `&&`. |
11 | // This means something of the form `!x` or `x != y`. | ||
12 | // | 11 | // |
13 | // ``` | 12 | // ``` |
14 | // fn main() { | 13 | // fn main() { |
15 | // if x != 4 ||$0 !y {} | 14 | // if x != 4 ||$0 y < 3.14 {} |
16 | // } | 15 | // } |
17 | // ``` | 16 | // ``` |
18 | // -> | 17 | // -> |
19 | // ``` | 18 | // ``` |
20 | // fn main() { | 19 | // fn main() { |
21 | // if !(x == 4 && y) {} | 20 | // if !(x == 4 && !(y < 3.14)) {} |
22 | // } | 21 | // } |
23 | // ``` | 22 | // ``` |
24 | pub(crate) fn apply_demorgan(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | 23 | pub(crate) fn apply_demorgan(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { |
@@ -33,11 +32,11 @@ pub(crate) fn apply_demorgan(acc: &mut Assists, ctx: &AssistContext) -> Option<( | |||
33 | 32 | ||
34 | let lhs = expr.lhs()?; | 33 | let lhs = expr.lhs()?; |
35 | let lhs_range = lhs.syntax().text_range(); | 34 | let lhs_range = lhs.syntax().text_range(); |
36 | let not_lhs = invert_boolean_expression(lhs); | 35 | let not_lhs = invert_boolean_expression(&ctx.sema, lhs); |
37 | 36 | ||
38 | let rhs = expr.rhs()?; | 37 | let rhs = expr.rhs()?; |
39 | let rhs_range = rhs.syntax().text_range(); | 38 | let rhs_range = rhs.syntax().text_range(); |
40 | let not_rhs = invert_boolean_expression(rhs); | 39 | let not_rhs = invert_boolean_expression(&ctx.sema, rhs); |
41 | 40 | ||
42 | acc.add( | 41 | acc.add( |
43 | AssistId("apply_demorgan", AssistKind::RefactorRewrite), | 42 | AssistId("apply_demorgan", AssistKind::RefactorRewrite), |
@@ -62,10 +61,77 @@ fn opposite_logic_op(kind: ast::BinOp) -> Option<&'static str> { | |||
62 | 61 | ||
63 | #[cfg(test)] | 62 | #[cfg(test)] |
64 | mod tests { | 63 | mod tests { |
64 | use ide_db::helpers::FamousDefs; | ||
65 | |||
65 | use super::*; | 66 | use super::*; |
66 | 67 | ||
67 | use crate::tests::{check_assist, check_assist_not_applicable}; | 68 | use crate::tests::{check_assist, check_assist_not_applicable}; |
68 | 69 | ||
70 | const ORDABLE_FIXTURE: &'static str = r" | ||
71 | //- /lib.rs deps:core crate:ordable | ||
72 | struct NonOrderable; | ||
73 | struct Orderable; | ||
74 | impl core::cmp::Ord for Orderable {} | ||
75 | "; | ||
76 | |||
77 | fn check(ra_fixture_before: &str, ra_fixture_after: &str) { | ||
78 | let before = &format!( | ||
79 | "//- /main.rs crate:main deps:core,ordable\n{}\n{}{}", | ||
80 | ra_fixture_before, | ||
81 | FamousDefs::FIXTURE, | ||
82 | ORDABLE_FIXTURE | ||
83 | ); | ||
84 | check_assist(apply_demorgan, before, &format!("{}\n", ra_fixture_after)); | ||
85 | } | ||
86 | |||
87 | #[test] | ||
88 | fn demorgan_handles_leq() { | ||
89 | check( | ||
90 | r"use ordable::Orderable; | ||
91 | fn f() { | ||
92 | Orderable < Orderable &&$0 Orderable <= Orderable | ||
93 | }", | ||
94 | r"use ordable::Orderable; | ||
95 | fn f() { | ||
96 | !(Orderable >= Orderable || Orderable > Orderable) | ||
97 | }", | ||
98 | ); | ||
99 | check( | ||
100 | r"use ordable::NonOrderable; | ||
101 | fn f() { | ||
102 | NonOrderable < NonOrderable &&$0 NonOrderable <= NonOrderable | ||
103 | }", | ||
104 | r"use ordable::NonOrderable; | ||
105 | fn f() { | ||
106 | !(!(NonOrderable < NonOrderable) || !(NonOrderable <= NonOrderable)) | ||
107 | }", | ||
108 | ); | ||
109 | } | ||
110 | |||
111 | #[test] | ||
112 | fn demorgan_handles_geq() { | ||
113 | check( | ||
114 | r"use ordable::Orderable; | ||
115 | fn f() { | ||
116 | Orderable > Orderable &&$0 Orderable >= Orderable | ||
117 | }", | ||
118 | r"use ordable::Orderable; | ||
119 | fn f() { | ||
120 | !(Orderable <= Orderable || Orderable < Orderable) | ||
121 | }", | ||
122 | ); | ||
123 | check( | ||
124 | r"use ordable::NonOrderable; | ||
125 | fn f() { | ||
126 | Orderable > Orderable &&$0 Orderable >= Orderable | ||
127 | }", | ||
128 | r"use ordable::NonOrderable; | ||
129 | fn f() { | ||
130 | !(!(Orderable > Orderable) || !(Orderable >= Orderable)) | ||
131 | }", | ||
132 | ); | ||
133 | } | ||
134 | |||
69 | #[test] | 135 | #[test] |
70 | fn demorgan_turns_and_into_or() { | 136 | fn demorgan_turns_and_into_or() { |
71 | check_assist(apply_demorgan, "fn f() { !x &&$0 !x }", "fn f() { !(x || x) }") | 137 | check_assist(apply_demorgan, "fn f() { !x &&$0 !x }", "fn f() { !(x || x) }") |
diff --git a/crates/ide_assists/src/handlers/auto_import.rs b/crates/ide_assists/src/handlers/auto_import.rs index e93901cb3..dc38f90e9 100644 --- a/crates/ide_assists/src/handlers/auto_import.rs +++ b/crates/ide_assists/src/handlers/auto_import.rs | |||
@@ -33,9 +33,9 @@ use crate::{AssistContext, AssistId, AssistKind, Assists, GroupLabel}; | |||
33 | // use super::AssistContext; | 33 | // use super::AssistContext; |
34 | // ``` | 34 | // ``` |
35 | // | 35 | // |
36 | // .Merge Behaviour | 36 | // .Merge Behavior |
37 | // | 37 | // |
38 | // It is possible to configure how use-trees are merged with the `importMergeBehaviour` setting. | 38 | // It is possible to configure how use-trees are merged with the `importMergeBehavior` setting. |
39 | // It has the following configurations: | 39 | // It has the following configurations: |
40 | // | 40 | // |
41 | // - `full`: This setting will cause auto-import to always completely merge use-trees that share the | 41 | // - `full`: This setting will cause auto-import to always completely merge use-trees that share the |
@@ -46,7 +46,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists, GroupLabel}; | |||
46 | // - `none`: This setting will cause auto-import to never merge use-trees keeping them as simple | 46 | // - `none`: This setting will cause auto-import to never merge use-trees keeping them as simple |
47 | // paths. | 47 | // paths. |
48 | // | 48 | // |
49 | // In `VS Code` the configuration for this is `rust-analyzer.assist.importMergeBehaviour`. | 49 | // In `VS Code` the configuration for this is `rust-analyzer.assist.importMergeBehavior`. |
50 | // | 50 | // |
51 | // .Import Prefix | 51 | // .Import Prefix |
52 | // | 52 | // |
diff --git a/crates/ide_assists/src/handlers/early_return.rs b/crates/ide_assists/src/handlers/early_return.rs index 6b87c3c05..9e0918477 100644 --- a/crates/ide_assists/src/handlers/early_return.rs +++ b/crates/ide_assists/src/handlers/early_return.rs | |||
@@ -111,7 +111,7 @@ pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext) | |||
111 | let new_expr = { | 111 | let new_expr = { |
112 | let then_branch = | 112 | let then_branch = |
113 | make::block_expr(once(make::expr_stmt(early_expression).into()), None); | 113 | make::block_expr(once(make::expr_stmt(early_expression).into()), None); |
114 | let cond = invert_boolean_expression(cond_expr); | 114 | let cond = invert_boolean_expression(&ctx.sema, cond_expr); |
115 | make::expr_if(make::condition(cond, None), then_branch, None) | 115 | make::expr_if(make::condition(cond, None), then_branch, None) |
116 | .indent(if_indent_level) | 116 | .indent(if_indent_level) |
117 | }; | 117 | }; |
diff --git a/crates/ide_assists/src/handlers/flip_comma.rs b/crates/ide_assists/src/handlers/flip_comma.rs index 18cf64a34..7f5e75d34 100644 --- a/crates/ide_assists/src/handlers/flip_comma.rs +++ b/crates/ide_assists/src/handlers/flip_comma.rs | |||
@@ -1,4 +1,4 @@ | |||
1 | use syntax::{algo::non_trivia_sibling, Direction, T}; | 1 | use syntax::{algo::non_trivia_sibling, Direction, SyntaxKind, T}; |
2 | 2 | ||
3 | use crate::{AssistContext, AssistId, AssistKind, Assists}; | 3 | use crate::{AssistContext, AssistId, AssistKind, Assists}; |
4 | 4 | ||
@@ -28,6 +28,12 @@ pub(crate) fn flip_comma(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | |||
28 | return None; | 28 | return None; |
29 | } | 29 | } |
30 | 30 | ||
31 | // Don't apply a "flip" inside the macro call | ||
32 | // since macro input are just mere tokens | ||
33 | if comma.ancestors().any(|it| it.kind() == SyntaxKind::MACRO_CALL) { | ||
34 | return None; | ||
35 | } | ||
36 | |||
31 | acc.add( | 37 | acc.add( |
32 | AssistId("flip_comma", AssistKind::RefactorRewrite), | 38 | AssistId("flip_comma", AssistKind::RefactorRewrite), |
33 | "Flip comma", | 39 | "Flip comma", |
@@ -43,7 +49,7 @@ pub(crate) fn flip_comma(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | |||
43 | mod tests { | 49 | mod tests { |
44 | use super::*; | 50 | use super::*; |
45 | 51 | ||
46 | use crate::tests::{check_assist, check_assist_target}; | 52 | use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target}; |
47 | 53 | ||
48 | #[test] | 54 | #[test] |
49 | fn flip_comma_works_for_function_parameters() { | 55 | fn flip_comma_works_for_function_parameters() { |
@@ -81,4 +87,20 @@ mod tests { | |||
81 | ",", | 87 | ",", |
82 | ); | 88 | ); |
83 | } | 89 | } |
90 | |||
91 | #[test] | ||
92 | fn flip_comma_works() { | ||
93 | check_assist( | ||
94 | flip_comma, | ||
95 | r#"fn main() {((1, 2),$0 (3, 4));}"#, | ||
96 | r#"fn main() {((3, 4), (1, 2));}"#, | ||
97 | ) | ||
98 | } | ||
99 | |||
100 | #[test] | ||
101 | fn flip_comma_not_applicable_for_macro_input() { | ||
102 | // "Flip comma" assist shouldn't be applicable inside the macro call | ||
103 | // See https://github.com/rust-analyzer/rust-analyzer/issues/7693 | ||
104 | check_assist_not_applicable(flip_comma, r#"bar!(a,$0 b)"#); | ||
105 | } | ||
84 | } | 106 | } |
diff --git a/crates/ide_assists/src/handlers/generate_enum_match_method.rs b/crates/ide_assists/src/handlers/generate_enum_is_method.rs index aeb887e71..7e181a480 100644 --- a/crates/ide_assists/src/handlers/generate_enum_match_method.rs +++ b/crates/ide_assists/src/handlers/generate_enum_is_method.rs | |||
@@ -1,14 +1,13 @@ | |||
1 | use stdx::{format_to, to_lower_snake_case}; | 1 | use stdx::to_lower_snake_case; |
2 | use syntax::ast::VisibilityOwner; | 2 | use syntax::ast::VisibilityOwner; |
3 | use syntax::ast::{self, AstNode, NameOwner}; | 3 | use syntax::ast::{self, AstNode, NameOwner}; |
4 | use test_utils::mark; | ||
5 | 4 | ||
6 | use crate::{ | 5 | use crate::{ |
7 | utils::{find_impl_block_end, find_struct_impl, generate_impl_text}, | 6 | utils::{add_method_to_adt, find_struct_impl}, |
8 | AssistContext, AssistId, AssistKind, Assists, | 7 | AssistContext, AssistId, AssistKind, Assists, |
9 | }; | 8 | }; |
10 | 9 | ||
11 | // Assist: generate_enum_match_method | 10 | // Assist: generate_enum_is_method |
12 | // | 11 | // |
13 | // Generate an `is_` method for an enum variant. | 12 | // Generate an `is_` method for an enum variant. |
14 | // | 13 | // |
@@ -34,79 +33,52 @@ use crate::{ | |||
34 | // } | 33 | // } |
35 | // } | 34 | // } |
36 | // ``` | 35 | // ``` |
37 | pub(crate) fn generate_enum_match_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | 36 | pub(crate) fn generate_enum_is_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { |
38 | let variant = ctx.find_node_at_offset::<ast::Variant>()?; | 37 | let variant = ctx.find_node_at_offset::<ast::Variant>()?; |
39 | let variant_name = variant.name()?; | 38 | let variant_name = variant.name()?; |
40 | let parent_enum = variant.parent_enum(); | 39 | let parent_enum = ast::Adt::Enum(variant.parent_enum()); |
41 | if !matches!(variant.kind(), ast::StructKind::Unit) { | 40 | let pattern_suffix = match variant.kind() { |
42 | mark::hit!(test_gen_enum_match_on_non_unit_variant_not_implemented); | 41 | ast::StructKind::Record(_) => " { .. }", |
43 | return None; | 42 | ast::StructKind::Tuple(_) => "(..)", |
44 | } | 43 | ast::StructKind::Unit => "", |
44 | }; | ||
45 | 45 | ||
46 | let enum_lowercase_name = to_lower_snake_case(&parent_enum.name()?.to_string()); | 46 | let enum_lowercase_name = to_lower_snake_case(&parent_enum.name()?.to_string()); |
47 | let fn_name = to_lower_snake_case(&variant_name.to_string()); | 47 | let fn_name = format!("is_{}", &to_lower_snake_case(variant_name.text())); |
48 | 48 | ||
49 | // Return early if we've found an existing new fn | 49 | // Return early if we've found an existing new fn |
50 | let impl_def = find_struct_impl( | 50 | let impl_def = find_struct_impl(&ctx, &parent_enum, &fn_name)?; |
51 | &ctx, | ||
52 | &ast::Adt::Enum(parent_enum.clone()), | ||
53 | format!("is_{}", fn_name).as_str(), | ||
54 | )?; | ||
55 | 51 | ||
56 | let target = variant.syntax().text_range(); | 52 | let target = variant.syntax().text_range(); |
57 | acc.add( | 53 | acc.add( |
58 | AssistId("generate_enum_match_method", AssistKind::Generate), | 54 | AssistId("generate_enum_is_method", AssistKind::Generate), |
59 | "Generate an `is_` method for an enum variant", | 55 | "Generate an `is_` method for an enum variant", |
60 | target, | 56 | target, |
61 | |builder| { | 57 | |builder| { |
62 | let mut buf = String::with_capacity(512); | ||
63 | |||
64 | if impl_def.is_some() { | ||
65 | buf.push('\n'); | ||
66 | } | ||
67 | |||
68 | let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v)); | 58 | let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v)); |
69 | format_to!( | 59 | let method = format!( |
70 | buf, | ||
71 | " /// Returns `true` if the {} is [`{}`]. | 60 | " /// Returns `true` if the {} is [`{}`]. |
72 | {}fn is_{}(&self) -> bool {{ | 61 | {}fn {}(&self) -> bool {{ |
73 | matches!(self, Self::{}) | 62 | matches!(self, Self::{}{}) |
74 | }}", | 63 | }}", |
75 | enum_lowercase_name, | 64 | enum_lowercase_name, variant_name, vis, fn_name, variant_name, pattern_suffix, |
76 | variant_name, | ||
77 | vis, | ||
78 | fn_name, | ||
79 | variant_name | ||
80 | ); | 65 | ); |
81 | 66 | ||
82 | let start_offset = impl_def | 67 | add_method_to_adt(builder, &parent_enum, impl_def, &method); |
83 | .and_then(|impl_def| find_impl_block_end(impl_def, &mut buf)) | ||
84 | .unwrap_or_else(|| { | ||
85 | buf = generate_impl_text(&ast::Adt::Enum(parent_enum.clone()), &buf); | ||
86 | parent_enum.syntax().text_range().end() | ||
87 | }); | ||
88 | |||
89 | builder.insert(start_offset, buf); | ||
90 | }, | 68 | }, |
91 | ) | 69 | ) |
92 | } | 70 | } |
93 | 71 | ||
94 | #[cfg(test)] | 72 | #[cfg(test)] |
95 | mod tests { | 73 | mod tests { |
96 | use test_utils::mark; | ||
97 | |||
98 | use crate::tests::{check_assist, check_assist_not_applicable}; | 74 | use crate::tests::{check_assist, check_assist_not_applicable}; |
99 | 75 | ||
100 | use super::*; | 76 | use super::*; |
101 | 77 | ||
102 | fn check_not_applicable(ra_fixture: &str) { | ||
103 | check_assist_not_applicable(generate_enum_match_method, ra_fixture) | ||
104 | } | ||
105 | |||
106 | #[test] | 78 | #[test] |
107 | fn test_generate_enum_match_from_variant() { | 79 | fn test_generate_enum_is_from_variant() { |
108 | check_assist( | 80 | check_assist( |
109 | generate_enum_match_method, | 81 | generate_enum_is_method, |
110 | r#" | 82 | r#" |
111 | enum Variant { | 83 | enum Variant { |
112 | Undefined, | 84 | Undefined, |
@@ -129,8 +101,9 @@ impl Variant { | |||
129 | } | 101 | } |
130 | 102 | ||
131 | #[test] | 103 | #[test] |
132 | fn test_generate_enum_match_already_implemented() { | 104 | fn test_generate_enum_is_already_implemented() { |
133 | check_not_applicable( | 105 | check_assist_not_applicable( |
106 | generate_enum_is_method, | ||
134 | r#" | 107 | r#" |
135 | enum Variant { | 108 | enum Variant { |
136 | Undefined, | 109 | Undefined, |
@@ -147,22 +120,59 @@ impl Variant { | |||
147 | } | 120 | } |
148 | 121 | ||
149 | #[test] | 122 | #[test] |
150 | fn test_add_from_impl_no_element() { | 123 | fn test_generate_enum_is_from_tuple_variant() { |
151 | mark::check!(test_gen_enum_match_on_non_unit_variant_not_implemented); | 124 | check_assist( |
152 | check_not_applicable( | 125 | generate_enum_is_method, |
153 | r#" | 126 | r#" |
154 | enum Variant { | 127 | enum Variant { |
155 | Undefined, | 128 | Undefined, |
156 | Minor(u32)$0, | 129 | Minor(u32)$0, |
157 | Major, | 130 | Major, |
158 | }"#, | 131 | }"#, |
132 | r#"enum Variant { | ||
133 | Undefined, | ||
134 | Minor(u32), | ||
135 | Major, | ||
136 | } | ||
137 | |||
138 | impl Variant { | ||
139 | /// Returns `true` if the variant is [`Minor`]. | ||
140 | fn is_minor(&self) -> bool { | ||
141 | matches!(self, Self::Minor(..)) | ||
142 | } | ||
143 | }"#, | ||
144 | ); | ||
145 | } | ||
146 | |||
147 | #[test] | ||
148 | fn test_generate_enum_is_from_record_variant() { | ||
149 | check_assist( | ||
150 | generate_enum_is_method, | ||
151 | r#" | ||
152 | enum Variant { | ||
153 | Undefined, | ||
154 | Minor { foo: i32 }$0, | ||
155 | Major, | ||
156 | }"#, | ||
157 | r#"enum Variant { | ||
158 | Undefined, | ||
159 | Minor { foo: i32 }, | ||
160 | Major, | ||
161 | } | ||
162 | |||
163 | impl Variant { | ||
164 | /// Returns `true` if the variant is [`Minor`]. | ||
165 | fn is_minor(&self) -> bool { | ||
166 | matches!(self, Self::Minor { .. }) | ||
167 | } | ||
168 | }"#, | ||
159 | ); | 169 | ); |
160 | } | 170 | } |
161 | 171 | ||
162 | #[test] | 172 | #[test] |
163 | fn test_generate_enum_match_from_variant_with_one_variant() { | 173 | fn test_generate_enum_is_from_variant_with_one_variant() { |
164 | check_assist( | 174 | check_assist( |
165 | generate_enum_match_method, | 175 | generate_enum_is_method, |
166 | r#"enum Variant { Undefi$0ned }"#, | 176 | r#"enum Variant { Undefi$0ned }"#, |
167 | r#" | 177 | r#" |
168 | enum Variant { Undefined } | 178 | enum Variant { Undefined } |
@@ -177,9 +187,9 @@ impl Variant { | |||
177 | } | 187 | } |
178 | 188 | ||
179 | #[test] | 189 | #[test] |
180 | fn test_generate_enum_match_from_variant_with_visibility_marker() { | 190 | fn test_generate_enum_is_from_variant_with_visibility_marker() { |
181 | check_assist( | 191 | check_assist( |
182 | generate_enum_match_method, | 192 | generate_enum_is_method, |
183 | r#" | 193 | r#" |
184 | pub(crate) enum Variant { | 194 | pub(crate) enum Variant { |
185 | Undefined, | 195 | Undefined, |
@@ -202,9 +212,9 @@ impl Variant { | |||
202 | } | 212 | } |
203 | 213 | ||
204 | #[test] | 214 | #[test] |
205 | fn test_multiple_generate_enum_match_from_variant() { | 215 | fn test_multiple_generate_enum_is_from_variant() { |
206 | check_assist( | 216 | check_assist( |
207 | generate_enum_match_method, | 217 | generate_enum_is_method, |
208 | r#" | 218 | r#" |
209 | enum Variant { | 219 | enum Variant { |
210 | Undefined, | 220 | Undefined, |
diff --git a/crates/ide_assists/src/handlers/generate_enum_projection_method.rs b/crates/ide_assists/src/handlers/generate_enum_projection_method.rs new file mode 100644 index 000000000..871bcab50 --- /dev/null +++ b/crates/ide_assists/src/handlers/generate_enum_projection_method.rs | |||
@@ -0,0 +1,331 @@ | |||
1 | use itertools::Itertools; | ||
2 | use stdx::to_lower_snake_case; | ||
3 | use syntax::ast::VisibilityOwner; | ||
4 | use syntax::ast::{self, AstNode, NameOwner}; | ||
5 | |||
6 | use crate::{ | ||
7 | utils::{add_method_to_adt, find_struct_impl}, | ||
8 | AssistContext, AssistId, AssistKind, Assists, | ||
9 | }; | ||
10 | |||
11 | // Assist: generate_enum_try_into_method | ||
12 | // | ||
13 | // Generate an `try_into_` method for an enum variant. | ||
14 | // | ||
15 | // ``` | ||
16 | // enum Value { | ||
17 | // Number(i32), | ||
18 | // Text(String)$0, | ||
19 | // } | ||
20 | // ``` | ||
21 | // -> | ||
22 | // ``` | ||
23 | // enum Value { | ||
24 | // Number(i32), | ||
25 | // Text(String), | ||
26 | // } | ||
27 | // | ||
28 | // impl Value { | ||
29 | // fn try_into_text(self) -> Result<String, Self> { | ||
30 | // if let Self::Text(v) = self { | ||
31 | // Ok(v) | ||
32 | // } else { | ||
33 | // Err(self) | ||
34 | // } | ||
35 | // } | ||
36 | // } | ||
37 | // ``` | ||
38 | pub(crate) fn generate_enum_try_into_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | ||
39 | generate_enum_projection_method( | ||
40 | acc, | ||
41 | ctx, | ||
42 | "generate_enum_try_into_method", | ||
43 | "Generate an `try_into_` method for an enum variant", | ||
44 | ProjectionProps { | ||
45 | fn_name_prefix: "try_into", | ||
46 | self_param: "self", | ||
47 | return_prefix: "Result<", | ||
48 | return_suffix: ", Self>", | ||
49 | happy_case: "Ok", | ||
50 | sad_case: "Err(self)", | ||
51 | }, | ||
52 | ) | ||
53 | } | ||
54 | |||
55 | // Assist: generate_enum_as_method | ||
56 | // | ||
57 | // Generate an `as_` method for an enum variant. | ||
58 | // | ||
59 | // ``` | ||
60 | // enum Value { | ||
61 | // Number(i32), | ||
62 | // Text(String)$0, | ||
63 | // } | ||
64 | // ``` | ||
65 | // -> | ||
66 | // ``` | ||
67 | // enum Value { | ||
68 | // Number(i32), | ||
69 | // Text(String), | ||
70 | // } | ||
71 | // | ||
72 | // impl Value { | ||
73 | // fn as_text(&self) -> Option<&String> { | ||
74 | // if let Self::Text(v) = self { | ||
75 | // Some(v) | ||
76 | // } else { | ||
77 | // None | ||
78 | // } | ||
79 | // } | ||
80 | // } | ||
81 | // ``` | ||
82 | pub(crate) fn generate_enum_as_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | ||
83 | generate_enum_projection_method( | ||
84 | acc, | ||
85 | ctx, | ||
86 | "generate_enum_as_method", | ||
87 | "Generate an `as_` method for an enum variant", | ||
88 | ProjectionProps { | ||
89 | fn_name_prefix: "as", | ||
90 | self_param: "&self", | ||
91 | return_prefix: "Option<&", | ||
92 | return_suffix: ">", | ||
93 | happy_case: "Some", | ||
94 | sad_case: "None", | ||
95 | }, | ||
96 | ) | ||
97 | } | ||
98 | |||
99 | struct ProjectionProps { | ||
100 | fn_name_prefix: &'static str, | ||
101 | self_param: &'static str, | ||
102 | return_prefix: &'static str, | ||
103 | return_suffix: &'static str, | ||
104 | happy_case: &'static str, | ||
105 | sad_case: &'static str, | ||
106 | } | ||
107 | |||
108 | fn generate_enum_projection_method( | ||
109 | acc: &mut Assists, | ||
110 | ctx: &AssistContext, | ||
111 | assist_id: &'static str, | ||
112 | assist_description: &str, | ||
113 | props: ProjectionProps, | ||
114 | ) -> Option<()> { | ||
115 | let variant = ctx.find_node_at_offset::<ast::Variant>()?; | ||
116 | let variant_name = variant.name()?; | ||
117 | let parent_enum = ast::Adt::Enum(variant.parent_enum()); | ||
118 | |||
119 | let (pattern_suffix, field_type, bound_name) = match variant.kind() { | ||
120 | ast::StructKind::Record(record) => { | ||
121 | let (field,) = record.fields().collect_tuple()?; | ||
122 | let name = field.name()?.to_string(); | ||
123 | let ty = field.ty()?; | ||
124 | let pattern_suffix = format!(" {{ {} }}", name); | ||
125 | (pattern_suffix, ty, name) | ||
126 | } | ||
127 | ast::StructKind::Tuple(tuple) => { | ||
128 | let (field,) = tuple.fields().collect_tuple()?; | ||
129 | let ty = field.ty()?; | ||
130 | ("(v)".to_owned(), ty, "v".to_owned()) | ||
131 | } | ||
132 | ast::StructKind::Unit => return None, | ||
133 | }; | ||
134 | |||
135 | let fn_name = format!("{}_{}", props.fn_name_prefix, &to_lower_snake_case(variant_name.text())); | ||
136 | |||
137 | // Return early if we've found an existing new fn | ||
138 | let impl_def = find_struct_impl(&ctx, &parent_enum, &fn_name)?; | ||
139 | |||
140 | let target = variant.syntax().text_range(); | ||
141 | acc.add(AssistId(assist_id, AssistKind::Generate), assist_description, target, |builder| { | ||
142 | let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v)); | ||
143 | let method = format!( | ||
144 | " {0}fn {1}({2}) -> {3}{4}{5} {{ | ||
145 | if let Self::{6}{7} = self {{ | ||
146 | {8}({9}) | ||
147 | }} else {{ | ||
148 | {10} | ||
149 | }} | ||
150 | }}", | ||
151 | vis, | ||
152 | fn_name, | ||
153 | props.self_param, | ||
154 | props.return_prefix, | ||
155 | field_type.syntax(), | ||
156 | props.return_suffix, | ||
157 | variant_name, | ||
158 | pattern_suffix, | ||
159 | props.happy_case, | ||
160 | bound_name, | ||
161 | props.sad_case, | ||
162 | ); | ||
163 | |||
164 | add_method_to_adt(builder, &parent_enum, impl_def, &method); | ||
165 | }) | ||
166 | } | ||
167 | |||
168 | #[cfg(test)] | ||
169 | mod tests { | ||
170 | use crate::tests::{check_assist, check_assist_not_applicable}; | ||
171 | |||
172 | use super::*; | ||
173 | |||
174 | #[test] | ||
175 | fn test_generate_enum_try_into_tuple_variant() { | ||
176 | check_assist( | ||
177 | generate_enum_try_into_method, | ||
178 | r#" | ||
179 | enum Value { | ||
180 | Number(i32), | ||
181 | Text(String)$0, | ||
182 | }"#, | ||
183 | r#"enum Value { | ||
184 | Number(i32), | ||
185 | Text(String), | ||
186 | } | ||
187 | |||
188 | impl Value { | ||
189 | fn try_into_text(self) -> Result<String, Self> { | ||
190 | if let Self::Text(v) = self { | ||
191 | Ok(v) | ||
192 | } else { | ||
193 | Err(self) | ||
194 | } | ||
195 | } | ||
196 | }"#, | ||
197 | ); | ||
198 | } | ||
199 | |||
200 | #[test] | ||
201 | fn test_generate_enum_try_into_already_implemented() { | ||
202 | check_assist_not_applicable( | ||
203 | generate_enum_try_into_method, | ||
204 | r#"enum Value { | ||
205 | Number(i32), | ||
206 | Text(String)$0, | ||
207 | } | ||
208 | |||
209 | impl Value { | ||
210 | fn try_into_text(self) -> Result<String, Self> { | ||
211 | if let Self::Text(v) = self { | ||
212 | Ok(v) | ||
213 | } else { | ||
214 | Err(self) | ||
215 | } | ||
216 | } | ||
217 | }"#, | ||
218 | ); | ||
219 | } | ||
220 | |||
221 | #[test] | ||
222 | fn test_generate_enum_try_into_unit_variant() { | ||
223 | check_assist_not_applicable( | ||
224 | generate_enum_try_into_method, | ||
225 | r#"enum Value { | ||
226 | Number(i32), | ||
227 | Text(String), | ||
228 | Unit$0, | ||
229 | }"#, | ||
230 | ); | ||
231 | } | ||
232 | |||
233 | #[test] | ||
234 | fn test_generate_enum_try_into_record_with_multiple_fields() { | ||
235 | check_assist_not_applicable( | ||
236 | generate_enum_try_into_method, | ||
237 | r#"enum Value { | ||
238 | Number(i32), | ||
239 | Text(String), | ||
240 | Both { first: i32, second: String }$0, | ||
241 | }"#, | ||
242 | ); | ||
243 | } | ||
244 | |||
245 | #[test] | ||
246 | fn test_generate_enum_try_into_tuple_with_multiple_fields() { | ||
247 | check_assist_not_applicable( | ||
248 | generate_enum_try_into_method, | ||
249 | r#"enum Value { | ||
250 | Number(i32), | ||
251 | Text(String, String)$0, | ||
252 | }"#, | ||
253 | ); | ||
254 | } | ||
255 | |||
256 | #[test] | ||
257 | fn test_generate_enum_try_into_record_variant() { | ||
258 | check_assist( | ||
259 | generate_enum_try_into_method, | ||
260 | r#"enum Value { | ||
261 | Number(i32), | ||
262 | Text { text: String }$0, | ||
263 | }"#, | ||
264 | r#"enum Value { | ||
265 | Number(i32), | ||
266 | Text { text: String }, | ||
267 | } | ||
268 | |||
269 | impl Value { | ||
270 | fn try_into_text(self) -> Result<String, Self> { | ||
271 | if let Self::Text { text } = self { | ||
272 | Ok(text) | ||
273 | } else { | ||
274 | Err(self) | ||
275 | } | ||
276 | } | ||
277 | }"#, | ||
278 | ); | ||
279 | } | ||
280 | |||
281 | #[test] | ||
282 | fn test_generate_enum_as_tuple_variant() { | ||
283 | check_assist( | ||
284 | generate_enum_as_method, | ||
285 | r#" | ||
286 | enum Value { | ||
287 | Number(i32), | ||
288 | Text(String)$0, | ||
289 | }"#, | ||
290 | r#"enum Value { | ||
291 | Number(i32), | ||
292 | Text(String), | ||
293 | } | ||
294 | |||
295 | impl Value { | ||
296 | fn as_text(&self) -> Option<&String> { | ||
297 | if let Self::Text(v) = self { | ||
298 | Some(v) | ||
299 | } else { | ||
300 | None | ||
301 | } | ||
302 | } | ||
303 | }"#, | ||
304 | ); | ||
305 | } | ||
306 | |||
307 | #[test] | ||
308 | fn test_generate_enum_as_record_variant() { | ||
309 | check_assist( | ||
310 | generate_enum_as_method, | ||
311 | r#"enum Value { | ||
312 | Number(i32), | ||
313 | Text { text: String }$0, | ||
314 | }"#, | ||
315 | r#"enum Value { | ||
316 | Number(i32), | ||
317 | Text { text: String }, | ||
318 | } | ||
319 | |||
320 | impl Value { | ||
321 | fn as_text(&self) -> Option<&String> { | ||
322 | if let Self::Text { text } = self { | ||
323 | Some(text) | ||
324 | } else { | ||
325 | None | ||
326 | } | ||
327 | } | ||
328 | }"#, | ||
329 | ); | ||
330 | } | ||
331 | } | ||
diff --git a/crates/ide_assists/src/handlers/invert_if.rs b/crates/ide_assists/src/handlers/invert_if.rs index 5b69dafd4..b131dc205 100644 --- a/crates/ide_assists/src/handlers/invert_if.rs +++ b/crates/ide_assists/src/handlers/invert_if.rs | |||
@@ -50,7 +50,7 @@ pub(crate) fn invert_if(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | |||
50 | }; | 50 | }; |
51 | 51 | ||
52 | acc.add(AssistId("invert_if", AssistKind::RefactorRewrite), "Invert if", if_range, |edit| { | 52 | acc.add(AssistId("invert_if", AssistKind::RefactorRewrite), "Invert if", if_range, |edit| { |
53 | let flip_cond = invert_boolean_expression(cond.clone()); | 53 | let flip_cond = invert_boolean_expression(&ctx.sema, cond.clone()); |
54 | edit.replace_ast(cond, flip_cond); | 54 | edit.replace_ast(cond, flip_cond); |
55 | 55 | ||
56 | let else_node = else_block.syntax(); | 56 | let else_node = else_block.syntax(); |
diff --git a/crates/ide_assists/src/handlers/replace_for_loop_with_for_each.rs b/crates/ide_assists/src/handlers/replace_for_loop_with_for_each.rs new file mode 100644 index 000000000..27da28bc0 --- /dev/null +++ b/crates/ide_assists/src/handlers/replace_for_loop_with_for_each.rs | |||
@@ -0,0 +1,321 @@ | |||
1 | use ast::LoopBodyOwner; | ||
2 | use hir::known; | ||
3 | use ide_db::helpers::FamousDefs; | ||
4 | use stdx::format_to; | ||
5 | use syntax::{ast, AstNode}; | ||
6 | use test_utils::mark; | ||
7 | |||
8 | use crate::{AssistContext, AssistId, AssistKind, Assists}; | ||
9 | |||
10 | // Assist: replace_for_loop_with_for_each | ||
11 | // | ||
12 | // Converts a for loop into a for_each loop on the Iterator. | ||
13 | // | ||
14 | // ``` | ||
15 | // fn main() { | ||
16 | // let x = vec![1, 2, 3]; | ||
17 | // for$0 v in x { | ||
18 | // let y = v * 2; | ||
19 | // } | ||
20 | // } | ||
21 | // ``` | ||
22 | // -> | ||
23 | // ``` | ||
24 | // fn main() { | ||
25 | // let x = vec![1, 2, 3]; | ||
26 | // x.into_iter().for_each(|v| { | ||
27 | // let y = v * 2; | ||
28 | // }); | ||
29 | // } | ||
30 | // ``` | ||
31 | pub(crate) fn replace_for_loop_with_for_each(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | ||
32 | let for_loop = ctx.find_node_at_offset::<ast::ForExpr>()?; | ||
33 | let iterable = for_loop.iterable()?; | ||
34 | let pat = for_loop.pat()?; | ||
35 | let body = for_loop.loop_body()?; | ||
36 | if body.syntax().text_range().start() < ctx.offset() { | ||
37 | mark::hit!(not_available_in_body); | ||
38 | return None; | ||
39 | } | ||
40 | |||
41 | acc.add( | ||
42 | AssistId("replace_for_loop_with_for_each", AssistKind::RefactorRewrite), | ||
43 | "Replace this for loop with `Iterator::for_each`", | ||
44 | for_loop.syntax().text_range(), | ||
45 | |builder| { | ||
46 | let mut buf = String::new(); | ||
47 | |||
48 | if let Some((expr_behind_ref, method)) = | ||
49 | is_ref_and_impls_iter_method(&ctx.sema, &iterable) | ||
50 | { | ||
51 | // We have either "for x in &col" and col implements a method called iter | ||
52 | // or "for x in &mut col" and col implements a method called iter_mut | ||
53 | format_to!(buf, "{}.{}()", expr_behind_ref, method); | ||
54 | } else if impls_core_iter(&ctx.sema, &iterable) { | ||
55 | format_to!(buf, "{}", iterable); | ||
56 | } else { | ||
57 | if let ast::Expr::RefExpr(_) = iterable { | ||
58 | format_to!(buf, "({}).into_iter()", iterable); | ||
59 | } else { | ||
60 | format_to!(buf, "{}.into_iter()", iterable); | ||
61 | } | ||
62 | } | ||
63 | |||
64 | format_to!(buf, ".for_each(|{}| {});", pat, body); | ||
65 | |||
66 | builder.replace(for_loop.syntax().text_range(), buf) | ||
67 | }, | ||
68 | ) | ||
69 | } | ||
70 | |||
71 | /// If iterable is a reference where the expression behind the reference implements a method | ||
72 | /// returning an Iterator called iter or iter_mut (depending on the type of reference) then return | ||
73 | /// the expression behind the reference and the method name | ||
74 | fn is_ref_and_impls_iter_method( | ||
75 | sema: &hir::Semantics<ide_db::RootDatabase>, | ||
76 | iterable: &ast::Expr, | ||
77 | ) -> Option<(ast::Expr, hir::Name)> { | ||
78 | let ref_expr = match iterable { | ||
79 | ast::Expr::RefExpr(r) => r, | ||
80 | _ => return None, | ||
81 | }; | ||
82 | let wanted_method = if ref_expr.mut_token().is_some() { known::iter_mut } else { known::iter }; | ||
83 | let expr_behind_ref = ref_expr.expr()?; | ||
84 | let typ = sema.type_of_expr(&expr_behind_ref)?; | ||
85 | let scope = sema.scope(iterable.syntax()); | ||
86 | let krate = scope.module()?.krate(); | ||
87 | let traits_in_scope = scope.traits_in_scope(); | ||
88 | let iter_trait = FamousDefs(sema, Some(krate)).core_iter_Iterator()?; | ||
89 | let has_wanted_method = typ.iterate_method_candidates( | ||
90 | sema.db, | ||
91 | krate, | ||
92 | &traits_in_scope, | ||
93 | Some(&wanted_method), | ||
94 | |_, func| { | ||
95 | if func.ret_type(sema.db).impls_trait(sema.db, iter_trait, &[]) { | ||
96 | return Some(()); | ||
97 | } | ||
98 | None | ||
99 | }, | ||
100 | ); | ||
101 | has_wanted_method.and(Some((expr_behind_ref, wanted_method))) | ||
102 | } | ||
103 | |||
104 | /// Whether iterable implements core::Iterator | ||
105 | fn impls_core_iter(sema: &hir::Semantics<ide_db::RootDatabase>, iterable: &ast::Expr) -> bool { | ||
106 | let it_typ = if let Some(i) = sema.type_of_expr(iterable) { | ||
107 | i | ||
108 | } else { | ||
109 | return false; | ||
110 | }; | ||
111 | let module = if let Some(m) = sema.scope(iterable.syntax()).module() { | ||
112 | m | ||
113 | } else { | ||
114 | return false; | ||
115 | }; | ||
116 | let krate = module.krate(); | ||
117 | if let Some(iter_trait) = FamousDefs(sema, Some(krate)).core_iter_Iterator() { | ||
118 | return it_typ.impls_trait(sema.db, iter_trait, &[]); | ||
119 | } | ||
120 | false | ||
121 | } | ||
122 | |||
123 | #[cfg(test)] | ||
124 | mod tests { | ||
125 | use crate::tests::{check_assist, check_assist_not_applicable}; | ||
126 | |||
127 | use super::*; | ||
128 | |||
129 | const EMPTY_ITER_FIXTURE: &'static str = r" | ||
130 | //- /lib.rs deps:core crate:empty_iter | ||
131 | pub struct EmptyIter; | ||
132 | impl Iterator for EmptyIter { | ||
133 | type Item = usize; | ||
134 | fn next(&mut self) -> Option<Self::Item> { None } | ||
135 | } | ||
136 | |||
137 | pub struct Empty; | ||
138 | impl Empty { | ||
139 | pub fn iter(&self) -> EmptyIter { EmptyIter } | ||
140 | pub fn iter_mut(&self) -> EmptyIter { EmptyIter } | ||
141 | } | ||
142 | |||
143 | pub struct NoIterMethod; | ||
144 | "; | ||
145 | |||
146 | fn check_assist_with_fixtures(before: &str, after: &str) { | ||
147 | let before = &format!( | ||
148 | "//- /main.rs crate:main deps:core,empty_iter{}{}{}", | ||
149 | before, | ||
150 | FamousDefs::FIXTURE, | ||
151 | EMPTY_ITER_FIXTURE | ||
152 | ); | ||
153 | check_assist(replace_for_loop_with_for_each, before, after); | ||
154 | } | ||
155 | |||
156 | #[test] | ||
157 | fn test_not_for() { | ||
158 | check_assist_not_applicable( | ||
159 | replace_for_loop_with_for_each, | ||
160 | r" | ||
161 | let mut x = vec![1, 2, 3]; | ||
162 | x.iter_mut().$0for_each(|v| *v *= 2); | ||
163 | ", | ||
164 | ) | ||
165 | } | ||
166 | |||
167 | #[test] | ||
168 | fn test_simple_for() { | ||
169 | check_assist( | ||
170 | replace_for_loop_with_for_each, | ||
171 | r" | ||
172 | fn main() { | ||
173 | let x = vec![1, 2, 3]; | ||
174 | for $0v in x { | ||
175 | v *= 2; | ||
176 | } | ||
177 | }", | ||
178 | r" | ||
179 | fn main() { | ||
180 | let x = vec![1, 2, 3]; | ||
181 | x.into_iter().for_each(|v| { | ||
182 | v *= 2; | ||
183 | }); | ||
184 | }", | ||
185 | ) | ||
186 | } | ||
187 | |||
188 | #[test] | ||
189 | fn not_available_in_body() { | ||
190 | mark::check!(not_available_in_body); | ||
191 | check_assist_not_applicable( | ||
192 | replace_for_loop_with_for_each, | ||
193 | r" | ||
194 | fn main() { | ||
195 | let x = vec![1, 2, 3]; | ||
196 | for v in x { | ||
197 | $0v *= 2; | ||
198 | } | ||
199 | }", | ||
200 | ) | ||
201 | } | ||
202 | |||
203 | #[test] | ||
204 | fn test_for_borrowed() { | ||
205 | check_assist_with_fixtures( | ||
206 | r" | ||
207 | use empty_iter::*; | ||
208 | fn main() { | ||
209 | let x = Empty; | ||
210 | for $0v in &x { | ||
211 | let a = v * 2; | ||
212 | } | ||
213 | } | ||
214 | ", | ||
215 | r" | ||
216 | use empty_iter::*; | ||
217 | fn main() { | ||
218 | let x = Empty; | ||
219 | x.iter().for_each(|v| { | ||
220 | let a = v * 2; | ||
221 | }); | ||
222 | } | ||
223 | ", | ||
224 | ) | ||
225 | } | ||
226 | |||
227 | #[test] | ||
228 | fn test_for_borrowed_no_iter_method() { | ||
229 | check_assist_with_fixtures( | ||
230 | r" | ||
231 | use empty_iter::*; | ||
232 | fn main() { | ||
233 | let x = NoIterMethod; | ||
234 | for $0v in &x { | ||
235 | let a = v * 2; | ||
236 | } | ||
237 | } | ||
238 | ", | ||
239 | r" | ||
240 | use empty_iter::*; | ||
241 | fn main() { | ||
242 | let x = NoIterMethod; | ||
243 | (&x).into_iter().for_each(|v| { | ||
244 | let a = v * 2; | ||
245 | }); | ||
246 | } | ||
247 | ", | ||
248 | ) | ||
249 | } | ||
250 | |||
251 | #[test] | ||
252 | fn test_for_borrowed_mut() { | ||
253 | check_assist_with_fixtures( | ||
254 | r" | ||
255 | use empty_iter::*; | ||
256 | fn main() { | ||
257 | let x = Empty; | ||
258 | for $0v in &mut x { | ||
259 | let a = v * 2; | ||
260 | } | ||
261 | } | ||
262 | ", | ||
263 | r" | ||
264 | use empty_iter::*; | ||
265 | fn main() { | ||
266 | let x = Empty; | ||
267 | x.iter_mut().for_each(|v| { | ||
268 | let a = v * 2; | ||
269 | }); | ||
270 | } | ||
271 | ", | ||
272 | ) | ||
273 | } | ||
274 | |||
275 | #[test] | ||
276 | fn test_for_borrowed_mut_behind_var() { | ||
277 | check_assist( | ||
278 | replace_for_loop_with_for_each, | ||
279 | r" | ||
280 | fn main() { | ||
281 | let x = vec![1, 2, 3]; | ||
282 | let y = &mut x; | ||
283 | for $0v in y { | ||
284 | *v *= 2; | ||
285 | } | ||
286 | }", | ||
287 | r" | ||
288 | fn main() { | ||
289 | let x = vec![1, 2, 3]; | ||
290 | let y = &mut x; | ||
291 | y.into_iter().for_each(|v| { | ||
292 | *v *= 2; | ||
293 | }); | ||
294 | }", | ||
295 | ) | ||
296 | } | ||
297 | |||
298 | #[test] | ||
299 | fn test_already_impls_iterator() { | ||
300 | check_assist_with_fixtures( | ||
301 | r#" | ||
302 | use empty_iter::*; | ||
303 | fn main() { | ||
304 | let x = Empty; | ||
305 | for$0 a in x.iter().take(1) { | ||
306 | println!("{}", a); | ||
307 | } | ||
308 | } | ||
309 | "#, | ||
310 | r#" | ||
311 | use empty_iter::*; | ||
312 | fn main() { | ||
313 | let x = Empty; | ||
314 | x.iter().take(1).for_each(|a| { | ||
315 | println!("{}", a); | ||
316 | }); | ||
317 | } | ||
318 | "#, | ||
319 | ); | ||
320 | } | ||
321 | } | ||
diff --git a/crates/ide_assists/src/handlers/replace_let_with_if_let.rs b/crates/ide_assists/src/handlers/replace_let_with_if_let.rs index 5a27ada6b..be7e724b5 100644 --- a/crates/ide_assists/src/handlers/replace_let_with_if_let.rs +++ b/crates/ide_assists/src/handlers/replace_let_with_if_let.rs | |||
@@ -1,5 +1,6 @@ | |||
1 | use std::iter::once; | 1 | use std::iter::once; |
2 | 2 | ||
3 | use ide_db::ty_filter::TryEnum; | ||
3 | use syntax::{ | 4 | use syntax::{ |
4 | ast::{ | 5 | ast::{ |
5 | self, | 6 | self, |
@@ -10,7 +11,6 @@ use syntax::{ | |||
10 | }; | 11 | }; |
11 | 12 | ||
12 | use crate::{AssistContext, AssistId, AssistKind, Assists}; | 13 | use crate::{AssistContext, AssistId, AssistKind, Assists}; |
13 | use ide_db::ty_filter::TryEnum; | ||
14 | 14 | ||
15 | // Assist: replace_let_with_if_let | 15 | // Assist: replace_let_with_if_let |
16 | // | 16 | // |
diff --git a/crates/ide_assists/src/lib.rs b/crates/ide_assists/src/lib.rs index b9799b9d5..9c8148462 100644 --- a/crates/ide_assists/src/lib.rs +++ b/crates/ide_assists/src/lib.rs | |||
@@ -128,11 +128,12 @@ mod handlers { | |||
128 | mod flip_trait_bound; | 128 | mod flip_trait_bound; |
129 | mod generate_default_from_enum_variant; | 129 | mod generate_default_from_enum_variant; |
130 | mod generate_derive; | 130 | mod generate_derive; |
131 | mod generate_enum_match_method; | 131 | mod generate_enum_is_method; |
132 | mod generate_enum_projection_method; | ||
132 | mod generate_from_impl_for_enum; | 133 | mod generate_from_impl_for_enum; |
133 | mod generate_function; | 134 | mod generate_function; |
134 | mod generate_getter; | ||
135 | mod generate_getter_mut; | 135 | mod generate_getter_mut; |
136 | mod generate_getter; | ||
136 | mod generate_impl; | 137 | mod generate_impl; |
137 | mod generate_new; | 138 | mod generate_new; |
138 | mod generate_setter; | 139 | mod generate_setter; |
@@ -155,6 +156,7 @@ mod handlers { | |||
155 | mod reorder_fields; | 156 | mod reorder_fields; |
156 | mod reorder_impl; | 157 | mod reorder_impl; |
157 | mod replace_derive_with_manual_impl; | 158 | mod replace_derive_with_manual_impl; |
159 | mod replace_for_loop_with_for_each; | ||
158 | mod replace_if_let_with_match; | 160 | mod replace_if_let_with_match; |
159 | mod replace_impl_trait_with_generic; | 161 | mod replace_impl_trait_with_generic; |
160 | mod replace_let_with_if_let; | 162 | mod replace_let_with_if_let; |
@@ -180,7 +182,6 @@ mod handlers { | |||
180 | convert_comment_block::convert_comment_block, | 182 | convert_comment_block::convert_comment_block, |
181 | early_return::convert_to_guarded_return, | 183 | early_return::convert_to_guarded_return, |
182 | expand_glob_import::expand_glob_import, | 184 | expand_glob_import::expand_glob_import, |
183 | move_module_to_file::move_module_to_file, | ||
184 | extract_struct_from_enum_variant::extract_struct_from_enum_variant, | 185 | extract_struct_from_enum_variant::extract_struct_from_enum_variant, |
185 | fill_match_arms::fill_match_arms, | 186 | fill_match_arms::fill_match_arms, |
186 | fix_visibility::fix_visibility, | 187 | fix_visibility::fix_visibility, |
@@ -189,11 +190,13 @@ mod handlers { | |||
189 | flip_trait_bound::flip_trait_bound, | 190 | flip_trait_bound::flip_trait_bound, |
190 | generate_default_from_enum_variant::generate_default_from_enum_variant, | 191 | generate_default_from_enum_variant::generate_default_from_enum_variant, |
191 | generate_derive::generate_derive, | 192 | generate_derive::generate_derive, |
192 | generate_enum_match_method::generate_enum_match_method, | 193 | generate_enum_is_method::generate_enum_is_method, |
194 | generate_enum_projection_method::generate_enum_as_method, | ||
195 | generate_enum_projection_method::generate_enum_try_into_method, | ||
193 | generate_from_impl_for_enum::generate_from_impl_for_enum, | 196 | generate_from_impl_for_enum::generate_from_impl_for_enum, |
194 | generate_function::generate_function, | 197 | generate_function::generate_function, |
195 | generate_getter::generate_getter, | ||
196 | generate_getter_mut::generate_getter_mut, | 198 | generate_getter_mut::generate_getter_mut, |
199 | generate_getter::generate_getter, | ||
197 | generate_impl::generate_impl, | 200 | generate_impl::generate_impl, |
198 | generate_new::generate_new, | 201 | generate_new::generate_new, |
199 | generate_setter::generate_setter, | 202 | generate_setter::generate_setter, |
@@ -207,6 +210,7 @@ mod handlers { | |||
207 | move_bounds::move_bounds_to_where_clause, | 210 | move_bounds::move_bounds_to_where_clause, |
208 | move_guard::move_arm_cond_to_match_guard, | 211 | move_guard::move_arm_cond_to_match_guard, |
209 | move_guard::move_guard_to_arm_body, | 212 | move_guard::move_guard_to_arm_body, |
213 | move_module_to_file::move_module_to_file, | ||
210 | pull_assignment_up::pull_assignment_up, | 214 | pull_assignment_up::pull_assignment_up, |
211 | qualify_path::qualify_path, | 215 | qualify_path::qualify_path, |
212 | raw_string::add_hash, | 216 | raw_string::add_hash, |
@@ -218,6 +222,7 @@ mod handlers { | |||
218 | reorder_fields::reorder_fields, | 222 | reorder_fields::reorder_fields, |
219 | reorder_impl::reorder_impl, | 223 | reorder_impl::reorder_impl, |
220 | replace_derive_with_manual_impl::replace_derive_with_manual_impl, | 224 | replace_derive_with_manual_impl::replace_derive_with_manual_impl, |
225 | replace_for_loop_with_for_each::replace_for_loop_with_for_each, | ||
221 | replace_if_let_with_match::replace_if_let_with_match, | 226 | replace_if_let_with_match::replace_if_let_with_match, |
222 | replace_if_let_with_match::replace_match_with_if_let, | 227 | replace_if_let_with_match::replace_match_with_if_let, |
223 | replace_impl_trait_with_generic::replace_impl_trait_with_generic, | 228 | replace_impl_trait_with_generic::replace_impl_trait_with_generic, |
diff --git a/crates/ide_assists/src/tests.rs b/crates/ide_assists/src/tests.rs index 384eb7eee..b7f616760 100644 --- a/crates/ide_assists/src/tests.rs +++ b/crates/ide_assists/src/tests.rs | |||
@@ -190,8 +190,8 @@ fn assist_order_field_struct() { | |||
190 | let mut assists = assists.iter(); | 190 | let mut assists = assists.iter(); |
191 | 191 | ||
192 | assert_eq!(assists.next().expect("expected assist").label, "Change visibility to pub(crate)"); | 192 | assert_eq!(assists.next().expect("expected assist").label, "Change visibility to pub(crate)"); |
193 | assert_eq!(assists.next().expect("expected assist").label, "Generate a getter method"); | ||
194 | assert_eq!(assists.next().expect("expected assist").label, "Generate a mut getter method"); | 193 | assert_eq!(assists.next().expect("expected assist").label, "Generate a mut getter method"); |
194 | assert_eq!(assists.next().expect("expected assist").label, "Generate a getter method"); | ||
195 | assert_eq!(assists.next().expect("expected assist").label, "Generate a setter method"); | 195 | assert_eq!(assists.next().expect("expected assist").label, "Generate a setter method"); |
196 | assert_eq!(assists.next().expect("expected assist").label, "Add `#[derive]`"); | 196 | assert_eq!(assists.next().expect("expected assist").label, "Add `#[derive]`"); |
197 | } | 197 | } |
diff --git a/crates/ide_assists/src/tests/generated.rs b/crates/ide_assists/src/tests/generated.rs index 0516deaff..4f007aa48 100644 --- a/crates/ide_assists/src/tests/generated.rs +++ b/crates/ide_assists/src/tests/generated.rs | |||
@@ -147,12 +147,12 @@ fn doctest_apply_demorgan() { | |||
147 | "apply_demorgan", | 147 | "apply_demorgan", |
148 | r#####" | 148 | r#####" |
149 | fn main() { | 149 | fn main() { |
150 | if x != 4 ||$0 !y {} | 150 | if x != 4 ||$0 y < 3.14 {} |
151 | } | 151 | } |
152 | "#####, | 152 | "#####, |
153 | r#####" | 153 | r#####" |
154 | fn main() { | 154 | fn main() { |
155 | if !(x == 4 && y) {} | 155 | if !(x == 4 && !(y < 3.14)) {} |
156 | } | 156 | } |
157 | "#####, | 157 | "#####, |
158 | ) | 158 | ) |
@@ -460,9 +460,38 @@ struct Point { | |||
460 | } | 460 | } |
461 | 461 | ||
462 | #[test] | 462 | #[test] |
463 | fn doctest_generate_enum_match_method() { | 463 | fn doctest_generate_enum_as_method() { |
464 | check_doc_test( | 464 | check_doc_test( |
465 | "generate_enum_match_method", | 465 | "generate_enum_as_method", |
466 | r#####" | ||
467 | enum Value { | ||
468 | Number(i32), | ||
469 | Text(String)$0, | ||
470 | } | ||
471 | "#####, | ||
472 | r#####" | ||
473 | enum Value { | ||
474 | Number(i32), | ||
475 | Text(String), | ||
476 | } | ||
477 | |||
478 | impl Value { | ||
479 | fn as_text(&self) -> Option<&String> { | ||
480 | if let Self::Text(v) = self { | ||
481 | Some(v) | ||
482 | } else { | ||
483 | None | ||
484 | } | ||
485 | } | ||
486 | } | ||
487 | "#####, | ||
488 | ) | ||
489 | } | ||
490 | |||
491 | #[test] | ||
492 | fn doctest_generate_enum_is_method() { | ||
493 | check_doc_test( | ||
494 | "generate_enum_is_method", | ||
466 | r#####" | 495 | r#####" |
467 | enum Version { | 496 | enum Version { |
468 | Undefined, | 497 | Undefined, |
@@ -488,6 +517,35 @@ impl Version { | |||
488 | } | 517 | } |
489 | 518 | ||
490 | #[test] | 519 | #[test] |
520 | fn doctest_generate_enum_try_into_method() { | ||
521 | check_doc_test( | ||
522 | "generate_enum_try_into_method", | ||
523 | r#####" | ||
524 | enum Value { | ||
525 | Number(i32), | ||
526 | Text(String)$0, | ||
527 | } | ||
528 | "#####, | ||
529 | r#####" | ||
530 | enum Value { | ||
531 | Number(i32), | ||
532 | Text(String), | ||
533 | } | ||
534 | |||
535 | impl Value { | ||
536 | fn try_into_text(self) -> Result<String, Self> { | ||
537 | if let Self::Text(v) = self { | ||
538 | Ok(v) | ||
539 | } else { | ||
540 | Err(self) | ||
541 | } | ||
542 | } | ||
543 | } | ||
544 | "#####, | ||
545 | ) | ||
546 | } | ||
547 | |||
548 | #[test] | ||
491 | fn doctest_generate_from_impl_for_enum() { | 549 | fn doctest_generate_from_impl_for_enum() { |
492 | check_doc_test( | 550 | check_doc_test( |
493 | "generate_from_impl_for_enum", | 551 | "generate_from_impl_for_enum", |
@@ -1099,6 +1157,29 @@ impl Debug for S { | |||
1099 | } | 1157 | } |
1100 | 1158 | ||
1101 | #[test] | 1159 | #[test] |
1160 | fn doctest_replace_for_loop_with_for_each() { | ||
1161 | check_doc_test( | ||
1162 | "replace_for_loop_with_for_each", | ||
1163 | r#####" | ||
1164 | fn main() { | ||
1165 | let x = vec![1, 2, 3]; | ||
1166 | for$0 v in x { | ||
1167 | let y = v * 2; | ||
1168 | } | ||
1169 | } | ||
1170 | "#####, | ||
1171 | r#####" | ||
1172 | fn main() { | ||
1173 | let x = vec![1, 2, 3]; | ||
1174 | x.into_iter().for_each(|v| { | ||
1175 | let y = v * 2; | ||
1176 | }); | ||
1177 | } | ||
1178 | "#####, | ||
1179 | ) | ||
1180 | } | ||
1181 | |||
1182 | #[test] | ||
1102 | fn doctest_replace_if_let_with_match() { | 1183 | fn doctest_replace_if_let_with_match() { |
1103 | check_doc_test( | 1184 | check_doc_test( |
1104 | "replace_if_let_with_match", | 1185 | "replace_if_let_with_match", |
diff --git a/crates/ide_assists/src/utils.rs b/crates/ide_assists/src/utils.rs index 0074da741..880ab6fe3 100644 --- a/crates/ide_assists/src/utils.rs +++ b/crates/ide_assists/src/utils.rs | |||
@@ -3,8 +3,11 @@ | |||
3 | use std::ops; | 3 | use std::ops; |
4 | 4 | ||
5 | use ast::TypeBoundsOwner; | 5 | use ast::TypeBoundsOwner; |
6 | use hir::{Adt, HasSource}; | 6 | use hir::{Adt, HasSource, Semantics}; |
7 | use ide_db::{helpers::SnippetCap, RootDatabase}; | 7 | use ide_db::{ |
8 | helpers::{FamousDefs, SnippetCap}, | ||
9 | RootDatabase, | ||
10 | }; | ||
8 | use itertools::Itertools; | 11 | use itertools::Itertools; |
9 | use stdx::format_to; | 12 | use stdx::format_to; |
10 | use syntax::{ | 13 | use syntax::{ |
@@ -18,7 +21,7 @@ use syntax::{ | |||
18 | }; | 21 | }; |
19 | 22 | ||
20 | use crate::{ | 23 | use crate::{ |
21 | assist_context::AssistContext, | 24 | assist_context::{AssistBuilder, AssistContext}, |
22 | ast_transform::{self, AstTransform, QualifyPaths, SubstituteTypeParams}, | 25 | ast_transform::{self, AstTransform, QualifyPaths, SubstituteTypeParams}, |
23 | }; | 26 | }; |
24 | 27 | ||
@@ -205,23 +208,36 @@ pub(crate) fn vis_offset(node: &SyntaxNode) -> TextSize { | |||
205 | .unwrap_or_else(|| node.text_range().start()) | 208 | .unwrap_or_else(|| node.text_range().start()) |
206 | } | 209 | } |
207 | 210 | ||
208 | pub(crate) fn invert_boolean_expression(expr: ast::Expr) -> ast::Expr { | 211 | pub(crate) fn invert_boolean_expression( |
209 | if let Some(expr) = invert_special_case(&expr) { | 212 | sema: &Semantics<RootDatabase>, |
213 | expr: ast::Expr, | ||
214 | ) -> ast::Expr { | ||
215 | if let Some(expr) = invert_special_case(sema, &expr) { | ||
210 | return expr; | 216 | return expr; |
211 | } | 217 | } |
212 | make::expr_prefix(T![!], expr) | 218 | make::expr_prefix(T![!], expr) |
213 | } | 219 | } |
214 | 220 | ||
215 | fn invert_special_case(expr: &ast::Expr) -> Option<ast::Expr> { | 221 | fn invert_special_case(sema: &Semantics<RootDatabase>, expr: &ast::Expr) -> Option<ast::Expr> { |
216 | match expr { | 222 | match expr { |
217 | ast::Expr::BinExpr(bin) => match bin.op_kind()? { | 223 | ast::Expr::BinExpr(bin) => match bin.op_kind()? { |
218 | ast::BinOp::NegatedEqualityTest => bin.replace_op(T![==]).map(|it| it.into()), | 224 | ast::BinOp::NegatedEqualityTest => bin.replace_op(T![==]).map(|it| it.into()), |
219 | ast::BinOp::EqualityTest => bin.replace_op(T![!=]).map(|it| it.into()), | 225 | ast::BinOp::EqualityTest => bin.replace_op(T![!=]).map(|it| it.into()), |
220 | // Parenthesize composite boolean expressions before prefixing `!` | 226 | // Swap `<` with `>=`, `<=` with `>`, ... if operands `impl Ord` |
221 | ast::BinOp::BooleanAnd | ast::BinOp::BooleanOr => { | 227 | ast::BinOp::LesserTest if bin_impls_ord(sema, bin) => { |
222 | Some(make::expr_prefix(T![!], make::expr_paren(expr.clone()))) | 228 | bin.replace_op(T![>=]).map(|it| it.into()) |
229 | } | ||
230 | ast::BinOp::LesserEqualTest if bin_impls_ord(sema, bin) => { | ||
231 | bin.replace_op(T![>]).map(|it| it.into()) | ||
232 | } | ||
233 | ast::BinOp::GreaterTest if bin_impls_ord(sema, bin) => { | ||
234 | bin.replace_op(T![<=]).map(|it| it.into()) | ||
235 | } | ||
236 | ast::BinOp::GreaterEqualTest if bin_impls_ord(sema, bin) => { | ||
237 | bin.replace_op(T![<]).map(|it| it.into()) | ||
223 | } | 238 | } |
224 | _ => None, | 239 | // Parenthesize other expressions before prefixing `!` |
240 | _ => Some(make::expr_prefix(T![!], make::expr_paren(expr.clone()))), | ||
225 | }, | 241 | }, |
226 | ast::Expr::MethodCallExpr(mce) => { | 242 | ast::Expr::MethodCallExpr(mce) => { |
227 | let receiver = mce.receiver()?; | 243 | let receiver = mce.receiver()?; |
@@ -250,6 +266,22 @@ fn invert_special_case(expr: &ast::Expr) -> Option<ast::Expr> { | |||
250 | } | 266 | } |
251 | } | 267 | } |
252 | 268 | ||
269 | fn bin_impls_ord(sema: &Semantics<RootDatabase>, bin: &ast::BinExpr) -> bool { | ||
270 | match ( | ||
271 | bin.lhs().and_then(|lhs| sema.type_of_expr(&lhs)), | ||
272 | bin.rhs().and_then(|rhs| sema.type_of_expr(&rhs)), | ||
273 | ) { | ||
274 | (Some(lhs_ty), Some(rhs_ty)) if lhs_ty == rhs_ty => { | ||
275 | let krate = sema.scope(bin.syntax()).module().map(|it| it.krate()); | ||
276 | let ord_trait = FamousDefs(sema, krate).core_cmp_Ord(); | ||
277 | ord_trait.map_or(false, |ord_trait| { | ||
278 | lhs_ty.autoderef(sema.db).any(|ty| ty.impls_trait(sema.db, ord_trait, &[])) | ||
279 | }) | ||
280 | } | ||
281 | _ => false, | ||
282 | } | ||
283 | } | ||
284 | |||
253 | pub(crate) fn next_prev() -> impl Iterator<Item = Direction> { | 285 | pub(crate) fn next_prev() -> impl Iterator<Item = Direction> { |
254 | [Direction::Next, Direction::Prev].iter().copied() | 286 | [Direction::Next, Direction::Prev].iter().copied() |
255 | } | 287 | } |
@@ -432,3 +464,25 @@ fn generate_impl_text_inner(adt: &ast::Adt, trait_text: Option<&str>, code: &str | |||
432 | 464 | ||
433 | buf | 465 | buf |
434 | } | 466 | } |
467 | |||
468 | pub(crate) fn add_method_to_adt( | ||
469 | builder: &mut AssistBuilder, | ||
470 | adt: &ast::Adt, | ||
471 | impl_def: Option<ast::Impl>, | ||
472 | method: &str, | ||
473 | ) { | ||
474 | let mut buf = String::with_capacity(method.len() + 2); | ||
475 | if impl_def.is_some() { | ||
476 | buf.push('\n'); | ||
477 | } | ||
478 | buf.push_str(method); | ||
479 | |||
480 | let start_offset = impl_def | ||
481 | .and_then(|impl_def| find_impl_block_end(impl_def, &mut buf)) | ||
482 | .unwrap_or_else(|| { | ||
483 | buf = generate_impl_text(&adt, &buf); | ||
484 | adt.syntax().text_range().end() | ||
485 | }); | ||
486 | |||
487 | builder.insert(start_offset, buf); | ||
488 | } | ||
diff --git a/crates/ide_completion/src/generated_lint_completions.rs b/crates/ide_completion/src/generated_lint_completions.rs index 87df7f1c9..0d405350d 100644 --- a/crates/ide_completion/src/generated_lint_completions.rs +++ b/crates/ide_completion/src/generated_lint_completions.rs | |||
@@ -1,5 +1,6380 @@ | |||
1 | //! Generated file, do not edit by hand, see `xtask/src/codegen` | 1 | //! Generated file, do not edit by hand, see `xtask/src/codegen` |
2 | 2 | ||
3 | use crate::completions::attribute::LintCompletion; | 3 | use crate::completions::attribute::LintCompletion; |
4 | pub (super) const FEATURES : & [LintCompletion] = & [LintCompletion { label : "non_ascii_idents" , description : "# `non_ascii_idents`\n\nThe tracking issue for this feature is: [#55467]\n\n[#55467]: https://github.com/rust-lang/rust/issues/55467\n\n------------------------\n\nThe `non_ascii_idents` feature adds support for non-ASCII identifiers.\n\n## Examples\n\n```rust\n#![feature(non_ascii_idents)]\n\nconst ε: f64 = 0.00001f64;\nconst Π: f64 = 3.14f64;\n```\n\n## Changes to the language reference\n\n> **<sup>Lexer:<sup>** \n> IDENTIFIER : \n> XID_start XID_continue<sup>\\*</sup> \n> | `_` XID_continue<sup>+</sup> \n\nAn identifier is any nonempty Unicode string of the following form:\n\nEither\n\n * The first character has property [`XID_start`]\n * The remaining characters have property [`XID_continue`]\n\nOr\n\n * The first character is `_`\n * The identifier is more than one character, `_` alone is not an identifier\n * The remaining characters have property [`XID_continue`]\n\nthat does _not_ occur in the set of [strict keywords].\n\n> **Note**: [`XID_start`] and [`XID_continue`] as character properties cover the\n> character ranges used to form the more familiar C and Java language-family\n> identifiers.\n\n[`XID_start`]: http://unicode.org/cldr/utility/list-unicodeset.jsp?a=%5B%3AXID_Start%3A%5D&abb=on&g=&i=\n[`XID_continue`]: http://unicode.org/cldr/utility/list-unicodeset.jsp?a=%5B%3AXID_Continue%3A%5D&abb=on&g=&i=\n[strict keywords]: ../../reference/keywords.md#strict-keywords\n" } , LintCompletion { label : "custom_test_frameworks" , description : "# `custom_test_frameworks`\n\nThe tracking issue for this feature is: [#50297]\n\n[#50297]: https://github.com/rust-lang/rust/issues/50297\n\n------------------------\n\nThe `custom_test_frameworks` feature allows the use of `#[test_case]` and `#![test_runner]`.\nAny function, const, or static can be annotated with `#[test_case]` causing it to be aggregated (like `#[test]`)\nand be passed to the test runner determined by the `#![test_runner]` crate attribute.\n\n```rust\n#![feature(custom_test_frameworks)]\n#![test_runner(my_runner)]\n\nfn my_runner(tests: &[&i32]) {\n for t in tests {\n if **t == 0 {\n println!(\"PASSED\");\n } else {\n println!(\"FAILED\");\n }\n }\n}\n\n#[test_case]\nconst WILL_PASS: i32 = 0;\n\n#[test_case]\nconst WILL_FAIL: i32 = 4;\n```\n\n" } , LintCompletion { label : "abi_msp430_interrupt" , description : "# `abi_msp430_interrupt`\n\nThe tracking issue for this feature is: [#38487]\n\n[#38487]: https://github.com/rust-lang/rust/issues/38487\n\n------------------------\n\nIn the MSP430 architecture, interrupt handlers have a special calling\nconvention. You can use the `\"msp430-interrupt\"` ABI to make the compiler apply\nthe right calling convention to the interrupt handlers you define.\n\n<!-- NOTE(ignore) this example is specific to the msp430 target -->\n\n``` rust,ignore\n#![feature(abi_msp430_interrupt)]\n#![no_std]\n\n// Place the interrupt handler at the appropriate memory address\n// (Alternatively, you can use `#[used]` and remove `pub` and `#[no_mangle]`)\n#[link_section = \"__interrupt_vector_10\"]\n#[no_mangle]\npub static TIM0_VECTOR: extern \"msp430-interrupt\" fn() = tim0;\n\n// The interrupt handler\nextern \"msp430-interrupt\" fn tim0() {\n // ..\n}\n```\n\n``` text\n$ msp430-elf-objdump -CD ./target/msp430/release/app\nDisassembly of section __interrupt_vector_10:\n\n0000fff2 <TIM0_VECTOR>:\n fff2: 00 c0 interrupt service routine at 0xc000\n\nDisassembly of section .text:\n\n0000c000 <int::tim0>:\n c000: 00 13 reti\n```\n" } , LintCompletion { label : "link_args" , description : "# `link_args`\n\nThe tracking issue for this feature is: [#29596]\n\n[#29596]: https://github.com/rust-lang/rust/issues/29596\n\n------------------------\n\nYou can tell `rustc` how to customize linking, and that is via the `link_args`\nattribute. This attribute is applied to `extern` blocks and specifies raw flags\nwhich need to get passed to the linker when producing an artifact. An example\nusage would be:\n\n```rust,no_run\n#![feature(link_args)]\n\n#[link_args = \"-foo -bar -baz\"]\nextern {}\n# fn main() {}\n```\n\nNote that this feature is currently hidden behind the `feature(link_args)` gate\nbecause this is not a sanctioned way of performing linking. Right now `rustc`\nshells out to the system linker (`gcc` on most systems, `link.exe` on MSVC), so\nit makes sense to provide extra command line arguments, but this will not\nalways be the case. In the future `rustc` may use LLVM directly to link native\nlibraries, in which case `link_args` will have no meaning. You can achieve the\nsame effect as the `link_args` attribute with the `-C link-args` argument to\n`rustc`.\n\nIt is highly recommended to *not* use this attribute, and rather use the more\nformal `#[link(...)]` attribute on `extern` blocks instead.\n" } , LintCompletion { label : "const_eval_limit" , description : "# `const_eval_limit`\n\nThe tracking issue for this feature is: [#67217]\n\n[#67217]: https://github.com/rust-lang/rust/issues/67217\n\nThe `const_eval_limit` allows someone to limit the evaluation steps the CTFE undertakes to evaluate a `const fn`.\n" } , LintCompletion { label : "marker_trait_attr" , description : "# `marker_trait_attr`\n\nThe tracking issue for this feature is: [#29864]\n\n[#29864]: https://github.com/rust-lang/rust/issues/29864\n\n------------------------\n\nNormally, Rust keeps you from adding trait implementations that could\noverlap with each other, as it would be ambiguous which to use. This\nfeature, however, carves out an exception to that rule: a trait can\nopt-in to having overlapping implementations, at the cost that those\nimplementations are not allowed to override anything (and thus the\ntrait itself cannot have any associated items, as they're pointless\nwhen they'd need to do the same thing for every type anyway).\n\n```rust\n#![feature(marker_trait_attr)]\n\n#[marker] trait CheapToClone: Clone {}\n\nimpl<T: Copy> CheapToClone for T {}\n\n// These could potentially overlap with the blanket implementation above,\n// so are only allowed because CheapToClone is a marker trait.\nimpl<T: CheapToClone, U: CheapToClone> CheapToClone for (T, U) {}\nimpl<T: CheapToClone> CheapToClone for std::ops::Range<T> {}\n\nfn cheap_clone<T: CheapToClone>(t: T) -> T {\n t.clone()\n}\n```\n\nThis is expected to replace the unstable `overlapping_marker_traits`\nfeature, which applied to all empty traits (without needing an opt-in).\n" } , LintCompletion { label : "ffi_const" , description : "# `ffi_const`\n\nThe tracking issue for this feature is: [#58328]\n\n------\n\nThe `#[ffi_const]` attribute applies clang's `const` attribute to foreign\nfunctions declarations.\n\nThat is, `#[ffi_const]` functions shall have no effects except for its return\nvalue, which can only depend on the values of the function parameters, and is\nnot affected by changes to the observable state of the program.\n\nApplying the `#[ffi_const]` attribute to a function that violates these\nrequirements is undefined behaviour.\n\nThis attribute enables Rust to perform common optimizations, like sub-expression\nelimination, and it can avoid emitting some calls in repeated invocations of the\nfunction with the same argument values regardless of other operations being\nperformed in between these functions calls (as opposed to `#[ffi_pure]`\nfunctions).\n\n## Pitfalls\n\nA `#[ffi_const]` function can only read global memory that would not affect\nits return value for the whole execution of the program (e.g. immutable global\nmemory). `#[ffi_const]` functions are referentially-transparent and therefore\nmore strict than `#[ffi_pure]` functions.\n\nA common pitfall involves applying the `#[ffi_const]` attribute to a\nfunction that reads memory through pointer arguments which do not necessarily\npoint to immutable global memory.\n\nA `#[ffi_const]` function that returns unit has no effect on the abstract\nmachine's state, and a `#[ffi_const]` function cannot be `#[ffi_pure]`.\n\nA `#[ffi_const]` function must not diverge, neither via a side effect (e.g. a\ncall to `abort`) nor by infinite loops.\n\nWhen translating C headers to Rust FFI, it is worth verifying for which targets\nthe `const` attribute is enabled in those headers, and using the appropriate\n`cfg` macros in the Rust side to match those definitions. While the semantics of\n`const` are implemented identically by many C and C++ compilers, e.g., clang,\n[GCC], [ARM C/C++ compiler], [IBM ILE C/C++], etc. they are not necessarily\nimplemented in this way on all of them. It is therefore also worth verifying\nthat the semantics of the C toolchain used to compile the binary being linked\nagainst are compatible with those of the `#[ffi_const]`.\n\n[#58328]: https://github.com/rust-lang/rust/issues/58328\n[ARM C/C++ compiler]: http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0491c/Cacgigch.html\n[GCC]: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-const-function-attribute\n[IBM ILE C/C++]: https://www.ibm.com/support/knowledgecenter/fr/ssw_ibm_i_71/rzarg/fn_attrib_const.htm\n" } , LintCompletion { label : "doc_spotlight" , description : "# `doc_spotlight`\n\nThe tracking issue for this feature is: [#45040]\n\nThe `doc_spotlight` feature allows the use of the `spotlight` parameter to the `#[doc]` attribute,\nto \"spotlight\" a specific trait on the return values of functions. Adding a `#[doc(spotlight)]`\nattribute to a trait definition will make rustdoc print extra information for functions which return\na type that implements that trait. This attribute is applied to the `Iterator`, `io::Read`, and\n`io::Write` traits in the standard library.\n\nYou can do this on your own traits, like this:\n\n```\n#![feature(doc_spotlight)]\n\n#[doc(spotlight)]\npub trait MyTrait {}\n\npub struct MyStruct;\nimpl MyTrait for MyStruct {}\n\n/// The docs for this function will have an extra line about `MyStruct` implementing `MyTrait`,\n/// without having to write that yourself!\npub fn my_fn() -> MyStruct { MyStruct }\n```\n\nThis feature was originally implemented in PR [#45039].\n\n[#45040]: https://github.com/rust-lang/rust/issues/45040\n[#45039]: https://github.com/rust-lang/rust/pull/45039\n" } , LintCompletion { label : "compiler_builtins" , description : "# `compiler_builtins`\n\nThis feature is internal to the Rust compiler and is not intended for general use.\n\n------------------------\n" } , LintCompletion { label : "lang_items" , description : "# `lang_items`\n\nThe tracking issue for this feature is: None.\n\n------------------------\n\nThe `rustc` compiler has certain pluggable operations, that is,\nfunctionality that isn't hard-coded into the language, but is\nimplemented in libraries, with a special marker to tell the compiler\nit exists. The marker is the attribute `#[lang = \"...\"]` and there are\nvarious different values of `...`, i.e. various different 'lang\nitems'.\n\nFor example, `Box` pointers require two lang items, one for allocation\nand one for deallocation. A freestanding program that uses the `Box`\nsugar for dynamic allocations via `malloc` and `free`:\n\n```rust,ignore\n#![feature(lang_items, box_syntax, start, libc, core_intrinsics)]\n#![no_std]\nuse core::intrinsics;\nuse core::panic::PanicInfo;\n\nextern crate libc;\n\n#[lang = \"owned_box\"]\npub struct Box<T>(*mut T);\n\n#[lang = \"exchange_malloc\"]\nunsafe fn allocate(size: usize, _align: usize) -> *mut u8 {\n let p = libc::malloc(size as libc::size_t) as *mut u8;\n\n // Check if `malloc` failed:\n if p as usize == 0 {\n intrinsics::abort();\n }\n\n p\n}\n\n#[lang = \"box_free\"]\nunsafe fn box_free<T: ?Sized>(ptr: *mut T) {\n libc::free(ptr as *mut libc::c_void)\n}\n\n#[start]\nfn main(_argc: isize, _argv: *const *const u8) -> isize {\n let _x = box 1;\n\n 0\n}\n\n#[lang = \"eh_personality\"] extern fn rust_eh_personality() {}\n#[lang = \"panic_impl\"] extern fn rust_begin_panic(info: &PanicInfo) -> ! { unsafe { intrinsics::abort() } }\n#[no_mangle] pub extern fn rust_eh_register_frames () {}\n#[no_mangle] pub extern fn rust_eh_unregister_frames () {}\n```\n\nNote the use of `abort`: the `exchange_malloc` lang item is assumed to\nreturn a valid pointer, and so needs to do the check internally.\n\nOther features provided by lang items include:\n\n- overloadable operators via traits: the traits corresponding to the\n `==`, `<`, dereferencing (`*`) and `+` (etc.) operators are all\n marked with lang items; those specific four are `eq`, `ord`,\n `deref`, and `add` respectively.\n- stack unwinding and general failure; the `eh_personality`,\n `panic` and `panic_bounds_checks` lang items.\n- the traits in `std::marker` used to indicate types of\n various kinds; lang items `send`, `sync` and `copy`.\n- the marker types and variance indicators found in\n `std::marker`; lang items `covariant_type`,\n `contravariant_lifetime`, etc.\n\nLang items are loaded lazily by the compiler; e.g. if one never uses\n`Box` then there is no need to define functions for `exchange_malloc`\nand `box_free`. `rustc` will emit an error when an item is needed\nbut not found in the current crate or any that it depends on.\n\nMost lang items are defined by `libcore`, but if you're trying to build\nan executable without the standard library, you'll run into the need\nfor lang items. The rest of this page focuses on this use-case, even though\nlang items are a bit broader than that.\n\n### Using libc\n\nIn order to build a `#[no_std]` executable we will need libc as a dependency.\nWe can specify this using our `Cargo.toml` file:\n\n```toml\n[dependencies]\nlibc = { version = \"0.2.14\", default-features = false }\n```\n\nNote that the default features have been disabled. This is a critical step -\n**the default features of libc include the standard library and so must be\ndisabled.**\n\n### Writing an executable without stdlib\n\nControlling the entry point is possible in two ways: the `#[start]` attribute,\nor overriding the default shim for the C `main` function with your own.\n\nThe function marked `#[start]` is passed the command line parameters\nin the same format as C:\n\n```rust,ignore\n#![feature(lang_items, core_intrinsics)]\n#![feature(start)]\n#![no_std]\nuse core::intrinsics;\nuse core::panic::PanicInfo;\n\n// Pull in the system libc library for what crt0.o likely requires.\nextern crate libc;\n\n// Entry point for this program.\n#[start]\nfn start(_argc: isize, _argv: *const *const u8) -> isize {\n 0\n}\n\n// These functions are used by the compiler, but not\n// for a bare-bones hello world. These are normally\n// provided by libstd.\n#[lang = \"eh_personality\"]\n#[no_mangle]\npub extern fn rust_eh_personality() {\n}\n\n#[lang = \"panic_impl\"]\n#[no_mangle]\npub extern fn rust_begin_panic(info: &PanicInfo) -> ! {\n unsafe { intrinsics::abort() }\n}\n```\n\nTo override the compiler-inserted `main` shim, one has to disable it\nwith `#![no_main]` and then create the appropriate symbol with the\ncorrect ABI and the correct name, which requires overriding the\ncompiler's name mangling too:\n\n```rust,ignore\n#![feature(lang_items, core_intrinsics)]\n#![feature(start)]\n#![no_std]\n#![no_main]\nuse core::intrinsics;\nuse core::panic::PanicInfo;\n\n// Pull in the system libc library for what crt0.o likely requires.\nextern crate libc;\n\n// Entry point for this program.\n#[no_mangle] // ensure that this symbol is called `main` in the output\npub extern fn main(_argc: i32, _argv: *const *const u8) -> i32 {\n 0\n}\n\n// These functions are used by the compiler, but not\n// for a bare-bones hello world. These are normally\n// provided by libstd.\n#[lang = \"eh_personality\"]\n#[no_mangle]\npub extern fn rust_eh_personality() {\n}\n\n#[lang = \"panic_impl\"]\n#[no_mangle]\npub extern fn rust_begin_panic(info: &PanicInfo) -> ! {\n unsafe { intrinsics::abort() }\n}\n```\n\nIn many cases, you may need to manually link to the `compiler_builtins` crate\nwhen building a `no_std` binary. You may observe this via linker error messages\nsuch as \"```undefined reference to `__rust_probestack'```\".\n\n## More about the language items\n\nThe compiler currently makes a few assumptions about symbols which are\navailable in the executable to call. Normally these functions are provided by\nthe standard library, but without it you must define your own. These symbols\nare called \"language items\", and they each have an internal name, and then a\nsignature that an implementation must conform to.\n\nThe first of these functions, `rust_eh_personality`, is used by the failure\nmechanisms of the compiler. This is often mapped to GCC's personality function\n(see the [libstd implementation][unwind] for more information), but crates\nwhich do not trigger a panic can be assured that this function is never\ncalled. The language item's name is `eh_personality`.\n\n[unwind]: https://github.com/rust-lang/rust/blob/master/src/libpanic_unwind/gcc.rs\n\nThe second function, `rust_begin_panic`, is also used by the failure mechanisms of the\ncompiler. When a panic happens, this controls the message that's displayed on\nthe screen. While the language item's name is `panic_impl`, the symbol name is\n`rust_begin_panic`.\n\nFinally, a `eh_catch_typeinfo` static is needed for certain targets which\nimplement Rust panics on top of C++ exceptions.\n\n## List of all language items\n\nThis is a list of all language items in Rust along with where they are located in\nthe source code.\n\n- Primitives\n - `i8`: `libcore/num/mod.rs`\n - `i16`: `libcore/num/mod.rs`\n - `i32`: `libcore/num/mod.rs`\n - `i64`: `libcore/num/mod.rs`\n - `i128`: `libcore/num/mod.rs`\n - `isize`: `libcore/num/mod.rs`\n - `u8`: `libcore/num/mod.rs`\n - `u16`: `libcore/num/mod.rs`\n - `u32`: `libcore/num/mod.rs`\n - `u64`: `libcore/num/mod.rs`\n - `u128`: `libcore/num/mod.rs`\n - `usize`: `libcore/num/mod.rs`\n - `f32`: `libstd/f32.rs`\n - `f64`: `libstd/f64.rs`\n - `char`: `libcore/char.rs`\n - `slice`: `liballoc/slice.rs`\n - `str`: `liballoc/str.rs`\n - `const_ptr`: `libcore/ptr.rs`\n - `mut_ptr`: `libcore/ptr.rs`\n - `unsafe_cell`: `libcore/cell.rs`\n- Runtime\n - `start`: `libstd/rt.rs`\n - `eh_personality`: `libpanic_unwind/emcc.rs` (EMCC)\n - `eh_personality`: `libpanic_unwind/gcc.rs` (GNU)\n - `eh_personality`: `libpanic_unwind/seh.rs` (SEH)\n - `eh_catch_typeinfo`: `libpanic_unwind/emcc.rs` (EMCC)\n - `panic`: `libcore/panicking.rs`\n - `panic_bounds_check`: `libcore/panicking.rs`\n - `panic_impl`: `libcore/panicking.rs`\n - `panic_impl`: `libstd/panicking.rs`\n- Allocations\n - `owned_box`: `liballoc/boxed.rs`\n - `exchange_malloc`: `liballoc/heap.rs`\n - `box_free`: `liballoc/heap.rs`\n- Operands\n - `not`: `libcore/ops/bit.rs`\n - `bitand`: `libcore/ops/bit.rs`\n - `bitor`: `libcore/ops/bit.rs`\n - `bitxor`: `libcore/ops/bit.rs`\n - `shl`: `libcore/ops/bit.rs`\n - `shr`: `libcore/ops/bit.rs`\n - `bitand_assign`: `libcore/ops/bit.rs`\n - `bitor_assign`: `libcore/ops/bit.rs`\n - `bitxor_assign`: `libcore/ops/bit.rs`\n - `shl_assign`: `libcore/ops/bit.rs`\n - `shr_assign`: `libcore/ops/bit.rs`\n - `deref`: `libcore/ops/deref.rs`\n - `deref_mut`: `libcore/ops/deref.rs`\n - `index`: `libcore/ops/index.rs`\n - `index_mut`: `libcore/ops/index.rs`\n - `add`: `libcore/ops/arith.rs`\n - `sub`: `libcore/ops/arith.rs`\n - `mul`: `libcore/ops/arith.rs`\n - `div`: `libcore/ops/arith.rs`\n - `rem`: `libcore/ops/arith.rs`\n - `neg`: `libcore/ops/arith.rs`\n - `add_assign`: `libcore/ops/arith.rs`\n - `sub_assign`: `libcore/ops/arith.rs`\n - `mul_assign`: `libcore/ops/arith.rs`\n - `div_assign`: `libcore/ops/arith.rs`\n - `rem_assign`: `libcore/ops/arith.rs`\n - `eq`: `libcore/cmp.rs`\n - `ord`: `libcore/cmp.rs`\n- Functions\n - `fn`: `libcore/ops/function.rs`\n - `fn_mut`: `libcore/ops/function.rs`\n - `fn_once`: `libcore/ops/function.rs`\n - `generator_state`: `libcore/ops/generator.rs`\n - `generator`: `libcore/ops/generator.rs`\n- Other\n - `coerce_unsized`: `libcore/ops/unsize.rs`\n - `drop`: `libcore/ops/drop.rs`\n - `drop_in_place`: `libcore/ptr.rs`\n - `clone`: `libcore/clone.rs`\n - `copy`: `libcore/marker.rs`\n - `send`: `libcore/marker.rs`\n - `sized`: `libcore/marker.rs`\n - `unsize`: `libcore/marker.rs`\n - `sync`: `libcore/marker.rs`\n - `phantom_data`: `libcore/marker.rs`\n - `discriminant_kind`: `libcore/marker.rs`\n - `freeze`: `libcore/marker.rs`\n - `debug_trait`: `libcore/fmt/mod.rs`\n - `non_zero`: `libcore/nonzero.rs`\n - `arc`: `liballoc/sync.rs`\n - `rc`: `liballoc/rc.rs`\n" } , LintCompletion { label : "member_constraints" , description : "# `member_constraints`\n\nThe tracking issue for this feature is: [#61997]\n\n[#61997]: https://github.com/rust-lang/rust/issues/61997\n\n------------------------\n\nThe `member_constraints` feature gate lets you use `impl Trait` syntax with\nmultiple unrelated lifetime parameters.\n\nA simple example is:\n\n```rust\n#![feature(member_constraints)]\n\ntrait Trait<'a, 'b> { }\nimpl<T> Trait<'_, '_> for T {}\n\nfn foo<'a, 'b>(x: &'a u32, y: &'b u32) -> impl Trait<'a, 'b> {\n (x, y)\n}\n\nfn main() { }\n```\n\nWithout the `member_constraints` feature gate, the above example is an\nerror because both `'a` and `'b` appear in the impl Trait bounds, but\nneither outlives the other.\n" } , LintCompletion { label : "crate_visibility_modifier" , description : "# `crate_visibility_modifier`\n\nThe tracking issue for this feature is: [#53120]\n\n[#53120]: https://github.com/rust-lang/rust/issues/53120\n\n-----\n\nThe `crate_visibility_modifier` feature allows the `crate` keyword to be used\nas a visibility modifier synonymous to `pub(crate)`, indicating that a type\n(function, _&c._) is to be visible to the entire enclosing crate, but not to\nother crates.\n\n```rust\n#![feature(crate_visibility_modifier)]\n\ncrate struct Foo {\n bar: usize,\n}\n```\n" } , LintCompletion { label : "try_blocks" , description : "# `try_blocks`\n\nThe tracking issue for this feature is: [#31436]\n\n[#31436]: https://github.com/rust-lang/rust/issues/31436\n\n------------------------\n\nThe `try_blocks` feature adds support for `try` blocks. A `try`\nblock creates a new scope one can use the `?` operator in.\n\n```rust,edition2018\n#![feature(try_blocks)]\n\nuse std::num::ParseIntError;\n\nlet result: Result<i32, ParseIntError> = try {\n \"1\".parse::<i32>()?\n + \"2\".parse::<i32>()?\n + \"3\".parse::<i32>()?\n};\nassert_eq!(result, Ok(6));\n\nlet result: Result<i32, ParseIntError> = try {\n \"1\".parse::<i32>()?\n + \"foo\".parse::<i32>()?\n + \"3\".parse::<i32>()?\n};\nassert!(result.is_err());\n```\n" } , LintCompletion { label : "const_in_array_repeat_expressions" , description : "# `const_in_array_repeat_expressions`\n\nThe tracking issue for this feature is: [#49147]\n\n[#49147]: https://github.com/rust-lang/rust/issues/49147\n\n------------------------\n\nRelaxes the rules for repeat expressions, `[x; N]` such that `x` may also be `const` (strictly\nspeaking rvalue promotable), in addition to `typeof(x): Copy`. The result of `[x; N]` where `x` is\n`const` is itself also `const`.\n" } , LintCompletion { label : "negative_impls" , description : "# `negative_impls`\n\nThe tracking issue for this feature is [#68318].\n\n[#68318]: https://github.com/rust-lang/rust/issues/68318\n\n----\n\nWith the feature gate `negative_impls`, you can write negative impls as well as positive ones:\n\n```rust\n#![feature(negative_impls)]\ntrait DerefMut { }\nimpl<T: ?Sized> !DerefMut for &T { }\n```\n\nNegative impls indicate a semver guarantee that the given trait will not be implemented for the given types. Negative impls play an additional purpose for auto traits, described below.\n\nNegative impls have the following characteristics:\n\n* They do not have any items.\n* They must obey the orphan rules as if they were a positive impl.\n* They cannot \"overlap\" with any positive impls.\n\n## Semver interaction\n\nIt is a breaking change to remove a negative impl. Negative impls are a commitment not to implement the given trait for the named types.\n\n## Orphan and overlap rules\n\nNegative impls must obey the same orphan rules as a positive impl. This implies you cannot add a negative impl for types defined in upstream crates and so forth.\n\nSimilarly, negative impls cannot overlap with positive impls, again using the same \"overlap\" check that we ordinarily use to determine if two impls overlap. (Note that positive impls typically cannot overlap with one another either, except as permitted by specialization.)\n\n## Interaction with auto traits\n\nDeclaring a negative impl `impl !SomeAutoTrait for SomeType` for an\nauto-trait serves two purposes:\n\n* as with any trait, it declares that `SomeType` will never implement `SomeAutoTrait`;\n* it disables the automatic `SomeType: SomeAutoTrait` impl that would otherwise have been generated.\n\nNote that, at present, there is no way to indicate that a given type\ndoes not implement an auto trait *but that it may do so in the\nfuture*. For ordinary types, this is done by simply not declaring any\nimpl at all, but that is not an option for auto traits. A workaround\nis that one could embed a marker type as one of the fields, where the\nmarker type is `!AutoTrait`.\n\n## Immediate uses\n\nNegative impls are used to declare that `&T: !DerefMut` and `&mut T: !Clone`, as required to fix the soundness of `Pin` described in [#66544](https://github.com/rust-lang/rust/issues/66544).\n\nThis serves two purposes:\n\n* For proving the correctness of unsafe code, we can use that impl as evidence that no `DerefMut` or `Clone` impl exists.\n* It prevents downstream crates from creating such impls.\n" } , LintCompletion { label : "c_variadic" , description : "# `c_variadic`\n\nThe tracking issue for this feature is: [#44930]\n\n[#44930]: https://github.com/rust-lang/rust/issues/44930\n\n------------------------\n\nThe `c_variadic` language feature enables C-variadic functions to be\ndefined in Rust. The may be called both from within Rust and via FFI.\n\n## Examples\n\n```rust\n#![feature(c_variadic)]\n\npub unsafe extern \"C\" fn add(n: usize, mut args: ...) -> usize {\n let mut sum = 0;\n for _ in 0..n {\n sum += args.arg::<usize>();\n }\n sum\n}\n```\n" } , LintCompletion { label : "profiler_runtime" , description : "# `profiler_runtime`\n\nThe tracking issue for this feature is: [#42524](https://github.com/rust-lang/rust/issues/42524).\n\n------------------------\n" } , LintCompletion { label : "box_syntax" , description : "# `box_syntax`\n\nThe tracking issue for this feature is: [#49733]\n\n[#49733]: https://github.com/rust-lang/rust/issues/49733\n\nSee also [`box_patterns`](box-patterns.md)\n\n------------------------\n\nCurrently the only stable way to create a `Box` is via the `Box::new` method.\nAlso it is not possible in stable Rust to destructure a `Box` in a match\npattern. The unstable `box` keyword can be used to create a `Box`. An example\nusage would be:\n\n```rust\n#![feature(box_syntax)]\n\nfn main() {\n let b = box 5;\n}\n```\n" } , LintCompletion { label : "ffi_pure" , description : "# `ffi_pure`\n\nThe tracking issue for this feature is: [#58329]\n\n------\n\nThe `#[ffi_pure]` attribute applies clang's `pure` attribute to foreign\nfunctions declarations.\n\nThat is, `#[ffi_pure]` functions shall have no effects except for its return\nvalue, which shall not change across two consecutive function calls with\nthe same parameters.\n\nApplying the `#[ffi_pure]` attribute to a function that violates these\nrequirements is undefined behavior.\n\nThis attribute enables Rust to perform common optimizations, like sub-expression\nelimination and loop optimizations. Some common examples of pure functions are\n`strlen` or `memcmp`.\n\nThese optimizations are only applicable when the compiler can prove that no\nprogram state observable by the `#[ffi_pure]` function has changed between calls\nof the function, which could alter the result. See also the `#[ffi_const]`\nattribute, which provides stronger guarantees regarding the allowable behavior\nof a function, enabling further optimization.\n\n## Pitfalls\n\nA `#[ffi_pure]` function can read global memory through the function\nparameters (e.g. pointers), globals, etc. `#[ffi_pure]` functions are not\nreferentially-transparent, and are therefore more relaxed than `#[ffi_const]`\nfunctions.\n\nHowever, accesing global memory through volatile or atomic reads can violate the\nrequirement that two consecutive function calls shall return the same value.\n\nA `pure` function that returns unit has no effect on the abstract machine's\nstate.\n\nA `#[ffi_pure]` function must not diverge, neither via a side effect (e.g. a\ncall to `abort`) nor by infinite loops.\n\nWhen translating C headers to Rust FFI, it is worth verifying for which targets\nthe `pure` attribute is enabled in those headers, and using the appropriate\n`cfg` macros in the Rust side to match those definitions. While the semantics of\n`pure` are implemented identically by many C and C++ compilers, e.g., clang,\n[GCC], [ARM C/C++ compiler], [IBM ILE C/C++], etc. they are not necessarily\nimplemented in this way on all of them. It is therefore also worth verifying\nthat the semantics of the C toolchain used to compile the binary being linked\nagainst are compatible with those of the `#[ffi_pure]`.\n\n\n[#58329]: https://github.com/rust-lang/rust/issues/58329\n[ARM C/C++ compiler]: http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0491c/Cacigdac.html\n[GCC]: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-pure-function-attribute\n[IBM ILE C/C++]: https://www.ibm.com/support/knowledgecenter/fr/ssw_ibm_i_71/rzarg/fn_attrib_pure.htm\n" } , LintCompletion { label : "arbitrary_enum_discriminant" , description : "# `arbitrary_enum_discriminant`\n\nThe tracking issue for this feature is: [#60553]\n\n[#60553]: https://github.com/rust-lang/rust/issues/60553\n\n------------------------\n\nThe `arbitrary_enum_discriminant` feature permits tuple-like and\nstruct-like enum variants with `#[repr(<int-type>)]` to have explicit discriminants.\n\n## Examples\n\n```rust\n#![feature(arbitrary_enum_discriminant)]\n\n#[allow(dead_code)]\n#[repr(u8)]\nenum Enum {\n Unit = 3,\n Tuple(u16) = 2,\n Struct {\n a: u8,\n b: u16,\n } = 1,\n}\n\nimpl Enum {\n fn tag(&self) -> u8 {\n unsafe { *(self as *const Self as *const u8) }\n }\n}\n\nassert_eq!(3, Enum::Unit.tag());\nassert_eq!(2, Enum::Tuple(5).tag());\nassert_eq!(1, Enum::Struct{a: 7, b: 11}.tag());\n```\n" } , LintCompletion { label : "unsized_locals" , description : "# `unsized_locals`\n\nThe tracking issue for this feature is: [#48055]\n\n[#48055]: https://github.com/rust-lang/rust/issues/48055\n\n------------------------\n\nThis implements [RFC1909]. When turned on, you can have unsized arguments and locals:\n\n[RFC1909]: https://github.com/rust-lang/rfcs/blob/master/text/1909-unsized-rvalues.md\n\n```rust\n#![feature(unsized_locals)]\n\nuse std::any::Any;\n\nfn main() {\n let x: Box<dyn Any> = Box::new(42);\n let x: dyn Any = *x;\n // ^ unsized local variable\n // ^^ unsized temporary\n foo(x);\n}\n\nfn foo(_: dyn Any) {}\n// ^^^^^^ unsized argument\n```\n\nThe RFC still forbids the following unsized expressions:\n\n```rust,ignore\n#![feature(unsized_locals)]\n\nuse std::any::Any;\n\nstruct MyStruct<T: ?Sized> {\n content: T,\n}\n\nstruct MyTupleStruct<T: ?Sized>(T);\n\nfn answer() -> Box<dyn Any> {\n Box::new(42)\n}\n\nfn main() {\n // You CANNOT have unsized statics.\n static X: dyn Any = *answer(); // ERROR\n const Y: dyn Any = *answer(); // ERROR\n\n // You CANNOT have struct initialized unsized.\n MyStruct { content: *answer() }; // ERROR\n MyTupleStruct(*answer()); // ERROR\n (42, *answer()); // ERROR\n\n // You CANNOT have unsized return types.\n fn my_function() -> dyn Any { *answer() } // ERROR\n\n // You CAN have unsized local variables...\n let mut x: dyn Any = *answer(); // OK\n // ...but you CANNOT reassign to them.\n x = *answer(); // ERROR\n\n // You CANNOT even initialize them separately.\n let y: dyn Any; // OK\n y = *answer(); // ERROR\n\n // Not mentioned in the RFC, but by-move captured variables are also Sized.\n let x: dyn Any = *answer();\n (move || { // ERROR\n let y = x;\n })();\n\n // You CAN create a closure with unsized arguments,\n // but you CANNOT call it.\n // This is an implementation detail and may be changed in the future.\n let f = |x: dyn Any| {};\n f(*answer()); // ERROR\n}\n```\n\n## By-value trait objects\n\nWith this feature, you can have by-value `self` arguments without `Self: Sized` bounds.\n\n```rust\n#![feature(unsized_locals)]\n\ntrait Foo {\n fn foo(self) {}\n}\n\nimpl<T: ?Sized> Foo for T {}\n\nfn main() {\n let slice: Box<[i32]> = Box::new([1, 2, 3]);\n <[i32] as Foo>::foo(*slice);\n}\n```\n\nAnd `Foo` will also be object-safe.\n\n```rust\n#![feature(unsized_locals)]\n\ntrait Foo {\n fn foo(self) {}\n}\n\nimpl<T: ?Sized> Foo for T {}\n\nfn main () {\n let slice: Box<dyn Foo> = Box::new([1, 2, 3]);\n // doesn't compile yet\n <dyn Foo as Foo>::foo(*slice);\n}\n```\n\nOne of the objectives of this feature is to allow `Box<dyn FnOnce>`.\n\n## Variable length arrays\n\nThe RFC also describes an extension to the array literal syntax: `[e; dyn n]`. In the syntax, `n` isn't necessarily a constant expression. The array is dynamically allocated on the stack and has the type of `[T]`, instead of `[T; n]`.\n\n```rust,ignore\n#![feature(unsized_locals)]\n\nfn mergesort<T: Ord>(a: &mut [T]) {\n let mut tmp = [T; dyn a.len()];\n // ...\n}\n\nfn main() {\n let mut a = [3, 1, 5, 6];\n mergesort(&mut a);\n assert_eq!(a, [1, 3, 5, 6]);\n}\n```\n\nVLAs are not implemented yet. The syntax isn't final, either. We may need an alternative syntax for Rust 2015 because, in Rust 2015, expressions like `[e; dyn(1)]` would be ambiguous. One possible alternative proposed in the RFC is `[e; n]`: if `n` captures one or more local variables, then it is considered as `[e; dyn n]`.\n\n## Advisory on stack usage\n\nIt's advised not to casually use the `#![feature(unsized_locals)]` feature. Typical use-cases are:\n\n- When you need a by-value trait objects.\n- When you really need a fast allocation of small temporary arrays.\n\nAnother pitfall is repetitive allocation and temporaries. Currently the compiler simply extends the stack frame every time it encounters an unsized assignment. So for example, the code\n\n```rust\n#![feature(unsized_locals)]\n\nfn main() {\n let x: Box<[i32]> = Box::new([1, 2, 3, 4, 5]);\n let _x = {{{{{{{{{{*x}}}}}}}}}};\n}\n```\n\nand the code\n\n```rust\n#![feature(unsized_locals)]\n\nfn main() {\n for _ in 0..10 {\n let x: Box<[i32]> = Box::new([1, 2, 3, 4, 5]);\n let _x = *x;\n }\n}\n```\n\nwill unnecessarily extend the stack frame.\n" } , LintCompletion { label : "cfg_sanitize" , description : "# `cfg_sanitize`\n\nThe tracking issue for this feature is: [#39699]\n\n[#39699]: https://github.com/rust-lang/rust/issues/39699\n\n------------------------\n\nThe `cfg_sanitize` feature makes it possible to execute different code\ndepending on whether a particular sanitizer is enabled or not.\n\n## Examples\n\n```rust\n#![feature(cfg_sanitize)]\n\n#[cfg(sanitize = \"thread\")]\nfn a() {\n // ...\n}\n\n#[cfg(not(sanitize = \"thread\"))]\nfn a() {\n // ...\n}\n\nfn b() {\n if cfg!(sanitize = \"leak\") {\n // ...\n } else {\n // ...\n }\n}\n```\n" } , LintCompletion { label : "cmse_nonsecure_entry" , description : "# `cmse_nonsecure_entry`\n\nThe tracking issue for this feature is: [#75835]\n\n[#75835]: https://github.com/rust-lang/rust/issues/75835\n\n------------------------\n\nThe [TrustZone-M\nfeature](https://developer.arm.com/documentation/100690/latest/) is available\nfor targets with the Armv8-M architecture profile (`thumbv8m` in their target\nname).\nLLVM, the Rust compiler and the linker are providing\n[support](https://developer.arm.com/documentation/ecm0359818/latest/) for the\nTrustZone-M feature.\n\nOne of the things provided, with this unstable feature, is the\n`cmse_nonsecure_entry` attribute. This attribute marks a Secure function as an\nentry function (see [section\n5.4](https://developer.arm.com/documentation/ecm0359818/latest/) for details).\nWith this attribute, the compiler will do the following:\n* add a special symbol on the function which is the `__acle_se_` prefix and the\n standard function name\n* constrain the number of parameters to avoid using the Non-Secure stack\n* before returning from the function, clear registers that might contain Secure\n information\n* use the `BXNS` instruction to return\n\nBecause the stack can not be used to pass parameters, there will be compilation\nerrors if:\n* the total size of all parameters is too big (for example more than four 32\n bits integers)\n* the entry function is not using a C ABI\n\nThe special symbol `__acle_se_` will be used by the linker to generate a secure\ngateway veneer.\n\n<!-- NOTE(ignore) this example is specific to thumbv8m targets -->\n\n``` rust,ignore\n#![feature(cmse_nonsecure_entry)]\n\n#[no_mangle]\n#[cmse_nonsecure_entry]\npub extern \"C\" fn entry_function(input: u32) -> u32 {\n input + 6\n}\n```\n\n``` text\n$ rustc --emit obj --crate-type lib --target thumbv8m.main-none-eabi function.rs\n$ arm-none-eabi-objdump -D function.o\n\n00000000 <entry_function>:\n 0: b580 push {r7, lr}\n 2: 466f mov r7, sp\n 4: b082 sub sp, #8\n 6: 9001 str r0, [sp, #4]\n 8: 1d81 adds r1, r0, #6\n a: 460a mov r2, r1\n c: 4281 cmp r1, r0\n e: 9200 str r2, [sp, #0]\n 10: d30b bcc.n 2a <entry_function+0x2a>\n 12: e7ff b.n 14 <entry_function+0x14>\n 14: 9800 ldr r0, [sp, #0]\n 16: b002 add sp, #8\n 18: e8bd 4080 ldmia.w sp!, {r7, lr}\n 1c: 4671 mov r1, lr\n 1e: 4672 mov r2, lr\n 20: 4673 mov r3, lr\n 22: 46f4 mov ip, lr\n 24: f38e 8800 msr CPSR_f, lr\n 28: 4774 bxns lr\n 2a: f240 0000 movw r0, #0\n 2e: f2c0 0000 movt r0, #0\n 32: f240 0200 movw r2, #0\n 36: f2c0 0200 movt r2, #0\n 3a: 211c movs r1, #28\n 3c: f7ff fffe bl 0 <_ZN4core9panicking5panic17h5c028258ca2fb3f5E>\n 40: defe udf #254 ; 0xfe\n```\n" } , LintCompletion { label : "cfg_version" , description : "# `cfg_version`\n\nThe tracking issue for this feature is: [#64796]\n\n[#64796]: https://github.com/rust-lang/rust/issues/64796\n\n------------------------\n\nThe `cfg_version` feature makes it possible to execute different code\ndepending on the compiler version.\n\n## Examples\n\n```rust\n#![feature(cfg_version)]\n\n#[cfg(version(\"1.42\"))]\nfn a() {\n // ...\n}\n\n#[cfg(not(version(\"1.42\")))]\nfn a() {\n // ...\n}\n\nfn b() {\n if cfg!(version(\"1.42\")) {\n // ...\n } else {\n // ...\n }\n}\n```\n" } , LintCompletion { label : "unsized_tuple_coercion" , description : "# `unsized_tuple_coercion`\n\nThe tracking issue for this feature is: [#42877]\n\n[#42877]: https://github.com/rust-lang/rust/issues/42877\n\n------------------------\n\nThis is a part of [RFC0401]. According to the RFC, there should be an implementation like this:\n\n```rust,ignore\nimpl<..., T, U: ?Sized> Unsized<(..., U)> for (..., T) where T: Unsized<U> {}\n```\n\nThis implementation is currently gated behind `#[feature(unsized_tuple_coercion)]` to avoid insta-stability. Therefore you can use it like this:\n\n```rust\n#![feature(unsized_tuple_coercion)]\n\nfn main() {\n let x : ([i32; 3], [i32; 3]) = ([1, 2, 3], [4, 5, 6]);\n let y : &([i32; 3], [i32]) = &x;\n assert_eq!(y.1[0], 4);\n}\n```\n\n[RFC0401]: https://github.com/rust-lang/rfcs/blob/master/text/0401-coercions.md\n" } , LintCompletion { label : "generators" , description : "# `generators`\n\nThe tracking issue for this feature is: [#43122]\n\n[#43122]: https://github.com/rust-lang/rust/issues/43122\n\n------------------------\n\nThe `generators` feature gate in Rust allows you to define generator or\ncoroutine literals. A generator is a \"resumable function\" that syntactically\nresembles a closure but compiles to much different semantics in the compiler\nitself. The primary feature of a generator is that it can be suspended during\nexecution to be resumed at a later date. Generators use the `yield` keyword to\n\"return\", and then the caller can `resume` a generator to resume execution just\nafter the `yield` keyword.\n\nGenerators are an extra-unstable feature in the compiler right now. Added in\n[RFC 2033] they're mostly intended right now as a information/constraint\ngathering phase. The intent is that experimentation can happen on the nightly\ncompiler before actual stabilization. A further RFC will be required to\nstabilize generators/coroutines and will likely contain at least a few small\ntweaks to the overall design.\n\n[RFC 2033]: https://github.com/rust-lang/rfcs/pull/2033\n\nA syntactical example of a generator is:\n\n```rust\n#![feature(generators, generator_trait)]\n\nuse std::ops::{Generator, GeneratorState};\nuse std::pin::Pin;\n\nfn main() {\n let mut generator = || {\n yield 1;\n return \"foo\"\n };\n\n match Pin::new(&mut generator).resume(()) {\n GeneratorState::Yielded(1) => {}\n _ => panic!(\"unexpected value from resume\"),\n }\n match Pin::new(&mut generator).resume(()) {\n GeneratorState::Complete(\"foo\") => {}\n _ => panic!(\"unexpected value from resume\"),\n }\n}\n```\n\nGenerators are closure-like literals which can contain a `yield` statement. The\n`yield` statement takes an optional expression of a value to yield out of the\ngenerator. All generator literals implement the `Generator` trait in the\n`std::ops` module. The `Generator` trait has one main method, `resume`, which\nresumes execution of the generator at the previous suspension point.\n\nAn example of the control flow of generators is that the following example\nprints all numbers in order:\n\n```rust\n#![feature(generators, generator_trait)]\n\nuse std::ops::Generator;\nuse std::pin::Pin;\n\nfn main() {\n let mut generator = || {\n println!(\"2\");\n yield;\n println!(\"4\");\n };\n\n println!(\"1\");\n Pin::new(&mut generator).resume(());\n println!(\"3\");\n Pin::new(&mut generator).resume(());\n println!(\"5\");\n}\n```\n\nAt this time the main intended use case of generators is an implementation\nprimitive for async/await syntax, but generators will likely be extended to\nergonomic implementations of iterators and other primitives in the future.\nFeedback on the design and usage is always appreciated!\n\n### The `Generator` trait\n\nThe `Generator` trait in `std::ops` currently looks like:\n\n```rust\n# #![feature(arbitrary_self_types, generator_trait)]\n# use std::ops::GeneratorState;\n# use std::pin::Pin;\n\npub trait Generator<R = ()> {\n type Yield;\n type Return;\n fn resume(self: Pin<&mut Self>, resume: R) -> GeneratorState<Self::Yield, Self::Return>;\n}\n```\n\nThe `Generator::Yield` type is the type of values that can be yielded with the\n`yield` statement. The `Generator::Return` type is the returned type of the\ngenerator. This is typically the last expression in a generator's definition or\nany value passed to `return` in a generator. The `resume` function is the entry\npoint for executing the `Generator` itself.\n\nThe return value of `resume`, `GeneratorState`, looks like:\n\n```rust\npub enum GeneratorState<Y, R> {\n Yielded(Y),\n Complete(R),\n}\n```\n\nThe `Yielded` variant indicates that the generator can later be resumed. This\ncorresponds to a `yield` point in a generator. The `Complete` variant indicates\nthat the generator is complete and cannot be resumed again. Calling `resume`\nafter a generator has returned `Complete` will likely result in a panic of the\nprogram.\n\n### Closure-like semantics\n\nThe closure-like syntax for generators alludes to the fact that they also have\nclosure-like semantics. Namely:\n\n* When created, a generator executes no code. A closure literal does not\n actually execute any of the closure's code on construction, and similarly a\n generator literal does not execute any code inside the generator when\n constructed.\n\n* Generators can capture outer variables by reference or by move, and this can\n be tweaked with the `move` keyword at the beginning of the closure. Like\n closures all generators will have an implicit environment which is inferred by\n the compiler. Outer variables can be moved into a generator for use as the\n generator progresses.\n\n* Generator literals produce a value with a unique type which implements the\n `std::ops::Generator` trait. This allows actual execution of the generator\n through the `Generator::resume` method as well as also naming it in return\n types and such.\n\n* Traits like `Send` and `Sync` are automatically implemented for a `Generator`\n depending on the captured variables of the environment. Unlike closures,\n generators also depend on variables live across suspension points. This means\n that although the ambient environment may be `Send` or `Sync`, the generator\n itself may not be due to internal variables live across `yield` points being\n not-`Send` or not-`Sync`. Note that generators do\n not implement traits like `Copy` or `Clone` automatically.\n\n* Whenever a generator is dropped it will drop all captured environment\n variables.\n\n### Generators as state machines\n\nIn the compiler, generators are currently compiled as state machines. Each\n`yield` expression will correspond to a different state that stores all live\nvariables over that suspension point. Resumption of a generator will dispatch on\nthe current state and then execute internally until a `yield` is reached, at\nwhich point all state is saved off in the generator and a value is returned.\n\nLet's take a look at an example to see what's going on here:\n\n```rust\n#![feature(generators, generator_trait)]\n\nuse std::ops::Generator;\nuse std::pin::Pin;\n\nfn main() {\n let ret = \"foo\";\n let mut generator = move || {\n yield 1;\n return ret\n };\n\n Pin::new(&mut generator).resume(());\n Pin::new(&mut generator).resume(());\n}\n```\n\nThis generator literal will compile down to something similar to:\n\n```rust\n#![feature(arbitrary_self_types, generators, generator_trait)]\n\nuse std::ops::{Generator, GeneratorState};\nuse std::pin::Pin;\n\nfn main() {\n let ret = \"foo\";\n let mut generator = {\n enum __Generator {\n Start(&'static str),\n Yield1(&'static str),\n Done,\n }\n\n impl Generator for __Generator {\n type Yield = i32;\n type Return = &'static str;\n\n fn resume(mut self: Pin<&mut Self>, resume: ()) -> GeneratorState<i32, &'static str> {\n use std::mem;\n match mem::replace(&mut *self, __Generator::Done) {\n __Generator::Start(s) => {\n *self = __Generator::Yield1(s);\n GeneratorState::Yielded(1)\n }\n\n __Generator::Yield1(s) => {\n *self = __Generator::Done;\n GeneratorState::Complete(s)\n }\n\n __Generator::Done => {\n panic!(\"generator resumed after completion\")\n }\n }\n }\n }\n\n __Generator::Start(ret)\n };\n\n Pin::new(&mut generator).resume(());\n Pin::new(&mut generator).resume(());\n}\n```\n\nNotably here we can see that the compiler is generating a fresh type,\n`__Generator` in this case. This type has a number of states (represented here\nas an `enum`) corresponding to each of the conceptual states of the generator.\nAt the beginning we're closing over our outer variable `foo` and then that\nvariable is also live over the `yield` point, so it's stored in both states.\n\nWhen the generator starts it'll immediately yield 1, but it saves off its state\njust before it does so indicating that it has reached the yield point. Upon\nresuming again we'll execute the `return ret` which returns the `Complete`\nstate.\n\nHere we can also note that the `Done` state, if resumed, panics immediately as\nit's invalid to resume a completed generator. It's also worth noting that this\nis just a rough desugaring, not a normative specification for what the compiler\ndoes.\n" } , LintCompletion { label : "transparent_unions" , description : "# `transparent_unions`\n\nThe tracking issue for this feature is [#60405]\n\n[#60405]: https://github.com/rust-lang/rust/issues/60405\n\n----\n\nThe `transparent_unions` feature allows you mark `union`s as\n`#[repr(transparent)]`. A `union` may be `#[repr(transparent)]` in exactly the\nsame conditions in which a `struct` may be `#[repr(transparent)]` (generally,\nthis means the `union` must have exactly one non-zero-sized field). Some\nconcrete illustrations follow.\n\n```rust\n#![feature(transparent_unions)]\n\n// This union has the same representation as `f32`.\n#[repr(transparent)]\nunion SingleFieldUnion {\n field: f32,\n}\n\n// This union has the same representation as `usize`.\n#[repr(transparent)]\nunion MultiFieldUnion {\n field: usize,\n nothing: (),\n}\n```\n\nFor consistency with transparent `struct`s, `union`s must have exactly one\nnon-zero-sized field. If all fields are zero-sized, the `union` must not be\n`#[repr(transparent)]`:\n\n```rust\n#![feature(transparent_unions)]\n\n// This (non-transparent) union is already valid in stable Rust:\npub union GoodUnion {\n pub nothing: (),\n}\n\n// Error: transparent union needs exactly one non-zero-sized field, but has 0\n// #[repr(transparent)]\n// pub union BadUnion {\n// pub nothing: (),\n// }\n```\n\nThe one exception is if the `union` is generic over `T` and has a field of type\n`T`, it may be `#[repr(transparent)]` even if `T` is a zero-sized type:\n\n```rust\n#![feature(transparent_unions)]\n\n// This union has the same representation as `T`.\n#[repr(transparent)]\npub union GenericUnion<T: Copy> { // Unions with non-`Copy` fields are unstable.\n pub field: T,\n pub nothing: (),\n}\n\n// This is okay even though `()` is a zero-sized type.\npub const THIS_IS_OKAY: GenericUnion<()> = GenericUnion { field: () };\n```\n\nLike transarent `struct`s, a transparent `union` of type `U` has the same\nlayout, size, and ABI as its single non-ZST field. If it is generic over a type\n`T`, and all its fields are ZSTs except for exactly one field of type `T`, then\nit has the same layout and ABI as `T` (even if `T` is a ZST when monomorphized).\n\nLike transparent `struct`s, transparent `union`s are FFI-safe if and only if\ntheir underlying representation type is also FFI-safe.\n\nA `union` may not be eligible for the same nonnull-style optimizations that a\n`struct` or `enum` (with the same fields) are eligible for. Adding\n`#[repr(transparent)]` to `union` does not change this. To give a more concrete\nexample, it is unspecified whether `size_of::<T>()` is equal to\n`size_of::<Option<T>>()`, where `T` is a `union` (regardless of whether or not\nit is transparent). The Rust compiler is free to perform this optimization if\npossible, but is not required to, and different compiler versions may differ in\ntheir application of these optimizations.\n" } , LintCompletion { label : "plugin_registrar" , description : "# `plugin_registrar`\n\nThe tracking issue for this feature is: [#29597]\n\n[#29597]: https://github.com/rust-lang/rust/issues/29597\n\nThis feature is part of \"compiler plugins.\" It will often be used with the\n[`plugin`] and `rustc_private` features as well. For more details, see\ntheir docs.\n\n[`plugin`]: plugin.md\n\n------------------------\n" } , LintCompletion { label : "or_patterns" , description : "# `or_patterns`\n\nThe tracking issue for this feature is: [#54883]\n\n[#54883]: https://github.com/rust-lang/rust/issues/54883\n\n------------------------\n\nThe `or_pattern` language feature allows `|` to be arbitrarily nested within\na pattern, for example, `Some(A(0) | B(1 | 2))` becomes a valid pattern.\n\n## Examples\n\n```rust,ignore\n#![feature(or_patterns)]\n\npub enum Foo {\n Bar,\n Baz,\n Quux,\n}\n\npub fn example(maybe_foo: Option<Foo>) {\n match maybe_foo {\n Some(Foo::Bar | Foo::Baz) => {\n println!(\"The value contained `Bar` or `Baz`\");\n }\n Some(_) => {\n println!(\"The value did not contain `Bar` or `Baz`\");\n }\n None => {\n println!(\"The value was `None`\");\n }\n }\n}\n```\n" } , LintCompletion { label : "repr128" , description : "# `repr128`\n\nThe tracking issue for this feature is: [#56071]\n\n[#56071]: https://github.com/rust-lang/rust/issues/56071\n\n------------------------\n\nThe `repr128` feature adds support for `#[repr(u128)]` on `enum`s.\n\n```rust\n#![feature(repr128)]\n\n#[repr(u128)]\nenum Foo {\n Bar(u64),\n}\n```\n" } , LintCompletion { label : "unboxed_closures" , description : "# `unboxed_closures`\n\nThe tracking issue for this feature is [#29625]\n\nSee Also: [`fn_traits`](../library-features/fn-traits.md)\n\n[#29625]: https://github.com/rust-lang/rust/issues/29625\n\n----\n\nThe `unboxed_closures` feature allows you to write functions using the `\"rust-call\"` ABI,\nrequired for implementing the [`Fn*`] family of traits. `\"rust-call\"` functions must have \nexactly one (non self) argument, a tuple representing the argument list.\n\n[`Fn*`]: https://doc.rust-lang.org/std/ops/trait.Fn.html\n\n```rust\n#![feature(unboxed_closures)]\n\nextern \"rust-call\" fn add_args(args: (u32, u32)) -> u32 {\n args.0 + args.1\n}\n\nfn main() {}\n```\n" } , LintCompletion { label : "link_cfg" , description : "# `link_cfg`\n\nThis feature is internal to the Rust compiler and is not intended for general use.\n\n------------------------\n" } , LintCompletion { label : "rustc_attrs" , description : "# `rustc_attrs`\n\nThis feature has no tracking issue, and is therefore internal to\nthe compiler, not being intended for general use.\n\nNote: `rustc_attrs` enables many rustc-internal attributes and this page\nonly discuss a few of them.\n\n------------------------\n\nThe `rustc_attrs` feature allows debugging rustc type layouts by using\n`#[rustc_layout(...)]` to debug layout at compile time (it even works\nwith `cargo check`) as an alternative to `rustc -Z print-type-sizes`\nthat is way more verbose.\n\nOptions provided by `#[rustc_layout(...)]` are `debug`, `size`, `align`,\n`abi`. Note that it only works on sized types without generics.\n\n## Examples\n\n```rust,ignore\n#![feature(rustc_attrs)]\n\n#[rustc_layout(abi, size)]\npub enum X {\n Y(u8, u8, u8),\n Z(isize),\n}\n```\n\nWhen that is compiled, the compiler will error with something like\n\n```text\nerror: abi: Aggregate { sized: true }\n --> src/lib.rs:4:1\n |\n4 | / pub enum T {\n5 | | Y(u8, u8, u8),\n6 | | Z(isize),\n7 | | }\n | |_^\n\nerror: size: Size { raw: 16 }\n --> src/lib.rs:4:1\n |\n4 | / pub enum T {\n5 | | Y(u8, u8, u8),\n6 | | Z(isize),\n7 | | }\n | |_^\n\nerror: aborting due to 2 previous errors\n```\n" } , LintCompletion { label : "box_patterns" , description : "# `box_patterns`\n\nThe tracking issue for this feature is: [#29641]\n\n[#29641]: https://github.com/rust-lang/rust/issues/29641\n\nSee also [`box_syntax`](box-syntax.md)\n\n------------------------\n\nBox patterns let you match on `Box<T>`s:\n\n\n```rust\n#![feature(box_patterns)]\n\nfn main() {\n let b = Some(Box::new(5));\n match b {\n Some(box n) if n < 0 => {\n println!(\"Box contains negative number {}\", n);\n },\n Some(box n) if n >= 0 => {\n println!(\"Box contains non-negative number {}\", n);\n },\n None => {\n println!(\"No box\");\n },\n _ => unreachable!()\n }\n}\n```\n" } , LintCompletion { label : "infer_static_outlives_requirements" , description : "# `infer_static_outlives_requirements`\n\nThe tracking issue for this feature is: [#54185]\n\n[#54185]: https://github.com/rust-lang/rust/issues/54185\n\n------------------------\nThe `infer_static_outlives_requirements` feature indicates that certain\n`'static` outlives requirements can be inferred by the compiler rather than\nstating them explicitly.\n\nNote: It is an accompanying feature to `infer_outlives_requirements`,\nwhich must be enabled to infer outlives requirements.\n\nFor example, currently generic struct definitions that contain\nreferences, require where-clauses of the form T: 'static. By using\nthis feature the outlives predicates will be inferred, although\nthey may still be written explicitly.\n\n```rust,ignore (pseudo-Rust)\nstruct Foo<U> where U: 'static { // <-- currently required\n bar: Bar<U>\n}\nstruct Bar<T: 'static> {\n x: T,\n}\n```\n\n\n## Examples:\n\n```rust,ignore (pseudo-Rust)\n#![feature(infer_outlives_requirements)]\n#![feature(infer_static_outlives_requirements)]\n\n#[rustc_outlives]\n// Implicitly infer U: 'static\nstruct Foo<U> {\n bar: Bar<U>\n}\nstruct Bar<T: 'static> {\n x: T,\n}\n```\n\n" } , LintCompletion { label : "trait_alias" , description : "# `trait_alias`\n\nThe tracking issue for this feature is: [#41517]\n\n[#41517]: https://github.com/rust-lang/rust/issues/41517\n\n------------------------\n\nThe `trait_alias` feature adds support for trait aliases. These allow aliases\nto be created for one or more traits (currently just a single regular trait plus\nany number of auto-traits), and used wherever traits would normally be used as\neither bounds or trait objects.\n\n```rust\n#![feature(trait_alias)]\n\ntrait Foo = std::fmt::Debug + Send;\ntrait Bar = Foo + Sync;\n\n// Use trait alias as bound on type parameter.\nfn foo<T: Foo>(v: &T) {\n println!(\"{:?}\", v);\n}\n\npub fn main() {\n foo(&1);\n\n // Use trait alias for trait objects.\n let a: &Bar = &123;\n println!(\"{:?}\", a);\n let b = Box::new(456) as Box<dyn Foo>;\n println!(\"{:?}\", b);\n}\n```\n" } , LintCompletion { label : "const_fn" , description : "# `const_fn`\n\nThe tracking issue for this feature is: [#57563]\n\n[#57563]: https://github.com/rust-lang/rust/issues/57563\n\n------------------------\n\nThe `const_fn` feature allows marking free functions and inherent methods as\n`const`, enabling them to be called in constants contexts, with constant\narguments.\n\n## Examples\n\n```rust\n#![feature(const_fn)]\n\nconst fn double(x: i32) -> i32 {\n x * 2\n}\n\nconst FIVE: i32 = 5;\nconst TEN: i32 = double(FIVE);\n\nfn main() {\n assert_eq!(5, FIVE);\n assert_eq!(10, TEN);\n}\n```\n" } , LintCompletion { label : "doc_cfg" , description : "# `doc_cfg`\n\nThe tracking issue for this feature is: [#43781]\n\n------\n\nThe `doc_cfg` feature allows an API be documented as only available in some specific platforms.\nThis attribute has two effects:\n\n1. In the annotated item's documentation, there will be a message saying \"This is supported on\n (platform) only\".\n\n2. The item's doc-tests will only run on the specific platform.\n\nIn addition to allowing the use of the `#[doc(cfg)]` attribute, this feature enables the use of a\nspecial conditional compilation flag, `#[cfg(doc)]`, set whenever building documentation on your\ncrate.\n\nThis feature was introduced as part of PR [#43348] to allow the platform-specific parts of the\nstandard library be documented.\n\n```rust\n#![feature(doc_cfg)]\n\n#[cfg(any(windows, doc))]\n#[doc(cfg(windows))]\n/// The application's icon in the notification area (a.k.a. system tray).\n///\n/// # Examples\n///\n/// ```no_run\n/// extern crate my_awesome_ui_library;\n/// use my_awesome_ui_library::current_app;\n/// use my_awesome_ui_library::windows::notification;\n///\n/// let icon = current_app().get::<notification::Icon>();\n/// icon.show();\n/// icon.show_message(\"Hello\");\n/// ```\npub struct Icon {\n // ...\n}\n```\n\n[#43781]: https://github.com/rust-lang/rust/issues/43781\n[#43348]: https://github.com/rust-lang/rust/issues/43348\n" } , LintCompletion { label : "allocator_internals" , description : "# `allocator_internals`\n\nThis feature does not have a tracking issue, it is an unstable implementation\ndetail of the `global_allocator` feature not intended for use outside the\ncompiler.\n\n------------------------\n" } , LintCompletion { label : "doc_masked" , description : "# `doc_masked`\n\nThe tracking issue for this feature is: [#44027]\n\n-----\n\nThe `doc_masked` feature allows a crate to exclude types from a given crate from appearing in lists\nof trait implementations. The specifics of the feature are as follows:\n\n1. When rustdoc encounters an `extern crate` statement annotated with a `#[doc(masked)]` attribute,\n it marks the crate as being masked.\n\n2. When listing traits a given type implements, rustdoc ensures that traits from masked crates are\n not emitted into the documentation.\n\n3. When listing types that implement a given trait, rustdoc ensures that types from masked crates\n are not emitted into the documentation.\n\nThis feature was introduced in PR [#44026] to ensure that compiler-internal and\nimplementation-specific types and traits were not included in the standard library's documentation.\nSuch types would introduce broken links into the documentation.\n\n[#44026]: https://github.com/rust-lang/rust/pull/44026\n[#44027]: https://github.com/rust-lang/rust/pull/44027\n" } , LintCompletion { label : "no_sanitize" , description : "# `no_sanitize`\n\nThe tracking issue for this feature is: [#39699]\n\n[#39699]: https://github.com/rust-lang/rust/issues/39699\n\n------------------------\n\nThe `no_sanitize` attribute can be used to selectively disable sanitizer\ninstrumentation in an annotated function. This might be useful to: avoid\ninstrumentation overhead in a performance critical function, or avoid\ninstrumenting code that contains constructs unsupported by given sanitizer.\n\nThe precise effect of this annotation depends on particular sanitizer in use.\nFor example, with `no_sanitize(thread)`, the thread sanitizer will no longer\ninstrument non-atomic store / load operations, but it will instrument atomic\noperations to avoid reporting false positives and provide meaning full stack\ntraces.\n\n## Examples\n\n``` rust\n#![feature(no_sanitize)]\n\n#[no_sanitize(address)]\nfn foo() {\n // ...\n}\n```\n" } , LintCompletion { label : "intrinsics" , description : "# `intrinsics`\n\nThe tracking issue for this feature is: None.\n\nIntrinsics are never intended to be stable directly, but intrinsics are often\nexported in some sort of stable manner. Prefer using the stable interfaces to\nthe intrinsic directly when you can.\n\n------------------------\n\n\nThese are imported as if they were FFI functions, with the special\n`rust-intrinsic` ABI. For example, if one was in a freestanding\ncontext, but wished to be able to `transmute` between types, and\nperform efficient pointer arithmetic, one would import those functions\nvia a declaration like\n\n```rust\n#![feature(intrinsics)]\n# fn main() {}\n\nextern \"rust-intrinsic\" {\n fn transmute<T, U>(x: T) -> U;\n\n fn offset<T>(dst: *const T, offset: isize) -> *const T;\n}\n```\n\nAs with any other FFI functions, these are always `unsafe` to call.\n\n" } , LintCompletion { label : "external_doc" , description : "# `external_doc`\n\nThe tracking issue for this feature is: [#44732]\n\nThe `external_doc` feature allows the use of the `include` parameter to the `#[doc]` attribute, to\ninclude external files in documentation. Use the attribute in place of, or in addition to, regular\ndoc comments and `#[doc]` attributes, and `rustdoc` will load the given file when it renders\ndocumentation for your crate.\n\nWith the following files in the same directory:\n\n`external-doc.md`:\n\n```markdown\n# My Awesome Type\n\nThis is the documentation for this spectacular type.\n```\n\n`lib.rs`:\n\n```no_run (needs-external-files)\n#![feature(external_doc)]\n\n#[doc(include = \"external-doc.md\")]\npub struct MyAwesomeType;\n```\n\n`rustdoc` will load the file `external-doc.md` and use it as the documentation for the `MyAwesomeType`\nstruct.\n\nWhen locating files, `rustdoc` will base paths in the `src/` directory, as if they were alongside the\n`lib.rs` for your crate. So if you want a `docs/` folder to live alongside the `src/` directory,\nstart your paths with `../docs/` for `rustdoc` to properly find the file.\n\nThis feature was proposed in [RFC #1990] and initially implemented in PR [#44781].\n\n[#44732]: https://github.com/rust-lang/rust/issues/44732\n[RFC #1990]: https://github.com/rust-lang/rfcs/pull/1990\n[#44781]: https://github.com/rust-lang/rust/pull/44781\n" } , LintCompletion { label : "inline_const" , description : "# `inline_const`\n\nThe tracking issue for this feature is: [#76001]\n\n------\n\nThis feature allows you to use inline constant expressions. For example, you can\nturn this code:\n\n```rust\n# fn add_one(x: i32) -> i32 { x + 1 }\nconst MY_COMPUTATION: i32 = 1 + 2 * 3 / 4;\n\nfn main() {\n let x = add_one(MY_COMPUTATION);\n}\n```\n\ninto this code:\n\n```rust\n#![feature(inline_const)]\n\n# fn add_one(x: i32) -> i32 { x + 1 }\nfn main() {\n let x = add_one(const { 1 + 2 * 3 / 4 });\n}\n```\n\nYou can also use inline constant expressions in patterns:\n\n```rust\n#![feature(inline_const)]\n\nconst fn one() -> i32 { 1 }\n\nlet some_int = 3;\nmatch some_int {\n const { 1 + 2 } => println!(\"Matched 1 + 2\"),\n const { one() } => println!(\"Matched const fn returning 1\"),\n _ => println!(\"Didn't match anything :(\"),\n}\n```\n\n[#76001]: https://github.com/rust-lang/rust/issues/76001\n" } , LintCompletion { label : "abi_thiscall" , description : "# `abi_thiscall`\n\nThe tracking issue for this feature is: [#42202]\n\n[#42202]: https://github.com/rust-lang/rust/issues/42202\n\n------------------------\n\nThe MSVC ABI on x86 Windows uses the `thiscall` calling convention for C++\ninstance methods by default; it is identical to the usual (C) calling\nconvention on x86 Windows except that the first parameter of the method,\nthe `this` pointer, is passed in the ECX register.\n" } , LintCompletion { label : "plugin" , description : "# `plugin`\n\nThe tracking issue for this feature is: [#29597]\n\n[#29597]: https://github.com/rust-lang/rust/issues/29597\n\n\nThis feature is part of \"compiler plugins.\" It will often be used with the\n[`plugin_registrar`] and `rustc_private` features.\n\n[`plugin_registrar`]: plugin-registrar.md\n\n------------------------\n\n`rustc` can load compiler plugins, which are user-provided libraries that\nextend the compiler's behavior with new lint checks, etc.\n\nA plugin is a dynamic library crate with a designated *registrar* function that\nregisters extensions with `rustc`. Other crates can load these extensions using\nthe crate attribute `#![plugin(...)]`. See the\n`rustc_driver::plugin` documentation for more about the\nmechanics of defining and loading a plugin.\n\nIn the vast majority of cases, a plugin should *only* be used through\n`#![plugin]` and not through an `extern crate` item. Linking a plugin would\npull in all of librustc_ast and librustc as dependencies of your crate. This is\ngenerally unwanted unless you are building another plugin.\n\nThe usual practice is to put compiler plugins in their own crate, separate from\nany `macro_rules!` macros or ordinary Rust code meant to be used by consumers\nof a library.\n\n# Lint plugins\n\nPlugins can extend [Rust's lint\ninfrastructure](../../reference/attributes/diagnostics.md#lint-check-attributes) with\nadditional checks for code style, safety, etc. Now let's write a plugin\n[`lint-plugin-test.rs`](https://github.com/rust-lang/rust/blob/master/src/test/ui-fulldeps/auxiliary/lint-plugin-test.rs)\nthat warns about any item named `lintme`.\n\n```rust,ignore\n#![feature(plugin_registrar)]\n#![feature(box_syntax, rustc_private)]\n\nextern crate rustc_ast;\n\n// Load rustc as a plugin to get macros\nextern crate rustc_driver;\n#[macro_use]\nextern crate rustc_lint;\n#[macro_use]\nextern crate rustc_session;\n\nuse rustc_driver::plugin::Registry;\nuse rustc_lint::{EarlyContext, EarlyLintPass, LintArray, LintContext, LintPass};\nuse rustc_ast::ast;\ndeclare_lint!(TEST_LINT, Warn, \"Warn about items named 'lintme'\");\n\ndeclare_lint_pass!(Pass => [TEST_LINT]);\n\nimpl EarlyLintPass for Pass {\n fn check_item(&mut self, cx: &EarlyContext, it: &ast::Item) {\n if it.ident.name.as_str() == \"lintme\" {\n cx.lint(TEST_LINT, |lint| {\n lint.build(\"item is named 'lintme'\").set_span(it.span).emit()\n });\n }\n }\n}\n\n#[plugin_registrar]\npub fn plugin_registrar(reg: &mut Registry) {\n reg.lint_store.register_lints(&[&TEST_LINT]);\n reg.lint_store.register_early_pass(|| box Pass);\n}\n```\n\nThen code like\n\n```rust,ignore\n#![feature(plugin)]\n#![plugin(lint_plugin_test)]\n\nfn lintme() { }\n```\n\nwill produce a compiler warning:\n\n```txt\nfoo.rs:4:1: 4:16 warning: item is named 'lintme', #[warn(test_lint)] on by default\nfoo.rs:4 fn lintme() { }\n ^~~~~~~~~~~~~~~\n```\n\nThe components of a lint plugin are:\n\n* one or more `declare_lint!` invocations, which define static `Lint` structs;\n\n* a struct holding any state needed by the lint pass (here, none);\n\n* a `LintPass`\n implementation defining how to check each syntax element. A single\n `LintPass` may call `span_lint` for several different `Lint`s, but should\n register them all through the `get_lints` method.\n\nLint passes are syntax traversals, but they run at a late stage of compilation\nwhere type information is available. `rustc`'s [built-in\nlints](https://github.com/rust-lang/rust/blob/master/src/librustc_session/lint/builtin.rs)\nmostly use the same infrastructure as lint plugins, and provide examples of how\nto access type information.\n\nLints defined by plugins are controlled by the usual [attributes and compiler\nflags](../../reference/attributes/diagnostics.md#lint-check-attributes), e.g.\n`#[allow(test_lint)]` or `-A test-lint`. These identifiers are derived from the\nfirst argument to `declare_lint!`, with appropriate case and punctuation\nconversion.\n\nYou can run `rustc -W help foo.rs` to see a list of lints known to `rustc`,\nincluding those provided by plugins loaded by `foo.rs`.\n" } , LintCompletion { label : "optin_builtin_traits" , description : "# `optin_builtin_traits`\n\nThe tracking issue for this feature is [#13231] \n\n[#13231]: https://github.com/rust-lang/rust/issues/13231\n\n----\n\nThe `optin_builtin_traits` feature gate allows you to define auto traits.\n\nAuto traits, like [`Send`] or [`Sync`] in the standard library, are marker traits\nthat are automatically implemented for every type, unless the type, or a type it contains, \nhas explicitly opted out via a negative impl. (Negative impls are separately controlled\nby the `negative_impls` feature.)\n\n[`Send`]: https://doc.rust-lang.org/std/marker/trait.Send.html\n[`Sync`]: https://doc.rust-lang.org/std/marker/trait.Sync.html\n\n```rust,ignore\nimpl !Trait for Type\n```\n\nExample:\n\n```rust\n#![feature(negative_impls)]\n#![feature(optin_builtin_traits)]\n\nauto trait Valid {}\n\nstruct True;\nstruct False;\n\nimpl !Valid for False {}\n\nstruct MaybeValid<T>(T);\n\nfn must_be_valid<T: Valid>(_t: T) { }\n\nfn main() {\n // works\n must_be_valid( MaybeValid(True) );\n \n // compiler error - trait bound not satisfied\n // must_be_valid( MaybeValid(False) );\n}\n```\n\n## Automatic trait implementations\n\nWhen a type is declared as an `auto trait`, we will automatically\ncreate impls for every struct/enum/union, unless an explicit impl is\nprovided. These automatic impls contain a where clause for each field\nof the form `T: AutoTrait`, where `T` is the type of the field and\n`AutoTrait` is the auto trait in question. As an example, consider the\nstruct `List` and the auto trait `Send`:\n\n```rust\nstruct List<T> {\n data: T,\n next: Option<Box<List<T>>>,\n}\n```\n\nPresuming that there is no explicit impl of `Send` for `List`, the\ncompiler will supply an automatic impl of the form:\n\n```rust\nstruct List<T> {\n data: T,\n next: Option<Box<List<T>>>,\n}\n\nunsafe impl<T> Send for List<T>\nwhere\n T: Send, // from the field `data`\n Option<Box<List<T>>>: Send, // from the field `next`\n{ }\n```\n\nExplicit impls may be either positive or negative. They take the form:\n\n```rust,ignore\nimpl<...> AutoTrait for StructName<..> { }\nimpl<...> !AutoTrait for StructName<..> { }\n```\n\n## Coinduction: Auto traits permit cyclic matching\n\nUnlike ordinary trait matching, auto traits are **coinductive**. This\nmeans, in short, that cycles which occur in trait matching are\nconsidered ok. As an example, consider the recursive struct `List`\nintroduced in the previous section. In attempting to determine whether\n`List: Send`, we would wind up in a cycle: to apply the impl, we must\nshow that `Option<Box<List>>: Send`, which will in turn require\n`Box<List>: Send` and then finally `List: Send` again. Under ordinary\ntrait matching, this cycle would be an error, but for an auto trait it\nis considered a successful match.\n\n## Items\n\nAuto traits cannot have any trait items, such as methods or associated types. This ensures that we can generate default implementations.\n\n## Supertraits\n\nAuto traits cannot have supertraits. This is for soundness reasons, as the interaction of coinduction with implied bounds is difficult to reconcile.\n\n" } , LintCompletion { label : "impl_trait_in_bindings" , description : "# `impl_trait_in_bindings`\n\nThe tracking issue for this feature is: [#63065]\n\n[#63065]: https://github.com/rust-lang/rust/issues/63065\n\n------------------------\n\nThe `impl_trait_in_bindings` feature gate lets you use `impl Trait` syntax in\n`let`, `static`, and `const` bindings.\n\nA simple example is:\n\n```rust\n#![feature(impl_trait_in_bindings)]\n\nuse std::fmt::Debug;\n\nfn main() {\n let a: impl Debug + Clone = 42;\n let b = a.clone();\n println!(\"{:?}\", b); // prints `42`\n}\n```\n\nNote however that because the types of `a` and `b` are opaque in the above\nexample, calling inherent methods or methods outside of the specified traits\n(e.g., `a.abs()` or `b.abs()`) is not allowed, and yields an error.\n" } , LintCompletion { label : "abi_ptx" , description : "# `abi_ptx`\n\nThe tracking issue for this feature is: [#38788]\n\n[#38788]: https://github.com/rust-lang/rust/issues/38788\n\n------------------------\n\nWhen emitting PTX code, all vanilla Rust functions (`fn`) get translated to\n\"device\" functions. These functions are *not* callable from the host via the\nCUDA API so a crate with only device functions is not too useful!\n\nOTOH, \"global\" functions *can* be called by the host; you can think of them\nas the real public API of your crate. To produce a global function use the\n`\"ptx-kernel\"` ABI.\n\n<!-- NOTE(ignore) this example is specific to the nvptx targets -->\n\n``` rust,ignore\n#![feature(abi_ptx)]\n#![no_std]\n\npub unsafe extern \"ptx-kernel\" fn global_function() {\n device_function();\n}\n\npub fn device_function() {\n // ..\n}\n```\n\n``` text\n$ xargo rustc --target nvptx64-nvidia-cuda --release -- --emit=asm\n\n$ cat $(find -name '*.s')\n//\n// Generated by LLVM NVPTX Back-End\n//\n\n.version 3.2\n.target sm_20\n.address_size 64\n\n // .globl _ZN6kernel15global_function17h46111ebe6516b382E\n\n.visible .entry _ZN6kernel15global_function17h46111ebe6516b382E()\n{\n\n\n ret;\n}\n\n // .globl _ZN6kernel15device_function17hd6a0e4993bbf3f78E\n.visible .func _ZN6kernel15device_function17hd6a0e4993bbf3f78E()\n{\n\n\n ret;\n}\n```\n" } , LintCompletion { label : "dec2flt" , description : "# `dec2flt`\n\nThis feature is internal to the Rust compiler and is not intended for general use.\n\n------------------------\n" } , LintCompletion { label : "int_error_internals" , description : "# `int_error_internals`\n\nThis feature is internal to the Rust compiler and is not intended for general use.\n\n------------------------\n" } , LintCompletion { label : "llvm_asm" , description : "# `llvm_asm`\n\nThe tracking issue for this feature is: [#70173]\n\n[#70173]: https://github.com/rust-lang/rust/issues/70173\n\n------------------------\n\nFor extremely low-level manipulations and performance reasons, one\nmight wish to control the CPU directly. Rust supports using inline\nassembly to do this via the `llvm_asm!` macro.\n\n```rust,ignore\nllvm_asm!(assembly template\n : output operands\n : input operands\n : clobbers\n : options\n );\n```\n\nAny use of `llvm_asm` is feature gated (requires `#![feature(llvm_asm)]` on the\ncrate to allow) and of course requires an `unsafe` block.\n\n> **Note**: the examples here are given in x86/x86-64 assembly, but\n> all platforms are supported.\n\n## Assembly template\n\nThe `assembly template` is the only required parameter and must be a\nliteral string (i.e. `\"\"`)\n\n```rust\n#![feature(llvm_asm)]\n\n#[cfg(any(target_arch = \"x86\", target_arch = \"x86_64\"))]\nfn foo() {\n unsafe {\n llvm_asm!(\"NOP\");\n }\n}\n\n// Other platforms:\n#[cfg(not(any(target_arch = \"x86\", target_arch = \"x86_64\")))]\nfn foo() { /* ... */ }\n\nfn main() {\n // ...\n foo();\n // ...\n}\n```\n\n(The `feature(llvm_asm)` and `#[cfg]`s are omitted from now on.)\n\nOutput operands, input operands, clobbers and options are all optional\nbut you must add the right number of `:` if you skip them:\n\n```rust\n# #![feature(llvm_asm)]\n# #[cfg(any(target_arch = \"x86\", target_arch = \"x86_64\"))]\n# fn main() { unsafe {\nllvm_asm!(\"xor %eax, %eax\"\n :\n :\n : \"eax\"\n );\n# } }\n# #[cfg(not(any(target_arch = \"x86\", target_arch = \"x86_64\")))]\n# fn main() {}\n```\n\nWhitespace also doesn't matter:\n\n```rust\n# #![feature(llvm_asm)]\n# #[cfg(any(target_arch = \"x86\", target_arch = \"x86_64\"))]\n# fn main() { unsafe {\nllvm_asm!(\"xor %eax, %eax\" ::: \"eax\");\n# } }\n# #[cfg(not(any(target_arch = \"x86\", target_arch = \"x86_64\")))]\n# fn main() {}\n```\n\n## Operands\n\nInput and output operands follow the same format: `:\n\"constraints1\"(expr1), \"constraints2\"(expr2), ...\"`. Output operand\nexpressions must be mutable place, or not yet assigned:\n\n```rust\n# #![feature(llvm_asm)]\n# #[cfg(any(target_arch = \"x86\", target_arch = \"x86_64\"))]\nfn add(a: i32, b: i32) -> i32 {\n let c: i32;\n unsafe {\n llvm_asm!(\"add $2, $0\"\n : \"=r\"(c)\n : \"0\"(a), \"r\"(b)\n );\n }\n c\n}\n# #[cfg(not(any(target_arch = \"x86\", target_arch = \"x86_64\")))]\n# fn add(a: i32, b: i32) -> i32 { a + b }\n\nfn main() {\n assert_eq!(add(3, 14159), 14162)\n}\n```\n\nIf you would like to use real operands in this position, however,\nyou are required to put curly braces `{}` around the register that\nyou want, and you are required to put the specific size of the\noperand. This is useful for very low level programming, where\nwhich register you use is important:\n\n```rust\n# #![feature(llvm_asm)]\n# #[cfg(any(target_arch = \"x86\", target_arch = \"x86_64\"))]\n# unsafe fn read_byte_in(port: u16) -> u8 {\nlet result: u8;\nllvm_asm!(\"in %dx, %al\" : \"={al}\"(result) : \"{dx}\"(port));\nresult\n# }\n```\n\n## Clobbers\n\nSome instructions modify registers which might otherwise have held\ndifferent values so we use the clobbers list to indicate to the\ncompiler not to assume any values loaded into those registers will\nstay valid.\n\n```rust\n# #![feature(llvm_asm)]\n# #[cfg(any(target_arch = \"x86\", target_arch = \"x86_64\"))]\n# fn main() { unsafe {\n// Put the value 0x200 in eax:\nllvm_asm!(\"mov $$0x200, %eax\" : /* no outputs */ : /* no inputs */ : \"eax\");\n# } }\n# #[cfg(not(any(target_arch = \"x86\", target_arch = \"x86_64\")))]\n# fn main() {}\n```\n\nInput and output registers need not be listed since that information\nis already communicated by the given constraints. Otherwise, any other\nregisters used either implicitly or explicitly should be listed.\n\nIf the assembly changes the condition code register `cc` should be\nspecified as one of the clobbers. Similarly, if the assembly modifies\nmemory, `memory` should also be specified.\n\n## Options\n\nThe last section, `options` is specific to Rust. The format is comma\nseparated literal strings (i.e. `:\"foo\", \"bar\", \"baz\"`). It's used to\nspecify some extra info about the inline assembly:\n\nCurrent valid options are:\n\n1. `volatile` - specifying this is analogous to\n `__asm__ __volatile__ (...)` in gcc/clang.\n2. `alignstack` - certain instructions expect the stack to be\n aligned a certain way (i.e. SSE) and specifying this indicates to\n the compiler to insert its usual stack alignment code\n3. `intel` - use intel syntax instead of the default AT&T.\n\n```rust\n# #![feature(llvm_asm)]\n# #[cfg(any(target_arch = \"x86\", target_arch = \"x86_64\"))]\n# fn main() {\nlet result: i32;\nunsafe {\n llvm_asm!(\"mov eax, 2\" : \"={eax}\"(result) : : : \"intel\")\n}\nprintln!(\"eax is currently {}\", result);\n# }\n# #[cfg(not(any(target_arch = \"x86\", target_arch = \"x86_64\")))]\n# fn main() {}\n```\n\n## More Information\n\nThe current implementation of the `llvm_asm!` macro is a direct binding to [LLVM's\ninline assembler expressions][llvm-docs], so be sure to check out [their\ndocumentation as well][llvm-docs] for more information about clobbers,\nconstraints, etc.\n\n[llvm-docs]: http://llvm.org/docs/LangRef.html#inline-assembler-expressions\n\nIf you need more power and don't mind losing some of the niceties of\n`llvm_asm!`, check out [global_asm](global-asm.md).\n" } , LintCompletion { label : "default_free_fn" , description : "# `default_free_fn`\n\nThe tracking issue for this feature is: [#73014]\n\n[#73014]: https://github.com/rust-lang/rust/issues/73014\n\n------------------------\n\nAdds a free `default()` function to the `std::default` module. This function\njust forwards to [`Default::default()`], but may remove repetition of the word\n\"default\" from the call site.\n\n[`Default::default()`]: https://doc.rust-lang.org/nightly/std/default/trait.Default.html#tymethod.default\n\nHere is an example:\n\n```rust\n#![feature(default_free_fn)]\nuse std::default::default;\n\n#[derive(Default)]\nstruct AppConfig {\n foo: FooConfig,\n bar: BarConfig,\n}\n\n#[derive(Default)]\nstruct FooConfig {\n foo: i32,\n}\n\n#[derive(Default)]\nstruct BarConfig {\n bar: f32,\n baz: u8,\n}\n\nfn main() {\n let options = AppConfig {\n foo: default(),\n bar: BarConfig {\n bar: 10.1,\n ..default()\n },\n };\n}\n```\n" } , LintCompletion { label : "libstd_thread_internals" , description : "# `libstd_thread_internals`\n\nThis feature is internal to the Rust compiler and is not intended for general use.\n\n------------------------\n" } , LintCompletion { label : "char_error_internals" , description : "# `char_error_internals`\n\nThis feature is internal to the Rust compiler and is not intended for general use.\n\n------------------------\n" } , LintCompletion { label : "windows_handle" , description : "# `windows_handle`\n\nThis feature is internal to the Rust compiler and is not intended for general use.\n\n------------------------\n" } , LintCompletion { label : "global_asm" , description : "# `global_asm`\n\nThe tracking issue for this feature is: [#35119]\n\n[#35119]: https://github.com/rust-lang/rust/issues/35119\n\n------------------------\n\nThe `global_asm!` macro allows the programmer to write arbitrary\nassembly outside the scope of a function body, passing it through\n`rustc` and `llvm` to the assembler. The macro is a no-frills\ninterface to LLVM's concept of [module-level inline assembly]. That is,\nall caveats applicable to LLVM's module-level inline assembly apply\nto `global_asm!`.\n\n[module-level inline assembly]: http://llvm.org/docs/LangRef.html#module-level-inline-assembly\n\n`global_asm!` fills a role not currently satisfied by either `asm!`\nor `#[naked]` functions. The programmer has _all_ features of the\nassembler at their disposal. The linker will expect to resolve any\nsymbols defined in the inline assembly, modulo any symbols marked as\nexternal. It also means syntax for directives and assembly follow the\nconventions of the assembler in your toolchain.\n\nA simple usage looks like this:\n\n```rust,ignore\n# #![feature(global_asm)]\n# you also need relevant target_arch cfgs\nglobal_asm!(include_str!(\"something_neato.s\"));\n```\n\nAnd a more complicated usage looks like this:\n\n```rust,ignore\n# #![feature(global_asm)]\n# #![cfg(any(target_arch = \"x86\", target_arch = \"x86_64\"))]\n\npub mod sally {\n global_asm!(r#\"\n .global foo\n foo:\n jmp baz\n \"#);\n\n #[no_mangle]\n pub unsafe extern \"C\" fn baz() {}\n}\n\n// the symbols `foo` and `bar` are global, no matter where\n// `global_asm!` was used.\nextern \"C\" {\n fn foo();\n fn bar();\n}\n\npub mod harry {\n global_asm!(r#\"\n .global bar\n bar:\n jmp quux\n \"#);\n\n #[no_mangle]\n pub unsafe extern \"C\" fn quux() {}\n}\n```\n\nYou may use `global_asm!` multiple times, anywhere in your crate, in\nwhatever way suits you. The effect is as if you concatenated all\nusages and placed the larger, single usage in the crate root.\n\n------------------------\n\nIf you don't need quite as much power and flexibility as\n`global_asm!` provides, and you don't mind restricting your inline\nassembly to `fn` bodies only, you might try the\n[asm](asm.md) feature instead.\n" } , LintCompletion { label : "windows_c" , description : "# `windows_c`\n\nThis feature is internal to the Rust compiler and is not intended for general use.\n\n------------------------\n" } , LintCompletion { label : "asm" , description : "# `asm`\n\nThe tracking issue for this feature is: [#72016]\n\n[#72016]: https://github.com/rust-lang/rust/issues/72016\n\n------------------------\n\nFor extremely low-level manipulations and performance reasons, one\nmight wish to control the CPU directly. Rust supports using inline\nassembly to do this via the `asm!` macro.\n\n# Guide-level explanation\n[guide-level-explanation]: #guide-level-explanation\n\nRust provides support for inline assembly via the `asm!` macro.\nIt can be used to embed handwritten assembly in the assembly output generated by the compiler.\nGenerally this should not be necessary, but might be where the required performance or timing\ncannot be otherwise achieved. Accessing low level hardware primitives, e.g. in kernel code, may also demand this functionality.\n\n> **Note**: the examples here are given in x86/x86-64 assembly, but other architectures are also supported.\n\nInline assembly is currently supported on the following architectures:\n- x86 and x86-64\n- ARM\n- AArch64\n- RISC-V\n- NVPTX\n- Hexagon\n- MIPS32r2 and MIPS64r2\n\n## Basic usage\n\nLet us start with the simplest possible example:\n\n```rust,allow_fail\n# #![feature(asm)]\nunsafe {\n asm!(\"nop\");\n}\n```\n\nThis will insert a NOP (no operation) instruction into the assembly generated by the compiler.\nNote that all `asm!` invocations have to be inside an `unsafe` block, as they could insert\narbitrary instructions and break various invariants. The instructions to be inserted are listed\nin the first argument of the `asm!` macro as a string literal.\n\n## Inputs and outputs\n\nNow inserting an instruction that does nothing is rather boring. Let us do something that\nactually acts on data:\n\n```rust,allow_fail\n# #![feature(asm)]\nlet x: u64;\nunsafe {\n asm!(\"mov {}, 5\", out(reg) x);\n}\nassert_eq!(x, 5);\n```\n\nThis will write the value `5` into the `u64` variable `x`.\nYou can see that the string literal we use to specify instructions is actually a template string.\nIt is governed by the same rules as Rust [format strings][format-syntax].\nThe arguments that are inserted into the template however look a bit different then you may\nbe familiar with. First we need to specify if the variable is an input or an output of the\ninline assembly. In this case it is an output. We declared this by writing `out`.\nWe also need to specify in what kind of register the assembly expects the variable.\nIn this case we put it in an arbitrary general purpose register by specifying `reg`.\nThe compiler will choose an appropriate register to insert into\nthe template and will read the variable from there after the inline assembly finishes executing.\n\nLet us see another example that also uses an input:\n\n```rust,allow_fail\n# #![feature(asm)]\nlet i: u64 = 3;\nlet o: u64;\nunsafe {\n asm!(\n \"mov {0}, {1}\",\n \"add {0}, {number}\",\n out(reg) o,\n in(reg) i,\n number = const 5,\n );\n}\nassert_eq!(o, 8);\n```\n\nThis will add `5` to the input in variable `i` and write the result to variable `o`.\nThe particular way this assembly does this is first copying the value from `i` to the output,\nand then adding `5` to it.\n\nThe example shows a few things:\n\nFirst, we can see that `asm!` allows multiple template string arguments; each\none is treated as a separate line of assembly code, as if they were all joined\ntogether with newlines between them. This makes it easy to format assembly\ncode.\n\nSecond, we can see that inputs are declared by writing `in` instead of `out`.\n\nThird, one of our operands has a type we haven't seen yet, `const`.\nThis tells the compiler to expand this argument to value directly inside the assembly template.\nThis is only possible for constants and literals.\n\nFourth, we can see that we can specify an argument number, or name as in any format string.\nFor inline assembly templates this is particularly useful as arguments are often used more than once.\nFor more complex inline assembly using this facility is generally recommended, as it improves\nreadability, and allows reordering instructions without changing the argument order.\n\nWe can further refine the above example to avoid the `mov` instruction:\n\n```rust,allow_fail\n# #![feature(asm)]\nlet mut x: u64 = 3;\nunsafe {\n asm!(\"add {0}, {number}\", inout(reg) x, number = const 5);\n}\nassert_eq!(x, 8);\n```\n\nWe can see that `inout` is used to specify an argument that is both input and output.\nThis is different from specifying an input and output separately in that it is guaranteed to assign both to the same register.\n\nIt is also possible to specify different variables for the input and output parts of an `inout` operand:\n\n```rust,allow_fail\n# #![feature(asm)]\nlet x: u64 = 3;\nlet y: u64;\nunsafe {\n asm!(\"add {0}, {number}\", inout(reg) x => y, number = const 5);\n}\nassert_eq!(y, 8);\n```\n\n## Late output operands\n\nThe Rust compiler is conservative with its allocation of operands. It is assumed that an `out`\ncan be written at any time, and can therefore not share its location with any other argument.\nHowever, to guarantee optimal performance it is important to use as few registers as possible,\nso they won't have to be saved and reloaded around the inline assembly block.\nTo achieve this Rust provides a `lateout` specifier. This can be used on any output that is\nwritten only after all inputs have been consumed.\nThere is also a `inlateout` variant of this specifier.\n\nHere is an example where `inlateout` *cannot* be used:\n\n```rust,allow_fail\n# #![feature(asm)]\nlet mut a: u64 = 4;\nlet b: u64 = 4;\nlet c: u64 = 4;\nunsafe {\n asm!(\n \"add {0}, {1}\",\n \"add {0}, {2}\",\n inout(reg) a,\n in(reg) b,\n in(reg) c,\n );\n}\nassert_eq!(a, 12);\n```\n\nHere the compiler is free to allocate the same register for inputs `b` and `c` since it knows they have the same value. However it must allocate a separate register for `a` since it uses `inout` and not `inlateout`. If `inlateout` was used, then `a` and `c` could be allocated to the same register, in which case the first instruction to overwrite the value of `c` and cause the assembly code to produce the wrong result.\n\nHowever the following example can use `inlateout` since the output is only modified after all input registers have been read:\n\n```rust,allow_fail\n# #![feature(asm)]\nlet mut a: u64 = 4;\nlet b: u64 = 4;\nunsafe {\n asm!(\"add {0}, {1}\", inlateout(reg) a, in(reg) b);\n}\nassert_eq!(a, 8);\n```\n\nAs you can see, this assembly fragment will still work correctly if `a` and `b` are assigned to the same register.\n\n## Explicit register operands\n\nSome instructions require that the operands be in a specific register.\nTherefore, Rust inline assembly provides some more specific constraint specifiers.\nWhile `reg` is generally available on any architecture, these are highly architecture specific. E.g. for x86 the general purpose registers `eax`, `ebx`, `ecx`, `edx`, `ebp`, `esi`, and `edi`\namong others can be addressed by their name.\n\n```rust,allow_fail,no_run\n# #![feature(asm)]\nlet cmd = 0xd1;\nunsafe {\n asm!(\"out 0x64, eax\", in(\"eax\") cmd);\n}\n```\n\nIn this example we call the `out` instruction to output the content of the `cmd` variable\nto port `0x64`. Since the `out` instruction only accepts `eax` (and its sub registers) as operand\nwe had to use the `eax` constraint specifier.\n\nNote that unlike other operand types, explicit register operands cannot be used in the template string: you can't use `{}` and should write the register name directly instead. Also, they must appear at the end of the operand list after all other operand types.\n\nConsider this example which uses the x86 `mul` instruction:\n\n```rust,allow_fail\n# #![feature(asm)]\nfn mul(a: u64, b: u64) -> u128 {\n let lo: u64;\n let hi: u64;\n\n unsafe {\n asm!(\n // The x86 mul instruction takes rax as an implicit input and writes\n // the 128-bit result of the multiplication to rax:rdx.\n \"mul {}\",\n in(reg) a,\n inlateout(\"rax\") b => lo,\n lateout(\"rdx\") hi\n );\n }\n\n ((hi as u128) << 64) + lo as u128\n}\n```\n\nThis uses the `mul` instruction to multiply two 64-bit inputs with a 128-bit result.\nThe only explicit operand is a register, that we fill from the variable `a`.\nThe second operand is implicit, and must be the `rax` register, which we fill from the variable `b`.\nThe lower 64 bits of the result are stored in `rax` from which we fill the variable `lo`.\nThe higher 64 bits are stored in `rdx` from which we fill the variable `hi`.\n\n## Clobbered registers\n\nIn many cases inline assembly will modify state that is not needed as an output.\nUsually this is either because we have to use a scratch register in the assembly,\nor instructions modify state that we don't need to further examine.\nThis state is generally referred to as being \"clobbered\".\nWe need to tell the compiler about this since it may need to save and restore this state\naround the inline assembly block.\n\n```rust,allow_fail\n# #![feature(asm)]\nlet ebx: u32;\nlet ecx: u32;\n\nunsafe {\n asm!(\n \"cpuid\",\n // EAX 4 selects the \"Deterministic Cache Parameters\" CPUID leaf\n inout(\"eax\") 4 => _,\n // ECX 0 selects the L0 cache information.\n inout(\"ecx\") 0 => ecx,\n lateout(\"ebx\") ebx,\n lateout(\"edx\") _,\n );\n}\n\nprintln!(\n \"L1 Cache: {}\",\n ((ebx >> 22) + 1) * (((ebx >> 12) & 0x3ff) + 1) * ((ebx & 0xfff) + 1) * (ecx + 1)\n);\n```\n\nIn the example above we use the `cpuid` instruction to get the L1 cache size.\nThis instruction writes to `eax`, `ebx`, `ecx`, and `edx`, but for the cache size we only care about the contents of `ebx` and `ecx`.\n\nHowever we still need to tell the compiler that `eax` and `edx` have been modified so that it can save any values that were in these registers before the asm. This is done by declaring these as outputs but with `_` instead of a variable name, which indicates that the output value is to be discarded.\n\nThis can also be used with a general register class (e.g. `reg`) to obtain a scratch register for use inside the asm code:\n\n```rust,allow_fail\n# #![feature(asm)]\n// Multiply x by 6 using shifts and adds\nlet mut x: u64 = 4;\nunsafe {\n asm!(\n \"mov {tmp}, {x}\",\n \"shl {tmp}, 1\",\n \"shl {x}, 2\",\n \"add {x}, {tmp}\",\n x = inout(reg) x,\n tmp = out(reg) _,\n );\n}\nassert_eq!(x, 4 * 6);\n```\n\n## Symbol operands\n\nA special operand type, `sym`, allows you to use the symbol name of a `fn` or `static` in inline assembly code.\nThis allows you to call a function or access a global variable without needing to keep its address in a register.\n\n```rust,allow_fail\n# #![feature(asm)]\nextern \"C\" fn foo(arg: i32) {\n println!(\"arg = {}\", arg);\n}\n\nfn call_foo(arg: i32) {\n unsafe {\n asm!(\n \"call {}\",\n sym foo,\n // 1st argument in rdi, which is caller-saved\n inout(\"rdi\") arg => _,\n // All caller-saved registers must be marked as clobberred\n out(\"rax\") _, out(\"rcx\") _, out(\"rdx\") _, out(\"rsi\") _,\n out(\"r8\") _, out(\"r9\") _, out(\"r10\") _, out(\"r11\") _,\n out(\"xmm0\") _, out(\"xmm1\") _, out(\"xmm2\") _, out(\"xmm3\") _,\n out(\"xmm4\") _, out(\"xmm5\") _, out(\"xmm6\") _, out(\"xmm7\") _,\n out(\"xmm8\") _, out(\"xmm9\") _, out(\"xmm10\") _, out(\"xmm11\") _,\n out(\"xmm12\") _, out(\"xmm13\") _, out(\"xmm14\") _, out(\"xmm15\") _,\n )\n }\n}\n```\n\nNote that the `fn` or `static` item does not need to be public or `#[no_mangle]`:\nthe compiler will automatically insert the appropriate mangled symbol name into the assembly code.\n\n## Register template modifiers\n\nIn some cases, fine control is needed over the way a register name is formatted when inserted into the template string. This is needed when an architecture's assembly language has several names for the same register, each typically being a \"view\" over a subset of the register (e.g. the low 32 bits of a 64-bit register).\n\nBy default the compiler will always choose the name that refers to the full register size (e.g. `rax` on x86-64, `eax` on x86, etc).\n\nThis default can be overriden by using modifiers on the template string operands, just like you would with format strings:\n\n```rust,allow_fail\n# #![feature(asm)]\nlet mut x: u16 = 0xab;\n\nunsafe {\n asm!(\"mov {0:h}, {0:l}\", inout(reg_abcd) x);\n}\n\nassert_eq!(x, 0xabab);\n```\n\nIn this example, we use the `reg_abcd` register class to restrict the register allocator to the 4 legacy x86 register (`ax`, `bx`, `cx`, `dx`) of which the first two bytes can be addressed independently.\n\nLet us assume that the register allocator has chosen to allocate `x` in the `ax` register.\nThe `h` modifier will emit the register name for the high byte of that register and the `l` modifier will emit the register name for the low byte. The asm code will therefore be expanded as `mov ah, al` which copies the low byte of the value into the high byte.\n\nIf you use a smaller data type (e.g. `u16`) with an operand and forget the use template modifiers, the compiler will emit a warning and suggest the correct modifier to use.\n\n## Memory address operands\n\nSometimes assembly instructions require operands passed via memory addresses/memory locations.\nYou have to manually use the memory address syntax specified by the respectively architectures.\nFor example, in x86/x86_64 and intel assembly syntax, you should wrap inputs/outputs in `[]`\nto indicate they are memory operands:\n\n```rust,allow_fail\n# #![feature(asm, llvm_asm)]\n# fn load_fpu_control_word(control: u16) {\nunsafe {\n asm!(\"fldcw [{}]\", in(reg) &control, options(nostack));\n\n // Previously this would have been written with the deprecated `llvm_asm!` like this\n llvm_asm!(\"fldcw $0\" :: \"m\" (control) :: \"volatile\");\n}\n# }\n```\n\n## Options\n\nBy default, an inline assembly block is treated the same way as an external FFI function call with a custom calling convention: it may read/write memory, have observable side effects, etc. However in many cases, it is desirable to give the compiler more information about what the assembly code is actually doing so that it can optimize better.\n\nLet's take our previous example of an `add` instruction:\n\n```rust,allow_fail\n# #![feature(asm)]\nlet mut a: u64 = 4;\nlet b: u64 = 4;\nunsafe {\n asm!(\n \"add {0}, {1}\",\n inlateout(reg) a, in(reg) b,\n options(pure, nomem, nostack),\n );\n}\nassert_eq!(a, 8);\n```\n\nOptions can be provided as an optional final argument to the `asm!` macro. We specified three options here:\n- `pure` means that the asm code has no observable side effects and that its output depends only on its inputs. This allows the compiler optimizer to call the inline asm fewer times or even eliminate it entirely.\n- `nomem` means that the asm code does not read or write to memory. By default the compiler will assume that inline assembly can read or write any memory address that is accessible to it (e.g. through a pointer passed as an operand, or a global).\n- `nostack` means that the asm code does not push any data onto the stack. This allows the compiler to use optimizations such as the stack red zone on x86-64 to avoid stack pointer adjustments.\n\nThese allow the compiler to better optimize code using `asm!`, for example by eliminating pure `asm!` blocks whose outputs are not needed.\n\nSee the reference for the full list of available options and their effects.\n\n# Reference-level explanation\n[reference-level-explanation]: #reference-level-explanation\n\nInline assembler is implemented as an unsafe macro `asm!()`.\nThe first argument to this macro is a template string literal used to build the final assembly.\nThe following arguments specify input and output operands.\nWhen required, options are specified as the final argument.\n\nThe following ABNF specifies the general syntax:\n\n```ignore\ndir_spec := \"in\" / \"out\" / \"lateout\" / \"inout\" / \"inlateout\"\nreg_spec := <register class> / \"<explicit register>\"\noperand_expr := expr / \"_\" / expr \"=>\" expr / expr \"=>\" \"_\"\nreg_operand := dir_spec \"(\" reg_spec \")\" operand_expr\noperand := reg_operand / \"const\" const_expr / \"sym\" path\noption := \"pure\" / \"nomem\" / \"readonly\" / \"preserves_flags\" / \"noreturn\" / \"nostack\" / \"att_syntax\"\noptions := \"options(\" option *[\",\" option] [\",\"] \")\"\nasm := \"asm!(\" format_string *(\",\" format_string) *(\",\" [ident \"=\"] operand) [\",\" options] [\",\"] \")\"\n```\n\nThe macro will initially be supported only on ARM, AArch64, Hexagon, x86, x86-64 and RISC-V targets. Support for more targets may be added in the future. The compiler will emit an error if `asm!` is used on an unsupported target.\n\n[format-syntax]: https://doc.rust-lang.org/std/fmt/#syntax\n\n## Template string arguments\n\nThe assembler template uses the same syntax as [format strings][format-syntax] (i.e. placeholders are specified by curly braces). The corresponding arguments are accessed in order, by index, or by name. However, implicit named arguments (introduced by [RFC #2795][rfc-2795]) are not supported.\n\nAn `asm!` invocation may have one or more template string arguments; an `asm!` with multiple template string arguments is treated as if all the strings were concatenated with a `\\n` between them. The expected usage is for each template string argument to correspond to a line of assembly code. All template string arguments must appear before any other arguments.\n\nAs with format strings, named arguments must appear after positional arguments. Explicit register operands must appear at the end of the operand list, after named arguments if any.\n\nExplicit register operands cannot be used by placeholders in the template string. All other named and positional operands must appear at least once in the template string, otherwise a compiler error is generated.\n\nThe exact assembly code syntax is target-specific and opaque to the compiler except for the way operands are substituted into the template string to form the code passed to the assembler.\n\nThe 5 targets specified in this RFC (x86, ARM, AArch64, RISC-V, Hexagon) all use the assembly code syntax of the GNU assembler (GAS). On x86, the `.intel_syntax noprefix` mode of GAS is used by default. On ARM, the `.syntax unified` mode is used. These targets impose an additional restriction on the assembly code: any assembler state (e.g. the current section which can be changed with `.section`) must be restored to its original value at the end of the asm string. Assembly code that does not conform to the GAS syntax will result in assembler-specific behavior.\n\n[rfc-2795]: https://github.com/rust-lang/rfcs/pull/2795\n\n## Operand type\n\nSeveral types of operands are supported:\n\n* `in(<reg>) <expr>`\n - `<reg>` can refer to a register class or an explicit register. The allocated register name is substituted into the asm template string.\n - The allocated register will contain the value of `<expr>` at the start of the asm code.\n - The allocated register must contain the same value at the end of the asm code (except if a `lateout` is allocated to the same register).\n* `out(<reg>) <expr>`\n - `<reg>` can refer to a register class or an explicit register. The allocated register name is substituted into the asm template string.\n - The allocated register will contain an undefined value at the start of the asm code.\n - `<expr>` must be a (possibly uninitialized) place expression, to which the contents of the allocated register is written to at the end of the asm code.\n - An underscore (`_`) may be specified instead of an expression, which will cause the contents of the register to be discarded at the end of the asm code (effectively acting as a clobber).\n* `lateout(<reg>) <expr>`\n - Identical to `out` except that the register allocator can reuse a register allocated to an `in`.\n - You should only write to the register after all inputs are read, otherwise you may clobber an input.\n* `inout(<reg>) <expr>`\n - `<reg>` can refer to a register class or an explicit register. The allocated register name is substituted into the asm template string.\n - The allocated register will contain the value of `<expr>` at the start of the asm code.\n - `<expr>` must be a mutable initialized place expression, to which the contents of the allocated register is written to at the end of the asm code.\n* `inout(<reg>) <in expr> => <out expr>`\n - Same as `inout` except that the initial value of the register is taken from the value of `<in expr>`.\n - `<out expr>` must be a (possibly uninitialized) place expression, to which the contents of the allocated register is written to at the end of the asm code.\n - An underscore (`_`) may be specified instead of an expression for `<out expr>`, which will cause the contents of the register to be discarded at the end of the asm code (effectively acting as a clobber).\n - `<in expr>` and `<out expr>` may have different types.\n* `inlateout(<reg>) <expr>` / `inlateout(<reg>) <in expr> => <out expr>`\n - Identical to `inout` except that the register allocator can reuse a register allocated to an `in` (this can happen if the compiler knows the `in` has the same initial value as the `inlateout`).\n - You should only write to the register after all inputs are read, otherwise you may clobber an input.\n* `const <expr>`\n - `<expr>` must be an integer or floating-point constant expression.\n - The value of the expression is formatted as a string and substituted directly into the asm template string.\n* `sym <path>`\n - `<path>` must refer to a `fn` or `static`.\n - A mangled symbol name referring to the item is substituted into the asm template string.\n - The substituted string does not include any modifiers (e.g. GOT, PLT, relocations, etc).\n - `<path>` is allowed to point to a `#[thread_local]` static, in which case the asm code can combine the symbol with relocations (e.g. `@plt`, `@TPOFF`) to read from thread-local data.\n\nOperand expressions are evaluated from left to right, just like function call arguments. After the `asm!` has executed, outputs are written to in left to right order. This is significant if two outputs point to the same place: that place will contain the value of the rightmost output.\n\n## Register operands\n\nInput and output operands can be specified either as an explicit register or as a register class from which the register allocator can select a register. Explicit registers are specified as string literals (e.g. `\"eax\"`) while register classes are specified as identifiers (e.g. `reg`). Using string literals for register names enables support for architectures that use special characters in register names, such as MIPS (`$0`, `$1`, etc).\n\nNote that explicit registers treat register aliases (e.g. `r14` vs `lr` on ARM) and smaller views of a register (e.g. `eax` vs `rax`) as equivalent to the base register. It is a compile-time error to use the same explicit register for two input operands or two output operands. Additionally, it is also a compile-time error to use overlapping registers (e.g. ARM VFP) in input operands or in output operands.\n\nOnly the following types are allowed as operands for inline assembly:\n- Integers (signed and unsigned)\n- Floating-point numbers\n- Pointers (thin only)\n- Function pointers\n- SIMD vectors (structs defined with `#[repr(simd)]` and which implement `Copy`). This includes architecture-specific vector types defined in `std::arch` such as `__m128` (x86) or `int8x16_t` (ARM).\n\nHere is the list of currently supported register classes:\n\n| Architecture | Register class | Registers | LLVM constraint code |\n| ------------ | -------------- | --------- | -------------------- |\n| x86 | `reg` | `ax`, `bx`, `cx`, `dx`, `si`, `di`, `r[8-15]` (x86-64 only) | `r` |\n| x86 | `reg_abcd` | `ax`, `bx`, `cx`, `dx` | `Q` |\n| x86-32 | `reg_byte` | `al`, `bl`, `cl`, `dl`, `ah`, `bh`, `ch`, `dh` | `q` |\n| x86-64 | `reg_byte` | `al`, `bl`, `cl`, `dl`, `sil`, `dil`, `r[8-15]b`, `ah`\\*, `bh`\\*, `ch`\\*, `dh`\\* | `q` |\n| x86 | `xmm_reg` | `xmm[0-7]` (x86) `xmm[0-15]` (x86-64) | `x` |\n| x86 | `ymm_reg` | `ymm[0-7]` (x86) `ymm[0-15]` (x86-64) | `x` |\n| x86 | `zmm_reg` | `zmm[0-7]` (x86) `zmm[0-31]` (x86-64) | `v` |\n| x86 | `kreg` | `k[1-7]` | `Yk` |\n| AArch64 | `reg` | `x[0-28]`, `x30` | `r` |\n| AArch64 | `vreg` | `v[0-31]` | `w` |\n| AArch64 | `vreg_low16` | `v[0-15]` | `x` |\n| ARM | `reg` | `r[0-5]` `r7`\\*, `r[8-10]`, `r11`\\*, `r12`, `r14` | `r` |\n| ARM (Thumb) | `reg_thumb` | `r[0-r7]` | `l` |\n| ARM (ARM) | `reg_thumb` | `r[0-r10]`, `r12`, `r14` | `l` |\n| ARM | `sreg` | `s[0-31]` | `t` |\n| ARM | `sreg_low16` | `s[0-15]` | `x` |\n| ARM | `dreg` | `d[0-31]` | `w` |\n| ARM | `dreg_low16` | `d[0-15]` | `t` |\n| ARM | `dreg_low8` | `d[0-8]` | `x` |\n| ARM | `qreg` | `q[0-15]` | `w` |\n| ARM | `qreg_low8` | `q[0-7]` | `t` |\n| ARM | `qreg_low4` | `q[0-3]` | `x` |\n| MIPS | `reg` | `$[2-25]` | `r` |\n| MIPS | `freg` | `$f[0-31]` | `f` |\n| NVPTX | `reg16` | None\\* | `h` |\n| NVPTX | `reg32` | None\\* | `r` |\n| NVPTX | `reg64` | None\\* | `l` |\n| RISC-V | `reg` | `x1`, `x[5-7]`, `x[9-15]`, `x[16-31]` (non-RV32E) | `r` |\n| RISC-V | `freg` | `f[0-31]` | `f` |\n| Hexagon | `reg` | `r[0-28]` | `r` |\n\n> **Note**: On x86 we treat `reg_byte` differently from `reg` because the compiler can allocate `al` and `ah` separately whereas `reg` reserves the whole register.\n>\n> Note #2: On x86-64 the high byte registers (e.g. `ah`) are only available when used as an explicit register. Specifying the `reg_byte` register class for an operand will always allocate a low byte register.\n>\n> Note #3: NVPTX doesn't have a fixed register set, so named registers are not supported.\n>\n> Note #4: On ARM the frame pointer is either `r7` or `r11` depending on the platform.\n\nAdditional register classes may be added in the future based on demand (e.g. MMX, x87, etc).\n\nEach register class has constraints on which value types they can be used with. This is necessary because the way a value is loaded into a register depends on its type. For example, on big-endian systems, loading a `i32x4` and a `i8x16` into a SIMD register may result in different register contents even if the byte-wise memory representation of both values is identical. The availability of supported types for a particular register class may depend on what target features are currently enabled.\n\n| Architecture | Register class | Target feature | Allowed types |\n| ------------ | -------------- | -------------- | ------------- |\n| x86-32 | `reg` | None | `i16`, `i32`, `f32` |\n| x86-64 | `reg` | None | `i16`, `i32`, `f32`, `i64`, `f64` |\n| x86 | `reg_byte` | None | `i8` |\n| x86 | `xmm_reg` | `sse` | `i32`, `f32`, `i64`, `f64`, <br> `i8x16`, `i16x8`, `i32x4`, `i64x2`, `f32x4`, `f64x2` |\n| x86 | `ymm_reg` | `avx` | `i32`, `f32`, `i64`, `f64`, <br> `i8x16`, `i16x8`, `i32x4`, `i64x2`, `f32x4`, `f64x2` <br> `i8x32`, `i16x16`, `i32x8`, `i64x4`, `f32x8`, `f64x4` |\n| x86 | `zmm_reg` | `avx512f` | `i32`, `f32`, `i64`, `f64`, <br> `i8x16`, `i16x8`, `i32x4`, `i64x2`, `f32x4`, `f64x2` <br> `i8x32`, `i16x16`, `i32x8`, `i64x4`, `f32x8`, `f64x4` <br> `i8x64`, `i16x32`, `i32x16`, `i64x8`, `f32x16`, `f64x8` |\n| x86 | `kreg` | `axv512f` | `i8`, `i16` |\n| x86 | `kreg` | `axv512bw` | `i32`, `i64` |\n| AArch64 | `reg` | None | `i8`, `i16`, `i32`, `f32`, `i64`, `f64` |\n| AArch64 | `vreg` | `fp` | `i8`, `i16`, `i32`, `f32`, `i64`, `f64`, <br> `i8x8`, `i16x4`, `i32x2`, `i64x1`, `f32x2`, `f64x1`, <br> `i8x16`, `i16x8`, `i32x4`, `i64x2`, `f32x4`, `f64x2` |\n| ARM | `reg` | None | `i8`, `i16`, `i32`, `f32` |\n| ARM | `sreg` | `vfp2` | `i32`, `f32` |\n| ARM | `dreg` | `vfp2` | `i64`, `f64`, `i8x8`, `i16x4`, `i32x2`, `i64x1`, `f32x2` |\n| ARM | `qreg` | `neon` | `i8x16`, `i16x8`, `i32x4`, `i64x2`, `f32x4` |\n| MIPS32 | `reg` | None | `i8`, `i16`, `i32`, `f32` |\n| MIPS32 | `freg` | None | `f32`, `f64` |\n| MIPS64 | `reg` | None | `i8`, `i16`, `i32`, `i64`, `f32`, `f64` |\n| MIPS64 | `freg` | None | `f32`, `f64` |\n| NVPTX | `reg16` | None | `i8`, `i16` |\n| NVPTX | `reg32` | None | `i8`, `i16`, `i32`, `f32` |\n| NVPTX | `reg64` | None | `i8`, `i16`, `i32`, `f32`, `i64`, `f64` |\n| RISC-V32 | `reg` | None | `i8`, `i16`, `i32`, `f32` |\n| RISC-V64 | `reg` | None | `i8`, `i16`, `i32`, `f32`, `i64`, `f64` |\n| RISC-V | `freg` | `f` | `f32` |\n| RISC-V | `freg` | `d` | `f64` |\n| Hexagon | `reg` | None | `i8`, `i16`, `i32`, `f32` |\n\n> **Note**: For the purposes of the above table pointers, function pointers and `isize`/`usize` are treated as the equivalent integer type (`i16`/`i32`/`i64` depending on the target).\n\nIf a value is of a smaller size than the register it is allocated in then the upper bits of that register will have an undefined value for inputs and will be ignored for outputs. The only exception is the `freg` register class on RISC-V where `f32` values are NaN-boxed in a `f64` as required by the RISC-V architecture.\n\nWhen separate input and output expressions are specified for an `inout` operand, both expressions must have the same type. The only exception is if both operands are pointers or integers, in which case they are only required to have the same size. This restriction exists because the register allocators in LLVM and GCC sometimes cannot handle tied operands with different types.\n\n## Register names\n\nSome registers have multiple names. These are all treated by the compiler as identical to the base register name. Here is the list of all supported register aliases:\n\n| Architecture | Base register | Aliases |\n| ------------ | ------------- | ------- |\n| x86 | `ax` | `eax`, `rax` |\n| x86 | `bx` | `ebx`, `rbx` |\n| x86 | `cx` | `ecx`, `rcx` |\n| x86 | `dx` | `edx`, `rdx` |\n| x86 | `si` | `esi`, `rsi` |\n| x86 | `di` | `edi`, `rdi` |\n| x86 | `bp` | `bpl`, `ebp`, `rbp` |\n| x86 | `sp` | `spl`, `esp`, `rsp` |\n| x86 | `ip` | `eip`, `rip` |\n| x86 | `st(0)` | `st` |\n| x86 | `r[8-15]` | `r[8-15]b`, `r[8-15]w`, `r[8-15]d` |\n| x86 | `xmm[0-31]` | `ymm[0-31]`, `zmm[0-31]` |\n| AArch64 | `x[0-30]` | `w[0-30]` |\n| AArch64 | `x29` | `fp` |\n| AArch64 | `x30` | `lr` |\n| AArch64 | `sp` | `wsp` |\n| AArch64 | `xzr` | `wzr` |\n| AArch64 | `v[0-31]` | `b[0-31]`, `h[0-31]`, `s[0-31]`, `d[0-31]`, `q[0-31]` |\n| ARM | `r[0-3]` | `a[1-4]` |\n| ARM | `r[4-9]` | `v[1-6]` |\n| ARM | `r9` | `rfp` |\n| ARM | `r10` | `sl` |\n| ARM | `r11` | `fp` |\n| ARM | `r12` | `ip` |\n| ARM | `r13` | `sp` |\n| ARM | `r14` | `lr` |\n| ARM | `r15` | `pc` |\n| RISC-V | `x0` | `zero` |\n| RISC-V | `x1` | `ra` |\n| RISC-V | `x2` | `sp` |\n| RISC-V | `x3` | `gp` |\n| RISC-V | `x4` | `tp` |\n| RISC-V | `x[5-7]` | `t[0-2]` |\n| RISC-V | `x8` | `fp`, `s0` |\n| RISC-V | `x9` | `s1` |\n| RISC-V | `x[10-17]` | `a[0-7]` |\n| RISC-V | `x[18-27]` | `s[2-11]` |\n| RISC-V | `x[28-31]` | `t[3-6]` |\n| RISC-V | `f[0-7]` | `ft[0-7]` |\n| RISC-V | `f[8-9]` | `fs[0-1]` |\n| RISC-V | `f[10-17]` | `fa[0-7]` |\n| RISC-V | `f[18-27]` | `fs[2-11]` |\n| RISC-V | `f[28-31]` | `ft[8-11]` |\n| Hexagon | `r29` | `sp` |\n| Hexagon | `r30` | `fr` |\n| Hexagon | `r31` | `lr` |\n\nSome registers cannot be used for input or output operands:\n\n| Architecture | Unsupported register | Reason |\n| ------------ | -------------------- | ------ |\n| All | `sp` | The stack pointer must be restored to its original value at the end of an asm code block. |\n| All | `bp` (x86), `x29` (AArch64), `x8` (RISC-V), `fr` (Hexagon), `$fp` (MIPS) | The frame pointer cannot be used as an input or output. |\n| ARM | `r7` or `r11` | On ARM the frame pointer can be either `r7` or `r11` depending on the target. The frame pointer cannot be used as an input or output. |\n| ARM | `r6` | `r6` is used internally by LLVM as a base pointer and therefore cannot be used as an input or output. |\n| x86 | `k0` | This is a constant zero register which can't be modified. |\n| x86 | `ip` | This is the program counter, not a real register. |\n| x86 | `mm[0-7]` | MMX registers are not currently supported (but may be in the future). |\n| x86 | `st([0-7])` | x87 registers are not currently supported (but may be in the future). |\n| AArch64 | `xzr` | This is a constant zero register which can't be modified. |\n| ARM | `pc` | This is the program counter, not a real register. |\n| MIPS | `$0` or `$zero` | This is a constant zero register which can't be modified. |\n| MIPS | `$1` or `$at` | Reserved for assembler. |\n| MIPS | `$26`/`$k0`, `$27`/`$k1` | OS-reserved registers. |\n| MIPS | `$28`/`$gp` | Global pointer cannot be used as inputs or outputs. |\n| MIPS | `$ra` | Return address cannot be used as inputs or outputs. |\n| RISC-V | `x0` | This is a constant zero register which can't be modified. |\n| RISC-V | `gp`, `tp` | These registers are reserved and cannot be used as inputs or outputs. |\n| Hexagon | `lr` | This is the link register which cannot be used as an input or output. |\n\nIn some cases LLVM will allocate a \"reserved register\" for `reg` operands even though this register cannot be explicitly specified. Assembly code making use of reserved registers should be careful since `reg` operands may alias with those registers. Reserved registers are:\n- The frame pointer on all architectures.\n- `r6` on ARM.\n\n## Template modifiers\n\nThe placeholders can be augmented by modifiers which are specified after the `:` in the curly braces. These modifiers do not affect register allocation, but change the way operands are formatted when inserted into the template string. Only one modifier is allowed per template placeholder.\n\nThe supported modifiers are a subset of LLVM's (and GCC's) [asm template argument modifiers][llvm-argmod], but do not use the same letter codes.\n\n| Architecture | Register class | Modifier | Example output | LLVM modifier |\n| ------------ | -------------- | -------- | -------------- | ------------- |\n| x86-32 | `reg` | None | `eax` | `k` |\n| x86-64 | `reg` | None | `rax` | `q` |\n| x86-32 | `reg_abcd` | `l` | `al` | `b` |\n| x86-64 | `reg` | `l` | `al` | `b` |\n| x86 | `reg_abcd` | `h` | `ah` | `h` |\n| x86 | `reg` | `x` | `ax` | `w` |\n| x86 | `reg` | `e` | `eax` | `k` |\n| x86-64 | `reg` | `r` | `rax` | `q` |\n| x86 | `reg_byte` | None | `al` / `ah` | None |\n| x86 | `xmm_reg` | None | `xmm0` | `x` |\n| x86 | `ymm_reg` | None | `ymm0` | `t` |\n| x86 | `zmm_reg` | None | `zmm0` | `g` |\n| x86 | `*mm_reg` | `x` | `xmm0` | `x` |\n| x86 | `*mm_reg` | `y` | `ymm0` | `t` |\n| x86 | `*mm_reg` | `z` | `zmm0` | `g` |\n| x86 | `kreg` | None | `k1` | None |\n| AArch64 | `reg` | None | `x0` | `x` |\n| AArch64 | `reg` | `w` | `w0` | `w` |\n| AArch64 | `reg` | `x` | `x0` | `x` |\n| AArch64 | `vreg` | None | `v0` | None |\n| AArch64 | `vreg` | `v` | `v0` | None |\n| AArch64 | `vreg` | `b` | `b0` | `b` |\n| AArch64 | `vreg` | `h` | `h0` | `h` |\n| AArch64 | `vreg` | `s` | `s0` | `s` |\n| AArch64 | `vreg` | `d` | `d0` | `d` |\n| AArch64 | `vreg` | `q` | `q0` | `q` |\n| ARM | `reg` | None | `r0` | None |\n| ARM | `sreg` | None | `s0` | None |\n| ARM | `dreg` | None | `d0` | `P` |\n| ARM | `qreg` | None | `q0` | `q` |\n| ARM | `qreg` | `e` / `f` | `d0` / `d1` | `e` / `f` |\n| MIPS | `reg` | None | `$2` | None |\n| MIPS | `freg` | None | `$f0` | None |\n| NVPTX | `reg16` | None | `rs0` | None |\n| NVPTX | `reg32` | None | `r0` | None |\n| NVPTX | `reg64` | None | `rd0` | None |\n| RISC-V | `reg` | None | `x1` | None |\n| RISC-V | `freg` | None | `f0` | None |\n| Hexagon | `reg` | None | `r0` | None |\n\n> Notes:\n> - on ARM `e` / `f`: this prints the low or high doubleword register name of a NEON quad (128-bit) register.\n> - on x86: our behavior for `reg` with no modifiers differs from what GCC does. GCC will infer the modifier based on the operand value type, while we default to the full register size.\n> - on x86 `xmm_reg`: the `x`, `t` and `g` LLVM modifiers are not yet implemented in LLVM (they are supported by GCC only), but this should be a simple change.\n\nAs stated in the previous section, passing an input value smaller than the register width will result in the upper bits of the register containing undefined values. This is not a problem if the inline asm only accesses the lower bits of the register, which can be done by using a template modifier to use a subregister name in the asm code (e.g. `ax` instead of `rax`). Since this an easy pitfall, the compiler will suggest a template modifier to use where appropriate given the input type. If all references to an operand already have modifiers then the warning is suppressed for that operand.\n\n[llvm-argmod]: http://llvm.org/docs/LangRef.html#asm-template-argument-modifiers\n\n## Options\n\nFlags are used to further influence the behavior of the inline assembly block.\nCurrently the following options are defined:\n- `pure`: The `asm` block has no side effects, and its outputs depend only on its direct inputs (i.e. the values themselves, not what they point to) or values read from memory (unless the `nomem` options is also set). This allows the compiler to execute the `asm` block fewer times than specified in the program (e.g. by hoisting it out of a loop) or even eliminate it entirely if the outputs are not used.\n- `nomem`: The `asm` blocks does not read or write to any memory. This allows the compiler to cache the values of modified global variables in registers across the `asm` block since it knows that they are not read or written to by the `asm`.\n- `readonly`: The `asm` block does not write to any memory. This allows the compiler to cache the values of unmodified global variables in registers across the `asm` block since it knows that they are not written to by the `asm`.\n- `preserves_flags`: The `asm` block does not modify the flags register (defined in the rules below). This allows the compiler to avoid recomputing the condition flags after the `asm` block.\n- `noreturn`: The `asm` block never returns, and its return type is defined as `!` (never). Behavior is undefined if execution falls through past the end of the asm code. A `noreturn` asm block behaves just like a function which doesn't return; notably, local variables in scope are not dropped before it is invoked.\n- `nostack`: The `asm` block does not push data to the stack, or write to the stack red-zone (if supported by the target). If this option is *not* used then the stack pointer is guaranteed to be suitably aligned (according to the target ABI) for a function call.\n- `att_syntax`: This option is only valid on x86, and causes the assembler to use the `.att_syntax prefix` mode of the GNU assembler. Register operands are substituted in with a leading `%`.\n\nThe compiler performs some additional checks on options:\n- The `nomem` and `readonly` options are mutually exclusive: it is a compile-time error to specify both.\n- The `pure` option must be combined with either the `nomem` or `readonly` options, otherwise a compile-time error is emitted.\n- It is a compile-time error to specify `pure` on an asm block with no outputs or only discarded outputs (`_`).\n- It is a compile-time error to specify `noreturn` on an asm block with outputs.\n\n## Rules for inline assembly\n\n- Any registers not specified as inputs will contain an undefined value on entry to the asm block.\n - An \"undefined value\" in the context of inline assembly means that the register can (non-deterministically) have any one of the possible values allowed by the architecture. Notably it is not the same as an LLVM `undef` which can have a different value every time you read it (since such a concept does not exist in assembly code).\n- Any registers not specified as outputs must have the same value upon exiting the asm block as they had on entry, otherwise behavior is undefined.\n - This only applies to registers which can be specified as an input or output. Other registers follow target-specific rules.\n - Note that a `lateout` may be allocated to the same register as an `in`, in which case this rule does not apply. Code should not rely on this however since it depends on the results of register allocation.\n- Behavior is undefined if execution unwinds out of an asm block.\n - This also applies if the assembly code calls a function which then unwinds.\n- The set of memory locations that assembly code is allowed the read and write are the same as those allowed for an FFI function.\n - Refer to the unsafe code guidelines for the exact rules.\n - If the `readonly` option is set, then only memory reads are allowed.\n - If the `nomem` option is set then no reads or writes to memory are allowed.\n - These rules do not apply to memory which is private to the asm code, such as stack space allocated within the asm block.\n- The compiler cannot assume that the instructions in the asm are the ones that will actually end up executed.\n - This effectively means that the compiler must treat the `asm!` as a black box and only take the interface specification into account, not the instructions themselves.\n - Runtime code patching is allowed, via target-specific mechanisms (outside the scope of this RFC).\n- Unless the `nostack` option is set, asm code is allowed to use stack space below the stack pointer.\n - On entry to the asm block the stack pointer is guaranteed to be suitably aligned (according to the target ABI) for a function call.\n - You are responsible for making sure you don't overflow the stack (e.g. use stack probing to ensure you hit a guard page).\n - You should adjust the stack pointer when allocating stack memory as required by the target ABI.\n - The stack pointer must be restored to its original value before leaving the asm block.\n- If the `noreturn` option is set then behavior is undefined if execution falls through to the end of the asm block.\n- If the `pure` option is set then behavior is undefined if the `asm` has side-effects other than its direct outputs. Behavior is also undefined if two executions of the `asm` code with the same inputs result in different outputs.\n - When used with the `nomem` option, \"inputs\" are just the direct inputs of the `asm!`.\n - When used with the `readonly` option, \"inputs\" comprise the direct inputs of the `asm!` and any memory that the `asm!` block is allowed to read.\n- These flags registers must be restored upon exiting the asm block if the `preserves_flags` option is set:\n - x86\n - Status flags in `EFLAGS` (CF, PF, AF, ZF, SF, OF).\n - Floating-point status word (all).\n - Floating-point exception flags in `MXCSR` (PE, UE, OE, ZE, DE, IE).\n - ARM\n - Condition flags in `CPSR` (N, Z, C, V)\n - Saturation flag in `CPSR` (Q)\n - Greater than or equal flags in `CPSR` (GE).\n - Condition flags in `FPSCR` (N, Z, C, V)\n - Saturation flag in `FPSCR` (QC)\n - Floating-point exception flags in `FPSCR` (IDC, IXC, UFC, OFC, DZC, IOC).\n - AArch64\n - Condition flags (`NZCV` register).\n - Floating-point status (`FPSR` register).\n - RISC-V\n - Floating-point exception flags in `fcsr` (`fflags`).\n- On x86, the direction flag (DF in `EFLAGS`) is clear on entry to an asm block and must be clear on exit.\n - Behavior is undefined if the direction flag is set on exiting an asm block.\n- The requirement of restoring the stack pointer and non-output registers to their original value only applies when exiting an `asm!` block.\n - This means that `asm!` blocks that never return (even if not marked `noreturn`) don't need to preserve these registers.\n - When returning to a different `asm!` block than you entered (e.g. for context switching), these registers must contain the value they had upon entering the `asm!` block that you are *exiting*.\n - You cannot exit an `asm!` block that has not been entered. Neither can you exit an `asm!` block that has already been exited.\n - You are responsible for switching any target-specific state (e.g. thread-local storage, stack bounds).\n - The set of memory locations that you may access is the intersection of those allowed by the `asm!` blocks you entered and exited.\n- You cannot assume that an `asm!` block will appear exactly once in the output binary. The compiler is allowed to instantiate multiple copies of the `asm!` block, for example when the function containing it is inlined in multiple places.\n - As a consequence, you should only use [local labels] inside inline assembly code. Defining symbols in assembly code may lead to assembler and/or linker errors due to duplicate symbol definitions.\n\n> **Note**: As a general rule, the flags covered by `preserves_flags` are those which are *not* preserved when performing a function call.\n\n[local labels]: https://sourceware.org/binutils/docs/as/Symbol-Names.html#Local-Labels\n" } , LintCompletion { label : "allocator_api" , description : "# `allocator_api`\n\nThe tracking issue for this feature is [#32838]\n\n[#32838]: https://github.com/rust-lang/rust/issues/32838\n\n------------------------\n\nSometimes you want the memory for one collection to use a different\nallocator than the memory for another collection. In this case,\nreplacing the global allocator is not a workable option. Instead,\nyou need to pass in an instance of an `AllocRef` to each collection\nfor which you want a custom allocator.\n\nTBD\n" } , LintCompletion { label : "set_stdio" , description : "# `set_stdio`\n\nThis feature is internal to the Rust compiler and is not intended for general use.\n\n------------------------\n" } , LintCompletion { label : "libstd_sys_internals" , description : "# `libstd_sys_internals`\n\nThis feature is internal to the Rust compiler and is not intended for general use.\n\n------------------------\n" } , LintCompletion { label : "concat_idents" , description : "# `concat_idents`\n\nThe tracking issue for this feature is: [#29599]\n\n[#29599]: https://github.com/rust-lang/rust/issues/29599\n\n------------------------\n\nThe `concat_idents` feature adds a macro for concatenating multiple identifiers\ninto one identifier.\n\n## Examples\n\n```rust\n#![feature(concat_idents)]\n\nfn main() {\n fn foobar() -> u32 { 23 }\n let f = concat_idents!(foo, bar);\n assert_eq!(f(), 23);\n}\n```" } , LintCompletion { label : "format_args_capture" , description : "# `format_args_capture`\n\nThe tracking issue for this feature is: [#67984]\n\n[#67984]: https://github.com/rust-lang/rust/issues/67984\n\n------------------------\n\nEnables `format_args!` (and macros which use `format_args!` in their implementation, such\nas `format!`, `print!` and `panic!`) to capture variables from the surrounding scope.\nThis avoids the need to pass named parameters when the binding in question\nalready exists in scope.\n\n```rust\n#![feature(format_args_capture)]\n\nlet (person, species, name) = (\"Charlie Brown\", \"dog\", \"Snoopy\");\n\n// captures named argument `person`\nprint!(\"Hello {person}\");\n\n// captures named arguments `species` and `name`\nformat!(\"The {species}'s name is {name}.\");\n```\n\nThis also works for formatting parameters such as width and precision:\n\n```rust\n#![feature(format_args_capture)]\n\nlet precision = 2;\nlet s = format!(\"{:.precision$}\", 1.324223);\n\nassert_eq!(&s, \"1.32\");\n```\n\nA non-exhaustive list of macros which benefit from this functionality include:\n- `format!`\n- `print!` and `println!`\n- `eprint!` and `eprintln!`\n- `write!` and `writeln!`\n- `panic!`\n- `unreachable!`\n- `unimplemented!`\n- `todo!`\n- `assert!` and similar\n- macros in many thirdparty crates, such as `log`\n" } , LintCompletion { label : "is_sorted" , description : "# `is_sorted`\n\nThe tracking issue for this feature is: [#53485]\n\n[#53485]: https://github.com/rust-lang/rust/issues/53485\n\n------------------------\n\nAdd the methods `is_sorted`, `is_sorted_by` and `is_sorted_by_key` to `[T]`;\nadd the methods `is_sorted`, `is_sorted_by` and `is_sorted_by_key` to\n`Iterator`.\n" } , LintCompletion { label : "sort_internals" , description : "# `sort_internals`\n\nThis feature is internal to the Rust compiler and is not intended for general use.\n\n------------------------\n" } , LintCompletion { label : "c_variadic" , description : "# `c_variadic`\n\nThe tracking issue for this feature is: [#44930]\n\n[#44930]: https://github.com/rust-lang/rust/issues/44930\n\n------------------------\n\nThe `c_variadic` library feature exposes the `VaList` structure,\nRust's analogue of C's `va_list` type.\n\n## Examples\n\n```rust\n#![feature(c_variadic)]\n\nuse std::ffi::VaList;\n\npub unsafe extern \"C\" fn vadd(n: usize, mut args: VaList) -> usize {\n let mut sum = 0;\n for _ in 0..n {\n sum += args.arg::<usize>();\n }\n sum\n}\n```\n" } , LintCompletion { label : "trace_macros" , description : "# `trace_macros`\n\nThe tracking issue for this feature is [#29598].\n\n[#29598]: https://github.com/rust-lang/rust/issues/29598\n\n------------------------\n\nWith `trace_macros` you can trace the expansion of macros in your code.\n\n## Examples\n\n```rust\n#![feature(trace_macros)]\n\nfn main() {\n trace_macros!(true);\n println!(\"Hello, Rust!\");\n trace_macros!(false);\n}\n```\n\nThe `cargo build` output:\n\n```txt\nnote: trace_macro\n --> src/main.rs:5:5\n |\n5 | println!(\"Hello, Rust!\");\n | ^^^^^^^^^^^^^^^^^^^^^^^^^\n |\n = note: expanding `println! { \"Hello, Rust!\" }`\n = note: to `print ! ( concat ! ( \"Hello, Rust!\" , \"\\n\" ) )`\n = note: expanding `print! { concat ! ( \"Hello, Rust!\" , \"\\n\" ) }`\n = note: to `$crate :: io :: _print ( format_args ! ( concat ! ( \"Hello, Rust!\" , \"\\n\" ) )\n )`\n\n Finished dev [unoptimized + debuginfo] target(s) in 0.60 secs\n```\n" } , LintCompletion { label : "c_void_variant" , description : "# `c_void_variant`\n\nThis feature is internal to the Rust compiler and is not intended for general use.\n\n------------------------\n" } , LintCompletion { label : "core_panic" , description : "# `core_panic`\n\nThis feature is internal to the Rust compiler and is not intended for general use.\n\n------------------------\n" } , LintCompletion { label : "fmt_internals" , description : "# `fmt_internals`\n\nThis feature is internal to the Rust compiler and is not intended for general use.\n\n------------------------\n" } , LintCompletion { label : "rt" , description : "# `rt`\n\nThis feature is internal to the Rust compiler and is not intended for general use.\n\n------------------------\n" } , LintCompletion { label : "thread_local_internals" , description : "# `thread_local_internals`\n\nThis feature is internal to the Rust compiler and is not intended for general use.\n\n------------------------\n" } , LintCompletion { label : "core_intrinsics" , description : "# `core_intrinsics`\n\nThis feature is internal to the Rust compiler and is not intended for general use.\n\n------------------------\n" } , LintCompletion { label : "update_panic_count" , description : "# `update_panic_count`\n\nThis feature is internal to the Rust compiler and is not intended for general use.\n\n------------------------\n" } , LintCompletion { label : "profiler_runtime_lib" , description : "# `profiler_runtime_lib`\n\nThis feature is internal to the Rust compiler and is not intended for general use.\n\n------------------------\n" } , LintCompletion { label : "core_private_bignum" , description : "# `core_private_bignum`\n\nThis feature is internal to the Rust compiler and is not intended for general use.\n\n------------------------\n" } , LintCompletion { label : "core_private_diy_float" , description : "# `core_private_diy_float`\n\nThis feature is internal to the Rust compiler and is not intended for general use.\n\n------------------------\n" } , LintCompletion { label : "str_internals" , description : "# `str_internals`\n\nThis feature is internal to the Rust compiler and is not intended for general use.\n\n------------------------\n" } , LintCompletion { label : "derive_clone_copy" , description : "# `derive_clone_copy`\n\nThis feature is internal to the Rust compiler and is not intended for general use.\n\n------------------------\n" } , LintCompletion { label : "fn_traits" , description : "# `fn_traits`\n\nThe tracking issue for this feature is [#29625]\n\nSee Also: [`unboxed_closures`](../language-features/unboxed-closures.md)\n\n[#29625]: https://github.com/rust-lang/rust/issues/29625\n\n----\n\nThe `fn_traits` feature allows for implementation of the [`Fn*`] traits\nfor creating custom closure-like types.\n\n[`Fn*`]: https://doc.rust-lang.org/std/ops/trait.Fn.html\n\n```rust\n#![feature(unboxed_closures)]\n#![feature(fn_traits)]\n\nstruct Adder {\n a: u32\n}\n\nimpl FnOnce<(u32, )> for Adder {\n type Output = u32;\n extern \"rust-call\" fn call_once(self, b: (u32, )) -> Self::Output {\n self.a + b.0\n }\n}\n\nfn main() {\n let adder = Adder { a: 3 };\n assert_eq!(adder(2), 5);\n}\n```\n" } , LintCompletion { label : "fd_read" , description : "# `fd_read`\n\nThis feature is internal to the Rust compiler and is not intended for general use.\n\n------------------------\n" } , LintCompletion { label : "windows_net" , description : "# `windows_net`\n\nThis feature is internal to the Rust compiler and is not intended for general use.\n\n------------------------\n" } , LintCompletion { label : "derive_eq" , description : "# `derive_eq`\n\nThis feature is internal to the Rust compiler and is not intended for general use.\n\n------------------------\n" } , LintCompletion { label : "windows_stdio" , description : "# `windows_stdio`\n\nThis feature is internal to the Rust compiler and is not intended for general use.\n\n------------------------\n" } , LintCompletion { label : "test" , description : "# `test`\n\nThe tracking issue for this feature is: None.\n\n------------------------\n\nThe internals of the `test` crate are unstable, behind the `test` flag. The\nmost widely used part of the `test` crate are benchmark tests, which can test\nthe performance of your code. Let's make our `src/lib.rs` look like this\n(comments elided):\n\n```rust,ignore\n#![feature(test)]\n\nextern crate test;\n\npub fn add_two(a: i32) -> i32 {\n a + 2\n}\n\n#[cfg(test)]\nmod tests {\n use super::*;\n use test::Bencher;\n\n #[test]\n fn it_works() {\n assert_eq!(4, add_two(2));\n }\n\n #[bench]\n fn bench_add_two(b: &mut Bencher) {\n b.iter(|| add_two(2));\n }\n}\n```\n\nNote the `test` feature gate, which enables this unstable feature.\n\nWe've imported the `test` crate, which contains our benchmarking support.\nWe have a new function as well, with the `bench` attribute. Unlike regular\ntests, which take no arguments, benchmark tests take a `&mut Bencher`. This\n`Bencher` provides an `iter` method, which takes a closure. This closure\ncontains the code we'd like to benchmark.\n\nWe can run benchmark tests with `cargo bench`:\n\n```bash\n$ cargo bench\n Compiling adder v0.0.1 (file:///home/steve/tmp/adder)\n Running target/release/adder-91b3e234d4ed382a\n\nrunning 2 tests\ntest tests::it_works ... ignored\ntest tests::bench_add_two ... bench: 1 ns/iter (+/- 0)\n\ntest result: ok. 0 passed; 0 failed; 1 ignored; 1 measured\n```\n\nOur non-benchmark test was ignored. You may have noticed that `cargo bench`\ntakes a bit longer than `cargo test`. This is because Rust runs our benchmark\na number of times, and then takes the average. Because we're doing so little\nwork in this example, we have a `1 ns/iter (+/- 0)`, but this would show\nthe variance if there was one.\n\nAdvice on writing benchmarks:\n\n\n* Move setup code outside the `iter` loop; only put the part you want to measure inside\n* Make the code do \"the same thing\" on each iteration; do not accumulate or change state\n* Make the outer function idempotent too; the benchmark runner is likely to run\n it many times\n* Make the inner `iter` loop short and fast so benchmark runs are fast and the\n calibrator can adjust the run-length at fine resolution\n* Make the code in the `iter` loop do something simple, to assist in pinpointing\n performance improvements (or regressions)\n\n## Gotcha: optimizations\n\nThere's another tricky part to writing benchmarks: benchmarks compiled with\noptimizations activated can be dramatically changed by the optimizer so that\nthe benchmark is no longer benchmarking what one expects. For example, the\ncompiler might recognize that some calculation has no external effects and\nremove it entirely.\n\n```rust,ignore\n#![feature(test)]\n\nextern crate test;\nuse test::Bencher;\n\n#[bench]\nfn bench_xor_1000_ints(b: &mut Bencher) {\n b.iter(|| {\n (0..1000).fold(0, |old, new| old ^ new);\n });\n}\n```\n\ngives the following results\n\n```text\nrunning 1 test\ntest bench_xor_1000_ints ... bench: 0 ns/iter (+/- 0)\n\ntest result: ok. 0 passed; 0 failed; 0 ignored; 1 measured\n```\n\nThe benchmarking runner offers two ways to avoid this. Either, the closure that\nthe `iter` method receives can return an arbitrary value which forces the\noptimizer to consider the result used and ensures it cannot remove the\ncomputation entirely. This could be done for the example above by adjusting the\n`b.iter` call to\n\n```rust\n# struct X;\n# impl X { fn iter<T, F>(&self, _: F) where F: FnMut() -> T {} } let b = X;\nb.iter(|| {\n // Note lack of `;` (could also use an explicit `return`).\n (0..1000).fold(0, |old, new| old ^ new)\n});\n```\n\nOr, the other option is to call the generic `test::black_box` function, which\nis an opaque \"black box\" to the optimizer and so forces it to consider any\nargument as used.\n\n```rust\n#![feature(test)]\n\nextern crate test;\n\n# fn main() {\n# struct X;\n# impl X { fn iter<T, F>(&self, _: F) where F: FnMut() -> T {} } let b = X;\nb.iter(|| {\n let n = test::black_box(1000);\n\n (0..n).fold(0, |a, b| a ^ b)\n})\n# }\n```\n\nNeither of these read or modify the value, and are very cheap for small values.\nLarger values can be passed indirectly to reduce overhead (e.g.\n`black_box(&huge_struct)`).\n\nPerforming either of the above changes gives the following benchmarking results\n\n```text\nrunning 1 test\ntest bench_xor_1000_ints ... bench: 131 ns/iter (+/- 3)\n\ntest result: ok. 0 passed; 0 failed; 0 ignored; 1 measured\n```\n\nHowever, the optimizer can still modify a testcase in an undesirable manner\neven when using either of the above.\n" } , LintCompletion { label : "flt2dec" , description : "# `flt2dec`\n\nThis feature is internal to the Rust compiler and is not intended for general use.\n\n------------------------\n" } , LintCompletion { label : "libstd_io_internals" , description : "# `libstd_io_internals`\n\nThis feature is internal to the Rust compiler and is not intended for general use.\n\n------------------------\n" } , LintCompletion { label : "fd" , description : "# `fd`\n\nThis feature is internal to the Rust compiler and is not intended for general use.\n\n------------------------\n" } , LintCompletion { label : "print_internals" , description : "# `print_internals`\n\nThis feature is internal to the Rust compiler and is not intended for general use.\n\n------------------------\n" } , LintCompletion { label : "range_bounds_assert_len" , description : "# `range_bounds_assert_len`\n\nThe tracking issue for this feature is: [#76393]\n\n------------------------\n\nThis adds [`RangeBounds::assert_len`].\n\n[#76393]: https://github.com/rust-lang/rust/issues/76393\n[`RangeBounds::assert_len`]: https://doc.rust-lang.org/nightly/std/ops/trait.RangeBounds.html#method.assert_len\n" } , LintCompletion { label : "try_trait" , description : "# `try_trait`\n\nThe tracking issue for this feature is: [#42327]\n\n[#42327]: https://github.com/rust-lang/rust/issues/42327\n\n------------------------\n\nThis introduces a new trait `Try` for extending the `?` operator to types\nother than `Result` (a part of [RFC 1859]). The trait provides the canonical\nway to _view_ a type in terms of a success/failure dichotomy. This will\nallow `?` to supplant the `try_opt!` macro on `Option` and the `try_ready!`\nmacro on `Poll`, among other things.\n\n[RFC 1859]: https://github.com/rust-lang/rfcs/pull/1859\n\nHere's an example implementation of the trait:\n\n```rust,ignore\n/// A distinct type to represent the `None` value of an `Option`.\n///\n/// This enables using the `?` operator on `Option`; it's rarely useful alone.\n#[derive(Debug)]\n#[unstable(feature = \"try_trait\", issue = \"42327\")]\npub struct None { _priv: () }\n\n#[unstable(feature = \"try_trait\", issue = \"42327\")]\nimpl<T> ops::Try for Option<T> {\n type Ok = T;\n type Error = None;\n\n fn into_result(self) -> Result<T, None> {\n self.ok_or(None { _priv: () })\n }\n\n fn from_ok(v: T) -> Self {\n Some(v)\n }\n\n fn from_error(_: None) -> Self {\n None\n }\n}\n```\n\nNote the `Error` associated type here is a new marker. The `?` operator\nallows interconversion between different `Try` implementers only when\nthe error type can be converted `Into` the error type of the enclosing\nfunction (or catch block). Having a distinct error type (as opposed to\njust `()`, or similar) restricts this to where it's semantically meaningful.\n" }] ; | 4 | pub(super) const FEATURES: &[LintCompletion] = &[ |
5 | pub (super) const CLIPPY_LINTS : & [LintCompletion] = & [LintCompletion { label : "clippy::absurd_extreme_comparisons" , description : "Checks for comparisons where one side of the relation is\\neither the minimum or maximum value for its type and warns if it involves a\\ncase that is always true or always false. Only integer and boolean types are\\nchecked." } , LintCompletion { label : "clippy::almost_swapped" , description : "Checks for `foo = bar; bar = foo` sequences." } , LintCompletion { label : "clippy::approx_constant" , description : "Checks for floating point literals that approximate\\nconstants which are defined in\\n[`std::f32::consts`](https://doc.rust-lang.org/stable/std/f32/consts/#constants)\\nor\\n[`std::f64::consts`](https://doc.rust-lang.org/stable/std/f64/consts/#constants),\\nrespectively, suggesting to use the predefined constant." } , LintCompletion { label : "clippy::as_conversions" , description : "Checks for usage of `as` conversions." } , LintCompletion { label : "clippy::assertions_on_constants" , description : "Checks for `assert!(true)` and `assert!(false)` calls." } , LintCompletion { label : "clippy::assign_op_pattern" , description : "Checks for `a = a op b` or `a = b commutative_op a`\\npatterns." } , LintCompletion { label : "clippy::assign_ops" , description : "Nothing. This lint has been deprecated." } , LintCompletion { label : "clippy::async_yields_async" , description : "Checks for async blocks that yield values of types\\nthat can themselves be awaited." } , LintCompletion { label : "clippy::await_holding_lock" , description : "Checks for calls to await while holding a\\nnon-async-aware MutexGuard." } , LintCompletion { label : "clippy::bad_bit_mask" , description : "Checks for incompatible bit masks in comparisons.\\n\\nThe formula for detecting if an expression of the type `_ <bit_op> m\\n<cmp_op> c` (where `<bit_op>` is one of {`&`, `|`} and `<cmp_op>` is one of\\n{`!=`, `>=`, `>`, `!=`, `>=`, `>`}) can be determined from the following\\ntable:\\n\\n|Comparison |Bit Op|Example |is always|Formula |\\n|------------|------|------------|---------|----------------------|\\n|`==` or `!=`| `&` |`x & 2 == 3`|`false` |`c & m != c` |\\n|`<` or `>=`| `&` |`x & 2 < 3` |`true` |`m < c` |\\n|`>` or `<=`| `&` |`x & 1 > 1` |`false` |`m <= c` |\\n|`==` or `!=`| `|` |`x | 1 == 0`|`false` |`c | m != c` |\\n|`<` or `>=`| `|` |`x | 1 < 1` |`false` |`m >= c` |\\n|`<=` or `>` | `|` |`x | 1 > 0` |`true` |`m > c` |" } , LintCompletion { label : "clippy::bind_instead_of_map" , description : "Checks for usage of `_.and_then(|x| Some(y))`, `_.and_then(|x| Ok(y))` or\\n`_.or_else(|x| Err(y))`." } , LintCompletion { label : "clippy::blacklisted_name" , description : "Checks for usage of blacklisted names for variables, such\\nas `foo`." } , LintCompletion { label : "clippy::blanket_clippy_restriction_lints" , description : "Checks for `warn`/`deny`/`forbid` attributes targeting the whole clippy::restriction category." } , LintCompletion { label : "clippy::blocks_in_if_conditions" , description : "Checks for `if` conditions that use blocks containing an\\nexpression, statements or conditions that use closures with blocks." } , LintCompletion { label : "clippy::bool_comparison" , description : "Checks for expressions of the form `x == true`,\\n`x != true` and order comparisons such as `x < true` (or vice versa) and\\nsuggest using the variable directly." } , LintCompletion { label : "clippy::borrow_interior_mutable_const" , description : "Checks if `const` items which is interior mutable (e.g.,\\ncontains a `Cell`, `Mutex`, `AtomicXxxx`, etc.) has been borrowed directly." } , LintCompletion { label : "clippy::borrowed_box" , description : "Checks for use of `&Box<T>` anywhere in the code.\\nCheck the [Box documentation](https://doc.rust-lang.org/std/boxed/index.html) for more information." } , LintCompletion { label : "clippy::box_vec" , description : "Checks for use of `Box<Vec<_>>` anywhere in the code.\\nCheck the [Box documentation](https://doc.rust-lang.org/std/boxed/index.html) for more information." } , LintCompletion { label : "clippy::boxed_local" , description : "Checks for usage of `Box<T>` where an unboxed `T` would\\nwork fine." } , LintCompletion { label : "clippy::builtin_type_shadow" , description : "Warns if a generic shadows a built-in type." } , LintCompletion { label : "clippy::cargo_common_metadata" , description : "Checks to see if all common metadata is defined in\\n`Cargo.toml`. See: https://rust-lang-nursery.github.io/api-guidelines/documentation.html#cargotoml-includes-all-common-metadata-c-metadata" } , LintCompletion { label : "clippy::cast_lossless" , description : "Checks for casts between numerical types that may\\nbe replaced by safe conversion functions." } , LintCompletion { label : "clippy::cast_possible_truncation" , description : "Checks for casts between numerical types that may\\ntruncate large values. This is expected behavior, so the cast is `Allow` by\\ndefault." } , LintCompletion { label : "clippy::cast_possible_wrap" , description : "Checks for casts from an unsigned type to a signed type of\\nthe same size. Performing such a cast is a 'no-op' for the compiler,\\ni.e., nothing is changed at the bit level, and the binary representation of\\nthe value is reinterpreted. This can cause wrapping if the value is too big\\nfor the target signed type. However, the cast works as defined, so this lint\\nis `Allow` by default." } , LintCompletion { label : "clippy::cast_precision_loss" , description : "Checks for casts from any numerical to a float type where\\nthe receiving type cannot store all values from the original type without\\nrounding errors. This possible rounding is to be expected, so this lint is\\n`Allow` by default.\\n\\nBasically, this warns on casting any integer with 32 or more bits to `f32`\\nor any 64-bit integer to `f64`." } , LintCompletion { label : "clippy::cast_ptr_alignment" , description : "Checks for casts from a less-strictly-aligned pointer to a\\nmore-strictly-aligned pointer" } , LintCompletion { label : "clippy::cast_ref_to_mut" , description : "Checks for casts of `&T` to `&mut T` anywhere in the code." } , LintCompletion { label : "clippy::cast_sign_loss" , description : "Checks for casts from a signed to an unsigned numerical\\ntype. In this case, negative values wrap around to large positive values,\\nwhich can be quite surprising in practice. However, as the cast works as\\ndefined, this lint is `Allow` by default." } , LintCompletion { label : "clippy::char_lit_as_u8" , description : "Checks for expressions where a character literal is cast\\nto `u8` and suggests using a byte literal instead." } , LintCompletion { label : "clippy::chars_last_cmp" , description : "Checks for usage of `_.chars().last()` or\\n`_.chars().next_back()` on a `str` to check if it ends with a given char." } , LintCompletion { label : "clippy::chars_next_cmp" , description : "Checks for usage of `.chars().next()` on a `str` to check\\nif it starts with a given char." } , LintCompletion { label : "clippy::checked_conversions" , description : "Checks for explicit bounds checking when casting." } , LintCompletion { label : "clippy::clone_double_ref" , description : "Checks for usage of `.clone()` on an `&&T`." } , LintCompletion { label : "clippy::clone_on_copy" , description : "Checks for usage of `.clone()` on a `Copy` type." } , LintCompletion { label : "clippy::clone_on_ref_ptr" , description : "Checks for usage of `.clone()` on a ref-counted pointer,\\n(`Rc`, `Arc`, `rc::Weak`, or `sync::Weak`), and suggests calling Clone via unified\\nfunction syntax instead (e.g., `Rc::clone(foo)`)." } , LintCompletion { label : "clippy::cmp_nan" , description : "Checks for comparisons to NaN." } , LintCompletion { label : "clippy::cmp_null" , description : "This lint checks for equality comparisons with `ptr::null`" } , LintCompletion { label : "clippy::cmp_owned" , description : "Checks for conversions to owned values just for the sake\\nof a comparison." } , LintCompletion { label : "clippy::cognitive_complexity" , description : "Checks for methods with high cognitive complexity." } , LintCompletion { label : "clippy::collapsible_if" , description : "Checks for nested `if` statements which can be collapsed\\nby `&&`-combining their conditions and for `else { if ... }` expressions\\nthat\\ncan be collapsed to `else if ...`." } , LintCompletion { label : "clippy::comparison_chain" , description : "Checks comparison chains written with `if` that can be\\nrewritten with `match` and `cmp`." } , LintCompletion { label : "clippy::copy_iterator" , description : "Checks for types that implement `Copy` as well as\\n`Iterator`." } , LintCompletion { label : "clippy::create_dir" , description : "Checks usage of `std::fs::create_dir` and suggest using `std::fs::create_dir_all` instead." } , LintCompletion { label : "clippy::crosspointer_transmute" , description : "Checks for transmutes between a type `T` and `*T`." } , LintCompletion { label : "clippy::dbg_macro" , description : "Checks for usage of dbg!() macro." } , LintCompletion { label : "clippy::debug_assert_with_mut_call" , description : "Checks for function/method calls with a mutable\\nparameter in `debug_assert!`, `debug_assert_eq!` and `debug_assert_ne!` macros." } , LintCompletion { label : "clippy::decimal_literal_representation" , description : "Warns if there is a better representation for a numeric literal." } , LintCompletion { label : "clippy::declare_interior_mutable_const" , description : "Checks for declaration of `const` items which is interior\\nmutable (e.g., contains a `Cell`, `Mutex`, `AtomicXxxx`, etc.)." } , LintCompletion { label : "clippy::default_trait_access" , description : "Checks for literal calls to `Default::default()`." } , LintCompletion { label : "clippy::deprecated_cfg_attr" , description : "Checks for `#[cfg_attr(rustfmt, rustfmt_skip)]` and suggests to replace it\\nwith `#[rustfmt::skip]`." } , LintCompletion { label : "clippy::deprecated_semver" , description : "Checks for `#[deprecated]` annotations with a `since`\\nfield that is not a valid semantic version." } , LintCompletion { label : "clippy::deref_addrof" , description : "Checks for usage of `*&` and `*&mut` in expressions." } , LintCompletion { label : "clippy::derive_hash_xor_eq" , description : "Checks for deriving `Hash` but implementing `PartialEq`\\nexplicitly or vice versa." } , LintCompletion { label : "clippy::derive_ord_xor_partial_ord" , description : "Checks for deriving `Ord` but implementing `PartialOrd`\\nexplicitly or vice versa." } , LintCompletion { label : "clippy::disallowed_method" , description : "Lints for specific trait methods defined in clippy.toml" } , LintCompletion { label : "clippy::diverging_sub_expression" , description : "Checks for diverging calls that are not match arms or\\nstatements." } , LintCompletion { label : "clippy::doc_markdown" , description : "Checks for the presence of `_`, `::` or camel-case words\\noutside ticks in documentation." } , LintCompletion { label : "clippy::double_comparisons" , description : "Checks for double comparisons that could be simplified to a single expression." } , LintCompletion { label : "clippy::double_must_use" , description : "Checks for a [`#[must_use]`] attribute without\\nfurther information on functions and methods that return a type already\\nmarked as `#[must_use]`.\\n\\n[`#[must_use]`]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute" } , LintCompletion { label : "clippy::double_neg" , description : "Detects expressions of the form `--x`." } , LintCompletion { label : "clippy::double_parens" , description : "Checks for unnecessary double parentheses." } , LintCompletion { label : "clippy::drop_bounds" , description : "Nothing. This lint has been deprecated." } , LintCompletion { label : "clippy::drop_copy" , description : "Checks for calls to `std::mem::drop` with a value\\nthat derives the Copy trait" } , LintCompletion { label : "clippy::drop_ref" , description : "Checks for calls to `std::mem::drop` with a reference\\ninstead of an owned value." } , LintCompletion { label : "clippy::duplicate_underscore_argument" , description : "Checks for function arguments having the similar names\\ndiffering by an underscore." } , LintCompletion { label : "clippy::duration_subsec" , description : "Checks for calculation of subsecond microseconds or milliseconds\\nfrom other `Duration` methods." } , LintCompletion { label : "clippy::else_if_without_else" , description : "Checks for usage of if expressions with an `else if` branch,\\nbut without a final `else` branch." } , LintCompletion { label : "clippy::empty_enum" , description : "Checks for `enum`s with no variants." } , LintCompletion { label : "clippy::empty_line_after_outer_attr" , description : "Checks for empty lines after outer attributes" } , LintCompletion { label : "clippy::empty_loop" , description : "Checks for empty `loop` expressions." } , LintCompletion { label : "clippy::enum_clike_unportable_variant" , description : "Checks for C-like enumerations that are\\n`repr(isize/usize)` and have values that don't fit into an `i32`." } , LintCompletion { label : "clippy::enum_glob_use" , description : "Checks for `use Enum::*`." } , LintCompletion { label : "clippy::enum_variant_names" , description : "Detects enumeration variants that are prefixed or suffixed\\nby the same characters." } , LintCompletion { label : "clippy::eq_op" , description : "Checks for equal operands to comparison, logical and\\nbitwise, difference and division binary operators (`==`, `>`, etc., `&&`,\\n`||`, `&`, `|`, `^`, `-` and `/`)." } , LintCompletion { label : "clippy::erasing_op" , description : "Checks for erasing operations, e.g., `x * 0`." } , LintCompletion { label : "clippy::eval_order_dependence" , description : "Checks for a read and a write to the same variable where\\nwhether the read occurs before or after the write depends on the evaluation\\norder of sub-expressions." } , LintCompletion { label : "clippy::excessive_precision" , description : "Checks for float literals with a precision greater\\nthan that supported by the underlying type." } , LintCompletion { label : "clippy::exit" , description : "`exit()` terminates the program and doesn't provide a\\nstack trace." } , LintCompletion { label : "clippy::expect_fun_call" , description : "Checks for calls to `.expect(&format!(...))`, `.expect(foo(..))`,\\netc., and suggests to use `unwrap_or_else` instead" } , LintCompletion { label : "clippy::expect_used" , description : "Checks for `.expect()` calls on `Option`s and `Result`s." } , LintCompletion { label : "clippy::expl_impl_clone_on_copy" , description : "Checks for explicit `Clone` implementations for `Copy`\\ntypes." } , LintCompletion { label : "clippy::explicit_counter_loop" , description : "Checks `for` loops over slices with an explicit counter\\nand suggests the use of `.enumerate()`." } , LintCompletion { label : "clippy::explicit_deref_methods" , description : "Checks for explicit `deref()` or `deref_mut()` method calls." } , LintCompletion { label : "clippy::explicit_into_iter_loop" , description : "Checks for loops on `y.into_iter()` where `y` will do, and\\nsuggests the latter." } , LintCompletion { label : "clippy::explicit_iter_loop" , description : "Checks for loops on `x.iter()` where `&x` will do, and\\nsuggests the latter." } , LintCompletion { label : "clippy::explicit_write" , description : "Checks for usage of `write!()` / `writeln()!` which can be\\nreplaced with `(e)print!()` / `(e)println!()`" } , LintCompletion { label : "clippy::extend_from_slice" , description : "Nothing. This lint has been deprecated." } , LintCompletion { label : "clippy::extra_unused_lifetimes" , description : "Checks for lifetimes in generics that are never used\\nanywhere else." } , LintCompletion { label : "clippy::fallible_impl_from" , description : "Checks for impls of `From<..>` that contain `panic!()` or `unwrap()`" } , LintCompletion { label : "clippy::filetype_is_file" , description : "Checks for `FileType::is_file()`." } , LintCompletion { label : "clippy::filter_map" , description : "Checks for usage of `_.filter(_).map(_)`,\\n`_.filter(_).flat_map(_)`, `_.filter_map(_).flat_map(_)` and similar." } , LintCompletion { label : "clippy::filter_map_next" , description : "Checks for usage of `_.filter_map(_).next()`." } , LintCompletion { label : "clippy::filter_next" , description : "Checks for usage of `_.filter(_).next()`." } , LintCompletion { label : "clippy::find_map" , description : "Checks for usage of `_.find(_).map(_)`." } , LintCompletion { label : "clippy::flat_map_identity" , description : "Checks for usage of `flat_map(|x| x)`." } , LintCompletion { label : "clippy::float_arithmetic" , description : "Checks for float arithmetic." } , LintCompletion { label : "clippy::float_cmp" , description : "Checks for (in-)equality comparisons on floating-point\\nvalues (apart from zero), except in functions called `*eq*` (which probably\\nimplement equality for a type involving floats)." } , LintCompletion { label : "clippy::float_cmp_const" , description : "Checks for (in-)equality comparisons on floating-point\\nvalue and constant, except in functions called `*eq*` (which probably\\nimplement equality for a type involving floats)." } , LintCompletion { label : "clippy::float_equality_without_abs" , description : "Checks for statements of the form `(a - b) < f32::EPSILON` or\\n`(a - b) < f64::EPSILON`. Notes the missing `.abs()`." } , LintCompletion { label : "clippy::fn_address_comparisons" , description : "Checks for comparisons with an address of a function item." } , LintCompletion { label : "clippy::fn_params_excessive_bools" , description : "Checks for excessive use of\\nbools in function definitions." } , LintCompletion { label : "clippy::fn_to_numeric_cast" , description : "Checks for casts of function pointers to something other than usize" } , LintCompletion { label : "clippy::fn_to_numeric_cast_with_truncation" , description : "Checks for casts of a function pointer to a numeric type not wide enough to\\nstore address." } , LintCompletion { label : "clippy::for_kv_map" , description : "Checks for iterating a map (`HashMap` or `BTreeMap`) and\\nignoring either the keys or values." } , LintCompletion { label : "clippy::for_loops_over_fallibles" , description : "Checks for `for` loops over `Option` or `Result` values." } , LintCompletion { label : "clippy::forget_copy" , description : "Checks for calls to `std::mem::forget` with a value that\\nderives the Copy trait" } , LintCompletion { label : "clippy::forget_ref" , description : "Checks for calls to `std::mem::forget` with a reference\\ninstead of an owned value." } , LintCompletion { label : "clippy::future_not_send" , description : "This lint requires Future implementations returned from\\nfunctions and methods to implement the `Send` marker trait. It is mostly\\nused by library authors (public and internal) that target an audience where\\nmultithreaded executors are likely to be used for running these Futures." } , LintCompletion { label : "clippy::get_last_with_len" , description : "Checks for using `x.get(x.len() - 1)` instead of\\n`x.last()`." } , LintCompletion { label : "clippy::get_unwrap" , description : "Checks for use of `.get().unwrap()` (or\\n`.get_mut().unwrap`) on a standard library type which implements `Index`" } , LintCompletion { label : "clippy::identity_op" , description : "Checks for identity operations, e.g., `x + 0`." } , LintCompletion { label : "clippy::if_let_mutex" , description : "Checks for `Mutex::lock` calls in `if let` expression\\nwith lock calls in any of the else blocks." } , LintCompletion { label : "clippy::if_let_redundant_pattern_matching" , description : "Nothing. This lint has been deprecated." } , LintCompletion { label : "clippy::if_let_some_result" , description : "* Checks for unnecessary `ok()` in if let." } , LintCompletion { label : "clippy::if_not_else" , description : "Checks for usage of `!` or `!=` in an if condition with an\\nelse branch." } , LintCompletion { label : "clippy::if_same_then_else" , description : "Checks for `if/else` with the same body as the *then* part\\nand the *else* part." } , LintCompletion { label : "clippy::ifs_same_cond" , description : "Checks for consecutive `if`s with the same condition." } , LintCompletion { label : "clippy::implicit_hasher" , description : "Checks for public `impl` or `fn` missing generalization\\nover different hashers and implicitly defaulting to the default hashing\\nalgorithm (`SipHash`)." } , LintCompletion { label : "clippy::implicit_return" , description : "Checks for missing return statements at the end of a block." } , LintCompletion { label : "clippy::implicit_saturating_sub" , description : "Checks for implicit saturating subtraction." } , LintCompletion { label : "clippy::imprecise_flops" , description : "Looks for floating-point expressions that\\ncan be expressed using built-in methods to improve accuracy\\nat the cost of performance." } , LintCompletion { label : "clippy::inconsistent_digit_grouping" , description : "Warns if an integral or floating-point constant is\\ngrouped inconsistently with underscores." } , LintCompletion { label : "clippy::indexing_slicing" , description : "Checks for usage of indexing or slicing. Arrays are special cases, this lint\\ndoes report on arrays if we can tell that slicing operations are in bounds and does not\\nlint on constant `usize` indexing on arrays because that is handled by rustc's `const_err` lint." } , LintCompletion { label : "clippy::ineffective_bit_mask" , description : "Checks for bit masks in comparisons which can be removed\\nwithout changing the outcome. The basic structure can be seen in the\\nfollowing table:\\n\\n|Comparison| Bit Op |Example |equals |\\n|----------|---------|-----------|-------|\\n|`>` / `<=`|`|` / `^`|`x | 2 > 3`|`x > 3`|\\n|`<` / `>=`|`|` / `^`|`x ^ 1 < 4`|`x < 4`|" } , LintCompletion { label : "clippy::inefficient_to_string" , description : "Checks for usage of `.to_string()` on an `&&T` where\\n`T` implements `ToString` directly (like `&&str` or `&&String`)." } , LintCompletion { label : "clippy::infallible_destructuring_match" , description : "Checks for matches being used to destructure a single-variant enum\\nor tuple struct where a `let` will suffice." } , LintCompletion { label : "clippy::infinite_iter" , description : "Checks for iteration that is guaranteed to be infinite." } , LintCompletion { label : "clippy::inherent_to_string" , description : "Checks for the definition of inherent methods with a signature of `to_string(&self) -> String`." } , LintCompletion { label : "clippy::inherent_to_string_shadow_display" , description : "Checks for the definition of inherent methods with a signature of `to_string(&self) -> String` and if the type implementing this method also implements the `Display` trait." } , LintCompletion { label : "clippy::inline_always" , description : "Checks for items annotated with `#[inline(always)]`,\\nunless the annotated function is empty or simply panics." } , LintCompletion { label : "clippy::inline_asm_x86_att_syntax" , description : "Checks for usage of AT&T x86 assembly syntax." } , LintCompletion { label : "clippy::inline_asm_x86_intel_syntax" , description : "Checks for usage of Intel x86 assembly syntax." } , LintCompletion { label : "clippy::inline_fn_without_body" , description : "Checks for `#[inline]` on trait methods without bodies" } , LintCompletion { label : "clippy::int_plus_one" , description : "Checks for usage of `x >= y + 1` or `x - 1 >= y` (and `<=`) in a block" } , LintCompletion { label : "clippy::integer_arithmetic" , description : "Checks for integer arithmetic operations which could overflow or panic.\\n\\nSpecifically, checks for any operators (`+`, `-`, `*`, `<<`, etc) which are capable\\nof overflowing according to the [Rust\\nReference](https://doc.rust-lang.org/reference/expressions/operator-expr.html#overflow),\\nor which can panic (`/`, `%`). No bounds analysis or sophisticated reasoning is\\nattempted." } , LintCompletion { label : "clippy::integer_division" , description : "Checks for division of integers" } , LintCompletion { label : "clippy::into_iter_on_array" , description : "Nothing. This lint has been deprecated." } , LintCompletion { label : "clippy::into_iter_on_ref" , description : "Checks for `into_iter` calls on references which should be replaced by `iter`\\nor `iter_mut`." } , LintCompletion { label : "clippy::invalid_atomic_ordering" , description : "Checks for usage of invalid atomic\\nordering in atomic loads/stores/exchanges/updates and\\nmemory fences." } , LintCompletion { label : "clippy::invalid_ref" , description : "Nothing. This lint has been deprecated." } , LintCompletion { label : "clippy::invalid_regex" , description : "Checks [regex](https://crates.io/crates/regex) creation\\n(with `Regex::new`,`RegexBuilder::new` or `RegexSet::new`) for correct\\nregex syntax." } , LintCompletion { label : "clippy::invalid_upcast_comparisons" , description : "Checks for comparisons where the relation is always either\\ntrue or false, but where one side has been upcast so that the comparison is\\nnecessary. Only integer types are checked." } , LintCompletion { label : "clippy::invisible_characters" , description : "Checks for invisible Unicode characters in the code." } , LintCompletion { label : "clippy::items_after_statements" , description : "Checks for items declared after some statement in a block." } , LintCompletion { label : "clippy::iter_cloned_collect" , description : "Checks for the use of `.cloned().collect()` on slice to\\ncreate a `Vec`." } , LintCompletion { label : "clippy::iter_next_loop" , description : "Checks for loops on `x.next()`." } , LintCompletion { label : "clippy::iter_next_slice" , description : "Checks for usage of `iter().next()` on a Slice or an Array" } , LintCompletion { label : "clippy::iter_nth" , description : "Checks for use of `.iter().nth()` (and the related\\n`.iter_mut().nth()`) on standard library types with O(1) element access." } , LintCompletion { label : "clippy::iter_nth_zero" , description : "Checks for the use of `iter.nth(0)`." } , LintCompletion { label : "clippy::iter_skip_next" , description : "Checks for use of `.skip(x).next()` on iterators." } , LintCompletion { label : "clippy::iterator_step_by_zero" , description : "Checks for calling `.step_by(0)` on iterators which panics." } , LintCompletion { label : "clippy::just_underscores_and_digits" , description : "Checks if you have variables whose name consists of just\\nunderscores and digits." } , LintCompletion { label : "clippy::large_const_arrays" , description : "Checks for large `const` arrays that should\\nbe defined as `static` instead." } , LintCompletion { label : "clippy::large_digit_groups" , description : "Warns if the digits of an integral or floating-point\\nconstant are grouped into groups that\\nare too large." } , LintCompletion { label : "clippy::large_enum_variant" , description : "Checks for large size differences between variants on\\n`enum`s." } , LintCompletion { label : "clippy::large_stack_arrays" , description : "Checks for local arrays that may be too large." } , LintCompletion { label : "clippy::large_types_passed_by_value" , description : "Checks for functions taking arguments by value, where\\nthe argument type is `Copy` and large enough to be worth considering\\npassing by reference. Does not trigger if the function is being exported,\\nbecause that might induce API breakage, if the parameter is declared as mutable,\\nor if the argument is a `self`." } , LintCompletion { label : "clippy::len_without_is_empty" , description : "Checks for items that implement `.len()` but not\\n`.is_empty()`." } , LintCompletion { label : "clippy::len_zero" , description : "Checks for getting the length of something via `.len()`\\njust to compare to zero, and suggests using `.is_empty()` where applicable." } , LintCompletion { label : "clippy::let_and_return" , description : "Checks for `let`-bindings, which are subsequently\\nreturned." } , LintCompletion { label : "clippy::let_underscore_lock" , description : "Checks for `let _ = sync_lock`" } , LintCompletion { label : "clippy::let_underscore_must_use" , description : "Checks for `let _ = <expr>`\\nwhere expr is #[must_use]" } , LintCompletion { label : "clippy::let_unit_value" , description : "Checks for binding a unit value." } , LintCompletion { label : "clippy::linkedlist" , description : "Checks for usage of any `LinkedList`, suggesting to use a\\n`Vec` or a `VecDeque` (formerly called `RingBuf`)." } , LintCompletion { label : "clippy::logic_bug" , description : "Checks for boolean expressions that contain terminals that\\ncan be eliminated." } , LintCompletion { label : "clippy::lossy_float_literal" , description : "Checks for whole number float literals that\\ncannot be represented as the underlying type without loss." } , LintCompletion { label : "clippy::macro_use_imports" , description : "Checks for `#[macro_use] use...`." } , LintCompletion { label : "clippy::main_recursion" , description : "Checks for recursion using the entrypoint." } , LintCompletion { label : "clippy::manual_async_fn" , description : "It checks for manual implementations of `async` functions." } , LintCompletion { label : "clippy::manual_memcpy" , description : "Checks for for-loops that manually copy items between\\nslices that could be optimized by having a memcpy." } , LintCompletion { label : "clippy::manual_non_exhaustive" , description : "Checks for manual implementations of the non-exhaustive pattern." } , LintCompletion { label : "clippy::manual_saturating_arithmetic" , description : "Checks for `.checked_add/sub(x).unwrap_or(MAX/MIN)`." } , LintCompletion { label : "clippy::manual_strip" , description : "Suggests using `strip_{prefix,suffix}` over `str::{starts,ends}_with` and slicing using\\nthe pattern's length." } , LintCompletion { label : "clippy::manual_swap" , description : "Checks for manual swapping." } , LintCompletion { label : "clippy::manual_unwrap_or" , description : "Finds patterns that reimplement `Option::unwrap_or` or `Result::unwrap_or`." } , LintCompletion { label : "clippy::many_single_char_names" , description : "Checks for too many variables whose name consists of a\\nsingle character." } , LintCompletion { label : "clippy::map_clone" , description : "Checks for usage of `iterator.map(|x| x.clone())` and suggests\\n`iterator.cloned()` instead" } , LintCompletion { label : "clippy::map_entry" , description : "Checks for uses of `contains_key` + `insert` on `HashMap`\\nor `BTreeMap`." } , LintCompletion { label : "clippy::map_err_ignore" , description : "Checks for instances of `map_err(|_| Some::Enum)`" } , LintCompletion { label : "clippy::map_flatten" , description : "Checks for usage of `_.map(_).flatten(_)`," } , LintCompletion { label : "clippy::map_identity" , description : "Checks for instances of `map(f)` where `f` is the identity function." } , LintCompletion { label : "clippy::map_unwrap_or" , description : "Checks for usage of `option.map(_).unwrap_or(_)` or `option.map(_).unwrap_or_else(_)` or\\n`result.map(_).unwrap_or_else(_)`." } , LintCompletion { label : "clippy::match_as_ref" , description : "Checks for match which is used to add a reference to an\\n`Option` value." } , LintCompletion { label : "clippy::match_bool" , description : "Checks for matches where match expression is a `bool`. It\\nsuggests to replace the expression with an `if...else` block." } , LintCompletion { label : "clippy::match_like_matches_macro" , description : "Checks for `match` or `if let` expressions producing a\\n`bool` that could be written using `matches!`" } , LintCompletion { label : "clippy::match_on_vec_items" , description : "Checks for `match vec[idx]` or `match vec[n..m]`." } , LintCompletion { label : "clippy::match_overlapping_arm" , description : "Checks for overlapping match arms." } , LintCompletion { label : "clippy::match_ref_pats" , description : "Checks for matches where all arms match a reference,\\nsuggesting to remove the reference and deref the matched expression\\ninstead. It also checks for `if let &foo = bar` blocks." } , LintCompletion { label : "clippy::match_same_arms" , description : "Checks for `match` with identical arm bodies." } , LintCompletion { label : "clippy::match_single_binding" , description : "Checks for useless match that binds to only one value." } , LintCompletion { label : "clippy::match_wild_err_arm" , description : "Checks for arm which matches all errors with `Err(_)`\\nand take drastic actions like `panic!`." } , LintCompletion { label : "clippy::match_wildcard_for_single_variants" , description : "Checks for wildcard enum matches for a single variant." } , LintCompletion { label : "clippy::maybe_infinite_iter" , description : "Checks for iteration that may be infinite." } , LintCompletion { label : "clippy::mem_discriminant_non_enum" , description : "Checks for calls of `mem::discriminant()` on a non-enum type." } , LintCompletion { label : "clippy::mem_forget" , description : "Checks for usage of `std::mem::forget(t)` where `t` is\\n`Drop`." } , LintCompletion { label : "clippy::mem_replace_option_with_none" , description : "Checks for `mem::replace()` on an `Option` with\\n`None`." } , LintCompletion { label : "clippy::mem_replace_with_default" , description : "Checks for `std::mem::replace` on a value of type\\n`T` with `T::default()`." } , LintCompletion { label : "clippy::mem_replace_with_uninit" , description : "Checks for `mem::replace(&mut _, mem::uninitialized())`\\nand `mem::replace(&mut _, mem::zeroed())`." } , LintCompletion { label : "clippy::min_max" , description : "Checks for expressions where `std::cmp::min` and `max` are\\nused to clamp values, but switched so that the result is constant." } , LintCompletion { label : "clippy::misaligned_transmute" , description : "Nothing. This lint has been deprecated." } , LintCompletion { label : "clippy::mismatched_target_os" , description : "Checks for cfg attributes having operating systems used in target family position." } , LintCompletion { label : "clippy::misrefactored_assign_op" , description : "Checks for `a op= a op b` or `a op= b op a` patterns." } , LintCompletion { label : "clippy::missing_const_for_fn" , description : "Suggests the use of `const` in functions and methods where possible." } , LintCompletion { label : "clippy::missing_docs_in_private_items" , description : "Warns if there is missing doc for any documentable item\\n(public or private)." } , LintCompletion { label : "clippy::missing_errors_doc" , description : "Checks the doc comments of publicly visible functions that\\nreturn a `Result` type and warns if there is no `# Errors` section." } , LintCompletion { label : "clippy::missing_inline_in_public_items" , description : "it lints if an exported function, method, trait method with default impl,\\nor trait method impl is not `#[inline]`." } , LintCompletion { label : "clippy::missing_safety_doc" , description : "Checks for the doc comments of publicly visible\\nunsafe functions and warns if there is no `# Safety` section." } , LintCompletion { label : "clippy::mistyped_literal_suffixes" , description : "Warns for mistyped suffix in literals" } , LintCompletion { label : "clippy::mixed_case_hex_literals" , description : "Warns on hexadecimal literals with mixed-case letter\\ndigits." } , LintCompletion { label : "clippy::module_inception" , description : "Checks for modules that have the same name as their\\nparent module" } , LintCompletion { label : "clippy::module_name_repetitions" , description : "Detects type names that are prefixed or suffixed by the\\ncontaining module's name." } , LintCompletion { label : "clippy::modulo_arithmetic" , description : "Checks for modulo arithmetic." } , LintCompletion { label : "clippy::modulo_one" , description : "Checks for getting the remainder of a division by one." } , LintCompletion { label : "clippy::multiple_crate_versions" , description : "Checks to see if multiple versions of a crate are being\\nused." } , LintCompletion { label : "clippy::multiple_inherent_impl" , description : "Checks for multiple inherent implementations of a struct" } , LintCompletion { label : "clippy::must_use_candidate" , description : "Checks for public functions that have no\\n[`#[must_use]`] attribute, but return something not already marked\\nmust-use, have no mutable arg and mutate no statics.\\n\\n[`#[must_use]`]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute" } , LintCompletion { label : "clippy::must_use_unit" , description : "Checks for a [`#[must_use]`] attribute on\\nunit-returning functions and methods.\\n\\n[`#[must_use]`]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute" } , LintCompletion { label : "clippy::mut_from_ref" , description : "This lint checks for functions that take immutable\\nreferences and return mutable ones." } , LintCompletion { label : "clippy::mut_mut" , description : "Checks for instances of `mut mut` references." } , LintCompletion { label : "clippy::mut_range_bound" , description : "Checks for loops which have a range bound that is a mutable variable" } , LintCompletion { label : "clippy::mutable_key_type" , description : "Checks for sets/maps with mutable key types." } , LintCompletion { label : "clippy::mutex_atomic" , description : "Checks for usages of `Mutex<X>` where an atomic will do." } , LintCompletion { label : "clippy::mutex_integer" , description : "Checks for usages of `Mutex<X>` where `X` is an integral\\ntype." } , LintCompletion { label : "clippy::naive_bytecount" , description : "Checks for naive byte counts" } , LintCompletion { label : "clippy::needless_arbitrary_self_type" , description : "The lint checks for `self` in fn parameters that\\nspecify the `Self`-type explicitly" } , LintCompletion { label : "clippy::needless_bool" , description : "Checks for expressions of the form `if c { true } else {\\nfalse }` (or vice versa) and suggests using the condition directly." } , LintCompletion { label : "clippy::needless_borrow" , description : "Checks for address of operations (`&`) that are going to\\nbe dereferenced immediately by the compiler." } , LintCompletion { label : "clippy::needless_borrowed_reference" , description : "Checks for useless borrowed references." } , LintCompletion { label : "clippy::needless_collect" , description : "Checks for functions collecting an iterator when collect\\nis not needed." } , LintCompletion { label : "clippy::needless_continue" , description : "The lint checks for `if`-statements appearing in loops\\nthat contain a `continue` statement in either their main blocks or their\\n`else`-blocks, when omitting the `else`-block possibly with some\\nrearrangement of code can make the code easier to understand." } , LintCompletion { label : "clippy::needless_doctest_main" , description : "Checks for `fn main() { .. }` in doctests" } , LintCompletion { label : "clippy::needless_lifetimes" , description : "Checks for lifetime annotations which can be removed by\\nrelying on lifetime elision." } , LintCompletion { label : "clippy::needless_pass_by_value" , description : "Checks for functions taking arguments by value, but not\\nconsuming them in its\\nbody." } , LintCompletion { label : "clippy::needless_range_loop" , description : "Checks for looping over the range of `0..len` of some\\ncollection just to get the values by index." } , LintCompletion { label : "clippy::needless_return" , description : "Checks for return statements at the end of a block." } , LintCompletion { label : "clippy::needless_update" , description : "Checks for needlessly including a base struct on update\\nwhen all fields are changed anyway." } , LintCompletion { label : "clippy::neg_cmp_op_on_partial_ord" , description : "Checks for the usage of negated comparison operators on types which only implement\\n`PartialOrd` (e.g., `f64`)." } , LintCompletion { label : "clippy::neg_multiply" , description : "Checks for multiplication by -1 as a form of negation." } , LintCompletion { label : "clippy::never_loop" , description : "Checks for loops that will always `break`, `return` or\\n`continue` an outer loop." } , LintCompletion { label : "clippy::new_ret_no_self" , description : "Checks for `new` not returning a type that contains `Self`." } , LintCompletion { label : "clippy::new_without_default" , description : "Checks for types with a `fn new() -> Self` method and no\\nimplementation of\\n[`Default`](https://doc.rust-lang.org/std/default/trait.Default.html)." } , LintCompletion { label : "clippy::no_effect" , description : "Checks for statements which have no effect." } , LintCompletion { label : "clippy::non_ascii_literal" , description : "Checks for non-ASCII characters in string literals." } , LintCompletion { label : "clippy::nonminimal_bool" , description : "Checks for boolean expressions that can be written more\\nconcisely." } , LintCompletion { label : "clippy::nonsensical_open_options" , description : "Checks for duplicate open options as well as combinations\\nthat make no sense." } , LintCompletion { label : "clippy::not_unsafe_ptr_arg_deref" , description : "Checks for public functions that dereference raw pointer\\narguments but are not marked unsafe." } , LintCompletion { label : "clippy::ok_expect" , description : "Checks for usage of `ok().expect(..)`." } , LintCompletion { label : "clippy::op_ref" , description : "Checks for arguments to `==` which have their address\\ntaken to satisfy a bound\\nand suggests to dereference the other argument instead" } , LintCompletion { label : "clippy::option_as_ref_deref" , description : "Checks for usage of `_.as_ref().map(Deref::deref)` or it's aliases (such as String::as_str)." } , LintCompletion { label : "clippy::option_env_unwrap" , description : "Checks for usage of `option_env!(...).unwrap()` and\\nsuggests usage of the `env!` macro." } , LintCompletion { label : "clippy::option_if_let_else" , description : "Lints usage of `if let Some(v) = ... { y } else { x }` which is more\\nidiomatically done with `Option::map_or` (if the else bit is a pure\\nexpression) or `Option::map_or_else` (if the else bit is an impure\\nexpression)." } , LintCompletion { label : "clippy::option_map_or_none" , description : "Checks for usage of `_.map_or(None, _)`." } , LintCompletion { label : "clippy::option_map_unit_fn" , description : "Checks for usage of `option.map(f)` where f is a function\\nor closure that returns the unit type `()`." } , LintCompletion { label : "clippy::option_option" , description : "Checks for use of `Option<Option<_>>` in function signatures and type\\ndefinitions" } , LintCompletion { label : "clippy::or_fun_call" , description : "Checks for calls to `.or(foo(..))`, `.unwrap_or(foo(..))`,\\netc., and suggests to use `or_else`, `unwrap_or_else`, etc., or\\n`unwrap_or_default` instead." } , LintCompletion { label : "clippy::out_of_bounds_indexing" , description : "Checks for out of bounds array indexing with a constant\\nindex." } , LintCompletion { label : "clippy::overflow_check_conditional" , description : "Detects classic underflow/overflow checks." } , LintCompletion { label : "clippy::panic" , description : "Checks for usage of `panic!`." } , LintCompletion { label : "clippy::panic_in_result_fn" , description : "Checks for usage of `panic!`, `unimplemented!`, `todo!` or `unreachable!` in a function of type result." } , LintCompletion { label : "clippy::panic_params" , description : "Checks for missing parameters in `panic!`." } , LintCompletion { label : "clippy::panicking_unwrap" , description : "Checks for calls of `unwrap[_err]()` that will always fail." } , LintCompletion { label : "clippy::partialeq_ne_impl" , description : "Checks for manual re-implementations of `PartialEq::ne`." } , LintCompletion { label : "clippy::path_buf_push_overwrite" , description : "* Checks for [push](https://doc.rust-lang.org/std/path/struct.PathBuf.html#method.push)\\ncalls on `PathBuf` that can cause overwrites." } , LintCompletion { label : "clippy::pattern_type_mismatch" , description : "Checks for patterns that aren't exact representations of the types\\nthey are applied to.\\n\\nTo satisfy this lint, you will have to adjust either the expression that is matched\\nagainst or the pattern itself, as well as the bindings that are introduced by the\\nadjusted patterns. For matching you will have to either dereference the expression\\nwith the `*` operator, or amend the patterns to explicitly match against `&<pattern>`\\nor `&mut <pattern>` depending on the reference mutability. For the bindings you need\\nto use the inverse. You can leave them as plain bindings if you wish for the value\\nto be copied, but you must use `ref mut <variable>` or `ref <variable>` to construct\\na reference into the matched structure.\\n\\nIf you are looking for a way to learn about ownership semantics in more detail, it\\nis recommended to look at IDE options available to you to highlight types, lifetimes\\nand reference semantics in your code. The available tooling would expose these things\\nin a general way even outside of the various pattern matching mechanics. Of course\\nthis lint can still be used to highlight areas of interest and ensure a good understanding\\nof ownership semantics." } , LintCompletion { label : "clippy::possible_missing_comma" , description : "Checks for possible missing comma in an array. It lints if\\nan array element is a binary operator expression and it lies on two lines." } , LintCompletion { label : "clippy::precedence" , description : "Checks for operations where precedence may be unclear\\nand suggests to add parentheses. Currently it catches the following:\\n* mixed usage of arithmetic and bit shifting/combining operators without\\nparentheses\\n* a \\\"negative\\\" numeric literal (which is really a unary `-` followed by a\\nnumeric literal)\\n followed by a method call" } , LintCompletion { label : "clippy::print_literal" , description : "This lint warns about the use of literals as `print!`/`println!` args." } , LintCompletion { label : "clippy::print_stdout" , description : "Checks for printing on *stdout*. The purpose of this lint\\nis to catch debugging remnants." } , LintCompletion { label : "clippy::print_with_newline" , description : "This lint warns when you use `print!()` with a format\\nstring that ends in a newline." } , LintCompletion { label : "clippy::println_empty_string" , description : "This lint warns when you use `println!(\\\"\\\")` to\\nprint a newline." } , LintCompletion { label : "clippy::ptr_arg" , description : "This lint checks for function arguments of type `&String`\\nor `&Vec` unless the references are mutable. It will also suggest you\\nreplace `.clone()` calls with the appropriate `.to_owned()`/`to_string()`\\ncalls." } , LintCompletion { label : "clippy::ptr_eq" , description : "Use `std::ptr::eq` when applicable" } , LintCompletion { label : "clippy::ptr_offset_with_cast" , description : "Checks for usage of the `offset` pointer method with a `usize` casted to an\\n`isize`." } , LintCompletion { label : "clippy::pub_enum_variant_names" , description : "Detects public enumeration variants that are\\nprefixed or suffixed by the same characters." } , LintCompletion { label : "clippy::question_mark" , description : "Checks for expressions that could be replaced by the question mark operator." } , LintCompletion { label : "clippy::range_minus_one" , description : "Checks for inclusive ranges where 1 is subtracted from\\nthe upper bound, e.g., `x..=(y-1)`." } , LintCompletion { label : "clippy::range_plus_one" , description : "Checks for exclusive ranges where 1 is added to the\\nupper bound, e.g., `x..(y+1)`." } , LintCompletion { label : "clippy::range_step_by_zero" , description : "Nothing. This lint has been deprecated." } , LintCompletion { label : "clippy::range_zip_with_len" , description : "Checks for zipping a collection with the range of\\n`0.._.len()`." } , LintCompletion { label : "clippy::rc_buffer" , description : "Checks for `Rc<T>` and `Arc<T>` when `T` is a mutable buffer type such as `String` or `Vec`." } , LintCompletion { label : "clippy::redundant_allocation" , description : "Checks for use of redundant allocations anywhere in the code." } , LintCompletion { label : "clippy::redundant_clone" , description : "Checks for a redundant `clone()` (and its relatives) which clones an owned\\nvalue that is going to be dropped without further use." } , LintCompletion { label : "clippy::redundant_closure" , description : "Checks for closures which just call another function where\\nthe function can be called directly. `unsafe` functions or calls where types\\nget adjusted are ignored." } , LintCompletion { label : "clippy::redundant_closure_call" , description : "Detects closures called in the same expression where they\\nare defined." } , LintCompletion { label : "clippy::redundant_closure_for_method_calls" , description : "Checks for closures which only invoke a method on the closure\\nargument and can be replaced by referencing the method directly." } , LintCompletion { label : "clippy::redundant_field_names" , description : "Checks for fields in struct literals where shorthands\\ncould be used." } , LintCompletion { label : "clippy::redundant_pattern" , description : "Checks for patterns in the form `name @ _`." } , LintCompletion { label : "clippy::redundant_pattern_matching" , description : "Lint for redundant pattern matching over `Result` or\\n`Option`" } , LintCompletion { label : "clippy::redundant_pub_crate" , description : "Checks for items declared `pub(crate)` that are not crate visible because they\\nare inside a private module." } , LintCompletion { label : "clippy::redundant_static_lifetimes" , description : "Checks for constants and statics with an explicit `'static` lifetime." } , LintCompletion { label : "clippy::ref_in_deref" , description : "Checks for references in expressions that use\\nauto dereference." } , LintCompletion { label : "clippy::regex_macro" , description : "Nothing. This lint has been deprecated." } , LintCompletion { label : "clippy::repeat_once" , description : "Checks for usage of `.repeat(1)` and suggest the following method for each types.\\n- `.to_string()` for `str`\\n- `.clone()` for `String`\\n- `.to_vec()` for `slice`" } , LintCompletion { label : "clippy::replace_consts" , description : "Nothing. This lint has been deprecated." } , LintCompletion { label : "clippy::rest_pat_in_fully_bound_structs" , description : "Checks for unnecessary '..' pattern binding on struct when all fields are explicitly matched." } , LintCompletion { label : "clippy::result_map_or_into_option" , description : "Checks for usage of `_.map_or(None, Some)`." } , LintCompletion { label : "clippy::result_map_unit_fn" , description : "Checks for usage of `result.map(f)` where f is a function\\nor closure that returns the unit type `()`." } , LintCompletion { label : "clippy::result_unit_err" , description : "Checks for public functions that return a `Result`\\nwith an `Err` type of `()`. It suggests using a custom type that\\nimplements [`std::error::Error`]." } , LintCompletion { label : "clippy::reversed_empty_ranges" , description : "Checks for range expressions `x..y` where both `x` and `y`\\nare constant and `x` is greater or equal to `y`." } , LintCompletion { label : "clippy::same_functions_in_if_condition" , description : "Checks for consecutive `if`s with the same function call." } , LintCompletion { label : "clippy::same_item_push" , description : "Checks whether a for loop is being used to push a constant\\nvalue into a Vec." } , LintCompletion { label : "clippy::search_is_some" , description : "Checks for an iterator search (such as `find()`,\\n`position()`, or `rposition()`) followed by a call to `is_some()`." } , LintCompletion { label : "clippy::self_assignment" , description : "Checks for explicit self-assignments." } , LintCompletion { label : "clippy::serde_api_misuse" , description : "Checks for mis-uses of the serde API." } , LintCompletion { label : "clippy::shadow_reuse" , description : "Checks for bindings that shadow other bindings already in\\nscope, while reusing the original value." } , LintCompletion { label : "clippy::shadow_same" , description : "Checks for bindings that shadow other bindings already in\\nscope, while just changing reference level or mutability." } , LintCompletion { label : "clippy::shadow_unrelated" , description : "Checks for bindings that shadow other bindings already in\\nscope, either without a initialization or with one that does not even use\\nthe original value." } , LintCompletion { label : "clippy::short_circuit_statement" , description : "Checks for the use of short circuit boolean conditions as\\na\\nstatement." } , LintCompletion { label : "clippy::should_assert_eq" , description : "Nothing. This lint has been deprecated." } , LintCompletion { label : "clippy::should_implement_trait" , description : "Checks for methods that should live in a trait\\nimplementation of a `std` trait (see [llogiq's blog\\npost](http://llogiq.github.io/2015/07/30/traits.html) for further\\ninformation) instead of an inherent implementation." } , LintCompletion { label : "clippy::similar_names" , description : "Checks for names that are very similar and thus confusing." } , LintCompletion { label : "clippy::single_char_pattern" , description : "Checks for string methods that receive a single-character\\n`str` as an argument, e.g., `_.split(\\\"x\\\")`." } , LintCompletion { label : "clippy::single_char_push_str" , description : "Warns when using `push_str` with a single-character string literal,\\nand `push` with a `char` would work fine." } , LintCompletion { label : "clippy::single_component_path_imports" , description : "Checking for imports with single component use path." } , LintCompletion { label : "clippy::single_match" , description : "Checks for matches with a single arm where an `if let`\\nwill usually suffice." } , LintCompletion { label : "clippy::single_match_else" , description : "Checks for matches with two arms where an `if let else` will\\nusually suffice." } , LintCompletion { label : "clippy::skip_while_next" , description : "Checks for usage of `_.skip_while(condition).next()`." } , LintCompletion { label : "clippy::slow_vector_initialization" , description : "Checks slow zero-filled vector initialization" } , LintCompletion { label : "clippy::stable_sort_primitive" , description : "When sorting primitive values (integers, bools, chars, as well\\nas arrays, slices, and tuples of such items), it is better to\\nuse an unstable sort than a stable sort." } , LintCompletion { label : "clippy::str_to_string" , description : "Nothing. This lint has been deprecated." } , LintCompletion { label : "clippy::string_add" , description : "Checks for all instances of `x + _` where `x` is of type\\n`String`, but only if [`string_add_assign`](#string_add_assign) does *not*\\nmatch." } , LintCompletion { label : "clippy::string_add_assign" , description : "Checks for string appends of the form `x = x + y` (without\\n`let`!)." } , LintCompletion { label : "clippy::string_extend_chars" , description : "Checks for the use of `.extend(s.chars())` where s is a\\n`&str` or `String`." } , LintCompletion { label : "clippy::string_lit_as_bytes" , description : "Checks for the `as_bytes` method called on string literals\\nthat contain only ASCII characters." } , LintCompletion { label : "clippy::string_to_string" , description : "Nothing. This lint has been deprecated." } , LintCompletion { label : "clippy::struct_excessive_bools" , description : "Checks for excessive\\nuse of bools in structs." } , LintCompletion { label : "clippy::suboptimal_flops" , description : "Looks for floating-point expressions that\\ncan be expressed using built-in methods to improve both\\naccuracy and performance." } , LintCompletion { label : "clippy::suspicious_arithmetic_impl" , description : "Lints for suspicious operations in impls of arithmetic operators, e.g.\\nsubtracting elements in an Add impl." } , LintCompletion { label : "clippy::suspicious_assignment_formatting" , description : "Checks for use of the non-existent `=*`, `=!` and `=-`\\noperators." } , LintCompletion { label : "clippy::suspicious_else_formatting" , description : "Checks for formatting of `else`. It lints if the `else`\\nis followed immediately by a newline or the `else` seems to be missing." } , LintCompletion { label : "clippy::suspicious_map" , description : "Checks for calls to `map` followed by a `count`." } , LintCompletion { label : "clippy::suspicious_op_assign_impl" , description : "Lints for suspicious operations in impls of OpAssign, e.g.\\nsubtracting elements in an AddAssign impl." } , LintCompletion { label : "clippy::suspicious_unary_op_formatting" , description : "Checks the formatting of a unary operator on the right hand side\\nof a binary operator. It lints if there is no space between the binary and unary operators,\\nbut there is a space between the unary and its operand." } , LintCompletion { label : "clippy::tabs_in_doc_comments" , description : "Checks doc comments for usage of tab characters." } , LintCompletion { label : "clippy::temporary_assignment" , description : "Checks for construction of a structure or tuple just to\\nassign a value in it." } , LintCompletion { label : "clippy::temporary_cstring_as_ptr" , description : "Checks for getting the inner pointer of a temporary\\n`CString`." } , LintCompletion { label : "clippy::to_digit_is_some" , description : "Checks for `.to_digit(..).is_some()` on `char`s." } , LintCompletion { label : "clippy::to_string_in_display" , description : "Checks for uses of `to_string()` in `Display` traits." } , LintCompletion { label : "clippy::todo" , description : "Checks for usage of `todo!`." } , LintCompletion { label : "clippy::too_many_arguments" , description : "Checks for functions with too many parameters." } , LintCompletion { label : "clippy::too_many_lines" , description : "Checks for functions with a large amount of lines." } , LintCompletion { label : "clippy::toplevel_ref_arg" , description : "Checks for function arguments and let bindings denoted as\\n`ref`." } , LintCompletion { label : "clippy::trait_duplication_in_bounds" , description : "Checks for cases where generics are being used and multiple\\nsyntax specifications for trait bounds are used simultaneously." } , LintCompletion { label : "clippy::transmute_bytes_to_str" , description : "Checks for transmutes from a `&[u8]` to a `&str`." } , LintCompletion { label : "clippy::transmute_float_to_int" , description : "Checks for transmutes from a float to an integer." } , LintCompletion { label : "clippy::transmute_int_to_bool" , description : "Checks for transmutes from an integer to a `bool`." } , LintCompletion { label : "clippy::transmute_int_to_char" , description : "Checks for transmutes from an integer to a `char`." } , LintCompletion { label : "clippy::transmute_int_to_float" , description : "Checks for transmutes from an integer to a float." } , LintCompletion { label : "clippy::transmute_ptr_to_ptr" , description : "Checks for transmutes from a pointer to a pointer, or\\nfrom a reference to a reference." } , LintCompletion { label : "clippy::transmute_ptr_to_ref" , description : "Checks for transmutes from a pointer to a reference." } , LintCompletion { label : "clippy::transmutes_expressible_as_ptr_casts" , description : "Checks for transmutes that could be a pointer cast." } , LintCompletion { label : "clippy::transmuting_null" , description : "Checks for transmute calls which would receive a null pointer." } , LintCompletion { label : "clippy::trivial_regex" , description : "Checks for trivial [regex](https://crates.io/crates/regex)\\ncreation (with `Regex::new`, `RegexBuilder::new` or `RegexSet::new`)." } , LintCompletion { label : "clippy::trivially_copy_pass_by_ref" , description : "Checks for functions taking arguments by reference, where\\nthe argument type is `Copy` and small enough to be more efficient to always\\npass by value." } , LintCompletion { label : "clippy::try_err" , description : "Checks for usages of `Err(x)?`." } , LintCompletion { label : "clippy::type_complexity" , description : "Checks for types used in structs, parameters and `let`\\ndeclarations above a certain complexity threshold." } , LintCompletion { label : "clippy::type_repetition_in_bounds" , description : "This lint warns about unnecessary type repetitions in trait bounds" } , LintCompletion { label : "clippy::unicode_not_nfc" , description : "Checks for string literals that contain Unicode in a form\\nthat is not equal to its\\n[NFC-recomposition](http://www.unicode.org/reports/tr15/#Norm_Forms)." } , LintCompletion { label : "clippy::unimplemented" , description : "Checks for usage of `unimplemented!`." } , LintCompletion { label : "clippy::uninit_assumed_init" , description : "Checks for `MaybeUninit::uninit().assume_init()`." } , LintCompletion { label : "clippy::unit_arg" , description : "Checks for passing a unit value as an argument to a function without using a\\nunit literal (`()`)." } , LintCompletion { label : "clippy::unit_cmp" , description : "Checks for comparisons to unit. This includes all binary\\ncomparisons (like `==` and `<`) and asserts." } , LintCompletion { label : "clippy::unit_return_expecting_ord" , description : "Checks for functions that expect closures of type\\nFn(...) -> Ord where the implemented closure returns the unit type.\\nThe lint also suggests to remove the semi-colon at the end of the statement if present." } , LintCompletion { label : "clippy::unknown_clippy_lints" , description : "Checks for `allow`/`warn`/`deny`/`forbid` attributes with scoped clippy\\nlints and if those lints exist in clippy. If there is an uppercase letter in the lint name\\n(not the tool name) and a lowercase version of this lint exists, it will suggest to lowercase\\nthe lint name." } , LintCompletion { label : "clippy::unnecessary_cast" , description : "Checks for casts to the same type, casts of int literals to integer types\\nand casts of float literals to float types." } , LintCompletion { label : "clippy::unnecessary_filter_map" , description : "Checks for `filter_map` calls which could be replaced by `filter` or `map`.\\nMore specifically it checks if the closure provided is only performing one of the\\nfilter or map operations and suggests the appropriate option." } , LintCompletion { label : "clippy::unnecessary_fold" , description : "Checks for using `fold` when a more succinct alternative exists.\\nSpecifically, this checks for `fold`s which could be replaced by `any`, `all`,\\n`sum` or `product`." } , LintCompletion { label : "clippy::unnecessary_lazy_evaluations" , description : "As the counterpart to `or_fun_call`, this lint looks for unnecessary\\nlazily evaluated closures on `Option` and `Result`.\\n\\nThis lint suggests changing the following functions, when eager evaluation results in\\nsimpler code:\\n - `unwrap_or_else` to `unwrap_or`\\n - `and_then` to `and`\\n - `or_else` to `or`\\n - `get_or_insert_with` to `get_or_insert`\\n - `ok_or_else` to `ok_or`" } , LintCompletion { label : "clippy::unnecessary_mut_passed" , description : "Detects passing a mutable reference to a function that only\\nrequires an immutable reference." } , LintCompletion { label : "clippy::unnecessary_operation" , description : "Checks for expression statements that can be reduced to a\\nsub-expression." } , LintCompletion { label : "clippy::unnecessary_sort_by" , description : "Detects uses of `Vec::sort_by` passing in a closure\\nwhich compares the two arguments, either directly or indirectly." } , LintCompletion { label : "clippy::unnecessary_unwrap" , description : "Checks for calls of `unwrap[_err]()` that cannot fail." } , LintCompletion { label : "clippy::unneeded_field_pattern" , description : "Checks for structure field patterns bound to wildcards." } , LintCompletion { label : "clippy::unneeded_wildcard_pattern" , description : "Checks for tuple patterns with a wildcard\\npattern (`_`) is next to a rest pattern (`..`).\\n\\n_NOTE_: While `_, ..` means there is at least one element left, `..`\\nmeans there are 0 or more elements left. This can make a difference\\nwhen refactoring, but shouldn't result in errors in the refactored code,\\nsince the wildcard pattern isn't used anyway." } , LintCompletion { label : "clippy::unnested_or_patterns" , description : "Checks for unnested or-patterns, e.g., `Some(0) | Some(2)` and\\nsuggests replacing the pattern with a nested one, `Some(0 | 2)`.\\n\\nAnother way to think of this is that it rewrites patterns in\\n*disjunctive normal form (DNF)* into *conjunctive normal form (CNF)*." } , LintCompletion { label : "clippy::unreachable" , description : "Checks for usage of `unreachable!`." } , LintCompletion { label : "clippy::unreadable_literal" , description : "Warns if a long integral or floating-point constant does\\nnot contain underscores." } , LintCompletion { label : "clippy::unsafe_derive_deserialize" , description : "Checks for deriving `serde::Deserialize` on a type that\\nhas methods using `unsafe`." } , LintCompletion { label : "clippy::unsafe_removed_from_name" , description : "Checks for imports that remove \\\"unsafe\\\" from an item's\\nname." } , LintCompletion { label : "clippy::unsafe_vector_initialization" , description : "Nothing. This lint has been deprecated." } , LintCompletion { label : "clippy::unseparated_literal_suffix" , description : "Warns if literal suffixes are not separated by an\\nunderscore." } , LintCompletion { label : "clippy::unsound_collection_transmute" , description : "Checks for transmutes between collections whose\\ntypes have different ABI, size or alignment." } , LintCompletion { label : "clippy::unstable_as_mut_slice" , description : "Nothing. This lint has been deprecated." } , LintCompletion { label : "clippy::unstable_as_slice" , description : "Nothing. This lint has been deprecated." } , LintCompletion { label : "clippy::unused_collect" , description : "Nothing. This lint has been deprecated." } , LintCompletion { label : "clippy::unused_io_amount" , description : "Checks for unused written/read amount." } , LintCompletion { label : "clippy::unused_label" , description : "Nothing. This lint has been deprecated." } , LintCompletion { label : "clippy::unused_self" , description : "Checks methods that contain a `self` argument but don't use it" } , LintCompletion { label : "clippy::unused_unit" , description : "Checks for unit (`()`) expressions that can be removed." } , LintCompletion { label : "clippy::unwrap_in_result" , description : "Checks for functions of type Result that contain `expect()` or `unwrap()`" } , LintCompletion { label : "clippy::unwrap_used" , description : "Checks for `.unwrap()` calls on `Option`s and on `Result`s." } , LintCompletion { label : "clippy::use_debug" , description : "Checks for use of `Debug` formatting. The purpose of this\\nlint is to catch debugging remnants." } , LintCompletion { label : "clippy::use_self" , description : "Checks for unnecessary repetition of structure name when a\\nreplacement with `Self` is applicable." } , LintCompletion { label : "clippy::used_underscore_binding" , description : "Checks for the use of bindings with a single leading\\nunderscore." } , LintCompletion { label : "clippy::useless_asref" , description : "Checks for usage of `.as_ref()` or `.as_mut()` where the\\ntypes before and after the call are the same." } , LintCompletion { label : "clippy::useless_attribute" , description : "Checks for `extern crate` and `use` items annotated with\\nlint attributes.\\n\\nThis lint permits `#[allow(unused_imports)]`, `#[allow(deprecated)]`,\\n`#[allow(unreachable_pub)]`, `#[allow(clippy::wildcard_imports)]` and\\n`#[allow(clippy::enum_glob_use)]` on `use` items and `#[allow(unused_imports)]` on\\n`extern crate` items with a `#[macro_use]` attribute." } , LintCompletion { label : "clippy::useless_conversion" , description : "Checks for `Into`, `TryInto`, `From`, `TryFrom`,`IntoIter` calls\\nthat useless converts to the same type as caller." } , LintCompletion { label : "clippy::useless_format" , description : "Checks for the use of `format!(\\\"string literal with no\\nargument\\\")` and `format!(\\\"{}\\\", foo)` where `foo` is a string." } , LintCompletion { label : "clippy::useless_let_if_seq" , description : "Checks for variable declarations immediately followed by a\\nconditional affectation." } , LintCompletion { label : "clippy::useless_transmute" , description : "Checks for transmutes to the original type of the object\\nand transmutes that could be a cast." } , LintCompletion { label : "clippy::useless_vec" , description : "Checks for usage of `&vec![..]` when using `&[..]` would\\nbe possible." } , LintCompletion { label : "clippy::vec_box" , description : "Checks for use of `Vec<Box<T>>` where T: Sized anywhere in the code.\\nCheck the [Box documentation](https://doc.rust-lang.org/std/boxed/index.html) for more information." } , LintCompletion { label : "clippy::vec_resize_to_zero" , description : "Finds occurrences of `Vec::resize(0, an_int)`" } , LintCompletion { label : "clippy::verbose_bit_mask" , description : "Checks for bit masks that can be replaced by a call\\nto `trailing_zeros`" } , LintCompletion { label : "clippy::verbose_file_reads" , description : "Checks for use of File::read_to_end and File::read_to_string." } , LintCompletion { label : "clippy::vtable_address_comparisons" , description : "Checks for comparisons with an address of a trait vtable." } , LintCompletion { label : "clippy::while_immutable_condition" , description : "Checks whether variables used within while loop condition\\ncan be (and are) mutated in the body." } , LintCompletion { label : "clippy::while_let_loop" , description : "Detects `loop + match` combinations that are easier\\nwritten as a `while let` loop." } , LintCompletion { label : "clippy::while_let_on_iterator" , description : "Checks for `while let` expressions on iterators." } , LintCompletion { label : "clippy::wildcard_dependencies" , description : "Checks for wildcard dependencies in the `Cargo.toml`." } , LintCompletion { label : "clippy::wildcard_enum_match_arm" , description : "Checks for wildcard enum matches using `_`." } , LintCompletion { label : "clippy::wildcard_imports" , description : "Checks for wildcard imports `use _::*`." } , LintCompletion { label : "clippy::wildcard_in_or_patterns" , description : "Checks for wildcard pattern used with others patterns in same match arm." } , LintCompletion { label : "clippy::write_literal" , description : "This lint warns about the use of literals as `write!`/`writeln!` args." } , LintCompletion { label : "clippy::write_with_newline" , description : "This lint warns when you use `write!()` with a format\\nstring that\\nends in a newline." } , LintCompletion { label : "clippy::writeln_empty_string" , description : "This lint warns when you use `writeln!(buf, \\\"\\\")` to\\nprint a newline." } , LintCompletion { label : "clippy::wrong_pub_self_convention" , description : "This is the same as\\n[`wrong_self_convention`](#wrong_self_convention), but for public items." } , LintCompletion { label : "clippy::wrong_self_convention" , description : "Checks for methods with certain name prefixes and which\\ndoesn't match how self is taken. The actual rules are:\\n\\n|Prefix |`self` taken |\\n|-------|----------------------|\\n|`as_` |`&self` or `&mut self`|\\n|`from_`| none |\\n|`into_`|`self` |\\n|`is_` |`&self` or none |\\n|`to_` |`&self` |" } , LintCompletion { label : "clippy::wrong_transmute" , description : "Checks for transmutes that can't ever be correct on any\\narchitecture." } , LintCompletion { label : "clippy::zero_divided_by_zero" , description : "Checks for `0.0 / 0.0`." } , LintCompletion { label : "clippy::zero_prefixed_literal" , description : "Warns if an integral constant literal starts with `0`." } , LintCompletion { label : "clippy::zero_ptr" , description : "Catch casts from `0` to some pointer type" } , LintCompletion { label : "clippy::zst_offset" , description : "Checks for `offset(_)`, `wrapping_`{`add`, `sub`}, etc. on raw pointers to\\nzero-sized types" }] ; | 5 | LintCompletion { |
6 | label: "plugin_registrar", | ||
7 | description: r##"# `plugin_registrar` | ||
8 | |||
9 | The tracking issue for this feature is: [#29597] | ||
10 | |||
11 | [#29597]: https://github.com/rust-lang/rust/issues/29597 | ||
12 | |||
13 | This feature is part of "compiler plugins." It will often be used with the | ||
14 | [`plugin`] and `rustc_private` features as well. For more details, see | ||
15 | their docs. | ||
16 | |||
17 | [`plugin`]: plugin.md | ||
18 | |||
19 | ------------------------ | ||
20 | "##, | ||
21 | }, | ||
22 | LintCompletion { | ||
23 | label: "inline_const", | ||
24 | description: r##"# `inline_const` | ||
25 | |||
26 | The tracking issue for this feature is: [#76001] | ||
27 | |||
28 | ------ | ||
29 | |||
30 | This feature allows you to use inline constant expressions. For example, you can | ||
31 | turn this code: | ||
32 | |||
33 | ```rust | ||
34 | # fn add_one(x: i32) -> i32 { x + 1 } | ||
35 | const MY_COMPUTATION: i32 = 1 + 2 * 3 / 4; | ||
36 | |||
37 | fn main() { | ||
38 | let x = add_one(MY_COMPUTATION); | ||
39 | } | ||
40 | ``` | ||
41 | |||
42 | into this code: | ||
43 | |||
44 | ```rust | ||
45 | #![feature(inline_const)] | ||
46 | |||
47 | # fn add_one(x: i32) -> i32 { x + 1 } | ||
48 | fn main() { | ||
49 | let x = add_one(const { 1 + 2 * 3 / 4 }); | ||
50 | } | ||
51 | ``` | ||
52 | |||
53 | You can also use inline constant expressions in patterns: | ||
54 | |||
55 | ```rust | ||
56 | #![feature(inline_const)] | ||
57 | |||
58 | const fn one() -> i32 { 1 } | ||
59 | |||
60 | let some_int = 3; | ||
61 | match some_int { | ||
62 | const { 1 + 2 } => println!("Matched 1 + 2"), | ||
63 | const { one() } => println!("Matched const fn returning 1"), | ||
64 | _ => println!("Didn't match anything :("), | ||
65 | } | ||
66 | ``` | ||
67 | |||
68 | [#76001]: https://github.com/rust-lang/rust/issues/76001 | ||
69 | "##, | ||
70 | }, | ||
71 | LintCompletion { | ||
72 | label: "auto_traits", | ||
73 | description: r##"# `auto_traits` | ||
74 | |||
75 | The tracking issue for this feature is [#13231] | ||
76 | |||
77 | [#13231]: https://github.com/rust-lang/rust/issues/13231 | ||
78 | |||
79 | ---- | ||
80 | |||
81 | The `auto_traits` feature gate allows you to define auto traits. | ||
82 | |||
83 | Auto traits, like [`Send`] or [`Sync`] in the standard library, are marker traits | ||
84 | that are automatically implemented for every type, unless the type, or a type it contains, | ||
85 | has explicitly opted out via a negative impl. (Negative impls are separately controlled | ||
86 | by the `negative_impls` feature.) | ||
87 | |||
88 | [`Send`]: https://doc.rust-lang.org/std/marker/trait.Send.html | ||
89 | [`Sync`]: https://doc.rust-lang.org/std/marker/trait.Sync.html | ||
90 | |||
91 | ```rust,ignore (partial-example) | ||
92 | impl !Trait for Type {} | ||
93 | ``` | ||
94 | |||
95 | Example: | ||
96 | |||
97 | ```rust | ||
98 | #![feature(negative_impls)] | ||
99 | #![feature(auto_traits)] | ||
100 | |||
101 | auto trait Valid {} | ||
102 | |||
103 | struct True; | ||
104 | struct False; | ||
105 | |||
106 | impl !Valid for False {} | ||
107 | |||
108 | struct MaybeValid<T>(T); | ||
109 | |||
110 | fn must_be_valid<T: Valid>(_t: T) { } | ||
111 | |||
112 | fn main() { | ||
113 | // works | ||
114 | must_be_valid( MaybeValid(True) ); | ||
115 | |||
116 | // compiler error - trait bound not satisfied | ||
117 | // must_be_valid( MaybeValid(False) ); | ||
118 | } | ||
119 | ``` | ||
120 | |||
121 | ## Automatic trait implementations | ||
122 | |||
123 | When a type is declared as an `auto trait`, we will automatically | ||
124 | create impls for every struct/enum/union, unless an explicit impl is | ||
125 | provided. These automatic impls contain a where clause for each field | ||
126 | of the form `T: AutoTrait`, where `T` is the type of the field and | ||
127 | `AutoTrait` is the auto trait in question. As an example, consider the | ||
128 | struct `List` and the auto trait `Send`: | ||
129 | |||
130 | ```rust | ||
131 | struct List<T> { | ||
132 | data: T, | ||
133 | next: Option<Box<List<T>>>, | ||
134 | } | ||
135 | ``` | ||
136 | |||
137 | Presuming that there is no explicit impl of `Send` for `List`, the | ||
138 | compiler will supply an automatic impl of the form: | ||
139 | |||
140 | ```rust | ||
141 | struct List<T> { | ||
142 | data: T, | ||
143 | next: Option<Box<List<T>>>, | ||
144 | } | ||
145 | |||
146 | unsafe impl<T> Send for List<T> | ||
147 | where | ||
148 | T: Send, // from the field `data` | ||
149 | Option<Box<List<T>>>: Send, // from the field `next` | ||
150 | { } | ||
151 | ``` | ||
152 | |||
153 | Explicit impls may be either positive or negative. They take the form: | ||
154 | |||
155 | ```rust,ignore (partial-example) | ||
156 | impl<...> AutoTrait for StructName<..> { } | ||
157 | impl<...> !AutoTrait for StructName<..> { } | ||
158 | ``` | ||
159 | |||
160 | ## Coinduction: Auto traits permit cyclic matching | ||
161 | |||
162 | Unlike ordinary trait matching, auto traits are **coinductive**. This | ||
163 | means, in short, that cycles which occur in trait matching are | ||
164 | considered ok. As an example, consider the recursive struct `List` | ||
165 | introduced in the previous section. In attempting to determine whether | ||
166 | `List: Send`, we would wind up in a cycle: to apply the impl, we must | ||
167 | show that `Option<Box<List>>: Send`, which will in turn require | ||
168 | `Box<List>: Send` and then finally `List: Send` again. Under ordinary | ||
169 | trait matching, this cycle would be an error, but for an auto trait it | ||
170 | is considered a successful match. | ||
171 | |||
172 | ## Items | ||
173 | |||
174 | Auto traits cannot have any trait items, such as methods or associated types. This ensures that we can generate default implementations. | ||
175 | |||
176 | ## Supertraits | ||
177 | |||
178 | Auto traits cannot have supertraits. This is for soundness reasons, as the interaction of coinduction with implied bounds is difficult to reconcile. | ||
179 | "##, | ||
180 | }, | ||
181 | LintCompletion { | ||
182 | label: "ffi_const", | ||
183 | description: r##"# `ffi_const` | ||
184 | |||
185 | The tracking issue for this feature is: [#58328] | ||
186 | |||
187 | ------ | ||
188 | |||
189 | The `#[ffi_const]` attribute applies clang's `const` attribute to foreign | ||
190 | functions declarations. | ||
191 | |||
192 | That is, `#[ffi_const]` functions shall have no effects except for its return | ||
193 | value, which can only depend on the values of the function parameters, and is | ||
194 | not affected by changes to the observable state of the program. | ||
195 | |||
196 | Applying the `#[ffi_const]` attribute to a function that violates these | ||
197 | requirements is undefined behaviour. | ||
198 | |||
199 | This attribute enables Rust to perform common optimizations, like sub-expression | ||
200 | elimination, and it can avoid emitting some calls in repeated invocations of the | ||
201 | function with the same argument values regardless of other operations being | ||
202 | performed in between these functions calls (as opposed to `#[ffi_pure]` | ||
203 | functions). | ||
204 | |||
205 | ## Pitfalls | ||
206 | |||
207 | A `#[ffi_const]` function can only read global memory that would not affect | ||
208 | its return value for the whole execution of the program (e.g. immutable global | ||
209 | memory). `#[ffi_const]` functions are referentially-transparent and therefore | ||
210 | more strict than `#[ffi_pure]` functions. | ||
211 | |||
212 | A common pitfall involves applying the `#[ffi_const]` attribute to a | ||
213 | function that reads memory through pointer arguments which do not necessarily | ||
214 | point to immutable global memory. | ||
215 | |||
216 | A `#[ffi_const]` function that returns unit has no effect on the abstract | ||
217 | machine's state, and a `#[ffi_const]` function cannot be `#[ffi_pure]`. | ||
218 | |||
219 | A `#[ffi_const]` function must not diverge, neither via a side effect (e.g. a | ||
220 | call to `abort`) nor by infinite loops. | ||
221 | |||
222 | When translating C headers to Rust FFI, it is worth verifying for which targets | ||
223 | the `const` attribute is enabled in those headers, and using the appropriate | ||
224 | `cfg` macros in the Rust side to match those definitions. While the semantics of | ||
225 | `const` are implemented identically by many C and C++ compilers, e.g., clang, | ||
226 | [GCC], [ARM C/C++ compiler], [IBM ILE C/C++], etc. they are not necessarily | ||
227 | implemented in this way on all of them. It is therefore also worth verifying | ||
228 | that the semantics of the C toolchain used to compile the binary being linked | ||
229 | against are compatible with those of the `#[ffi_const]`. | ||
230 | |||
231 | [#58328]: https://github.com/rust-lang/rust/issues/58328 | ||
232 | [ARM C/C++ compiler]: http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0491c/Cacgigch.html | ||
233 | [GCC]: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-const-function-attribute | ||
234 | [IBM ILE C/C++]: https://www.ibm.com/support/knowledgecenter/fr/ssw_ibm_i_71/rzarg/fn_attrib_const.htm | ||
235 | "##, | ||
236 | }, | ||
237 | LintCompletion { | ||
238 | label: "external_doc", | ||
239 | description: r##"# `external_doc` | ||
240 | |||
241 | The tracking issue for this feature is: [#44732] | ||
242 | |||
243 | The `external_doc` feature allows the use of the `include` parameter to the `#[doc]` attribute, to | ||
244 | include external files in documentation. Use the attribute in place of, or in addition to, regular | ||
245 | doc comments and `#[doc]` attributes, and `rustdoc` will load the given file when it renders | ||
246 | documentation for your crate. | ||
247 | |||
248 | With the following files in the same directory: | ||
249 | |||
250 | `external-doc.md`: | ||
251 | |||
252 | ```markdown | ||
253 | # My Awesome Type | ||
254 | |||
255 | This is the documentation for this spectacular type. | ||
256 | ``` | ||
257 | |||
258 | `lib.rs`: | ||
259 | |||
260 | ```no_run (needs-external-files) | ||
261 | #![feature(external_doc)] | ||
262 | |||
263 | #[doc(include = "external-doc.md")] | ||
264 | pub struct MyAwesomeType; | ||
265 | ``` | ||
266 | |||
267 | `rustdoc` will load the file `external-doc.md` and use it as the documentation for the `MyAwesomeType` | ||
268 | struct. | ||
269 | |||
270 | When locating files, `rustdoc` will base paths in the `src/` directory, as if they were alongside the | ||
271 | `lib.rs` for your crate. So if you want a `docs/` folder to live alongside the `src/` directory, | ||
272 | start your paths with `../docs/` for `rustdoc` to properly find the file. | ||
273 | |||
274 | This feature was proposed in [RFC #1990] and initially implemented in PR [#44781]. | ||
275 | |||
276 | [#44732]: https://github.com/rust-lang/rust/issues/44732 | ||
277 | [RFC #1990]: https://github.com/rust-lang/rfcs/pull/1990 | ||
278 | [#44781]: https://github.com/rust-lang/rust/pull/44781 | ||
279 | "##, | ||
280 | }, | ||
281 | LintCompletion { | ||
282 | label: "box_patterns", | ||
283 | description: r##"# `box_patterns` | ||
284 | |||
285 | The tracking issue for this feature is: [#29641] | ||
286 | |||
287 | [#29641]: https://github.com/rust-lang/rust/issues/29641 | ||
288 | |||
289 | See also [`box_syntax`](box-syntax.md) | ||
290 | |||
291 | ------------------------ | ||
292 | |||
293 | Box patterns let you match on `Box<T>`s: | ||
294 | |||
295 | |||
296 | ```rust | ||
297 | #![feature(box_patterns)] | ||
298 | |||
299 | fn main() { | ||
300 | let b = Some(Box::new(5)); | ||
301 | match b { | ||
302 | Some(box n) if n < 0 => { | ||
303 | println!("Box contains negative number {}", n); | ||
304 | }, | ||
305 | Some(box n) if n >= 0 => { | ||
306 | println!("Box contains non-negative number {}", n); | ||
307 | }, | ||
308 | None => { | ||
309 | println!("No box"); | ||
310 | }, | ||
311 | _ => unreachable!() | ||
312 | } | ||
313 | } | ||
314 | ``` | ||
315 | "##, | ||
316 | }, | ||
317 | LintCompletion { | ||
318 | label: "abi_c_cmse_nonsecure_call", | ||
319 | description: r##"# `abi_c_cmse_nonsecure_call` | ||
320 | |||
321 | The tracking issue for this feature is: [#81391] | ||
322 | |||
323 | [#81391]: https://github.com/rust-lang/rust/issues/81391 | ||
324 | |||
325 | ------------------------ | ||
326 | |||
327 | The [TrustZone-M | ||
328 | feature](https://developer.arm.com/documentation/100690/latest/) is available | ||
329 | for targets with the Armv8-M architecture profile (`thumbv8m` in their target | ||
330 | name). | ||
331 | LLVM, the Rust compiler and the linker are providing | ||
332 | [support](https://developer.arm.com/documentation/ecm0359818/latest/) for the | ||
333 | TrustZone-M feature. | ||
334 | |||
335 | One of the things provided, with this unstable feature, is the | ||
336 | `C-cmse-nonsecure-call` function ABI. This ABI is used on function pointers to | ||
337 | non-secure code to mark a non-secure function call (see [section | ||
338 | 5.5](https://developer.arm.com/documentation/ecm0359818/latest/) for details). | ||
339 | |||
340 | With this ABI, the compiler will do the following to perform the call: | ||
341 | * save registers needed after the call to Secure memory | ||
342 | * clear all registers that might contain confidential information | ||
343 | * clear the Least Significant Bit of the function address | ||
344 | * branches using the BLXNS instruction | ||
345 | |||
346 | To avoid using the non-secure stack, the compiler will constrain the number and | ||
347 | type of parameters/return value. | ||
348 | |||
349 | The `extern "C-cmse-nonsecure-call"` ABI is otherwise equivalent to the | ||
350 | `extern "C"` ABI. | ||
351 | |||
352 | <!-- NOTE(ignore) this example is specific to thumbv8m targets --> | ||
353 | |||
354 | ``` rust,ignore | ||
355 | #![no_std] | ||
356 | #![feature(abi_c_cmse_nonsecure_call)] | ||
357 | |||
358 | #[no_mangle] | ||
359 | pub fn call_nonsecure_function(addr: usize) -> u32 { | ||
360 | let non_secure_function = | ||
361 | unsafe { core::mem::transmute::<usize, extern "C-cmse-nonsecure-call" fn() -> u32>(addr) }; | ||
362 | non_secure_function() | ||
363 | } | ||
364 | ``` | ||
365 | |||
366 | ``` text | ||
367 | $ rustc --emit asm --crate-type lib --target thumbv8m.main-none-eabi function.rs | ||
368 | |||
369 | call_nonsecure_function: | ||
370 | .fnstart | ||
371 | .save {r7, lr} | ||
372 | push {r7, lr} | ||
373 | .setfp r7, sp | ||
374 | mov r7, sp | ||
375 | .pad #16 | ||
376 | sub sp, #16 | ||
377 | str r0, [sp, #12] | ||
378 | ldr r0, [sp, #12] | ||
379 | str r0, [sp, #8] | ||
380 | b .LBB0_1 | ||
381 | .LBB0_1: | ||
382 | ldr r0, [sp, #8] | ||
383 | push.w {r4, r5, r6, r7, r8, r9, r10, r11} | ||
384 | bic r0, r0, #1 | ||
385 | mov r1, r0 | ||
386 | mov r2, r0 | ||
387 | mov r3, r0 | ||
388 | mov r4, r0 | ||
389 | mov r5, r0 | ||
390 | mov r6, r0 | ||
391 | mov r7, r0 | ||
392 | mov r8, r0 | ||
393 | mov r9, r0 | ||
394 | mov r10, r0 | ||
395 | mov r11, r0 | ||
396 | mov r12, r0 | ||
397 | msr apsr_nzcvq, r0 | ||
398 | blxns r0 | ||
399 | pop.w {r4, r5, r6, r7, r8, r9, r10, r11} | ||
400 | str r0, [sp, #4] | ||
401 | b .LBB0_2 | ||
402 | .LBB0_2: | ||
403 | ldr r0, [sp, #4] | ||
404 | add sp, #16 | ||
405 | pop {r7, pc} | ||
406 | ``` | ||
407 | "##, | ||
408 | }, | ||
409 | LintCompletion { | ||
410 | label: "member_constraints", | ||
411 | description: r##"# `member_constraints` | ||
412 | |||
413 | The tracking issue for this feature is: [#61997] | ||
414 | |||
415 | [#61997]: https://github.com/rust-lang/rust/issues/61997 | ||
416 | |||
417 | ------------------------ | ||
418 | |||
419 | The `member_constraints` feature gate lets you use `impl Trait` syntax with | ||
420 | multiple unrelated lifetime parameters. | ||
421 | |||
422 | A simple example is: | ||
423 | |||
424 | ```rust | ||
425 | #![feature(member_constraints)] | ||
426 | |||
427 | trait Trait<'a, 'b> { } | ||
428 | impl<T> Trait<'_, '_> for T {} | ||
429 | |||
430 | fn foo<'a, 'b>(x: &'a u32, y: &'b u32) -> impl Trait<'a, 'b> { | ||
431 | (x, y) | ||
432 | } | ||
433 | |||
434 | fn main() { } | ||
435 | ``` | ||
436 | |||
437 | Without the `member_constraints` feature gate, the above example is an | ||
438 | error because both `'a` and `'b` appear in the impl Trait bounds, but | ||
439 | neither outlives the other. | ||
440 | "##, | ||
441 | }, | ||
442 | LintCompletion { | ||
443 | label: "allocator_internals", | ||
444 | description: r##"# `allocator_internals` | ||
445 | |||
446 | This feature does not have a tracking issue, it is an unstable implementation | ||
447 | detail of the `global_allocator` feature not intended for use outside the | ||
448 | compiler. | ||
449 | |||
450 | ------------------------ | ||
451 | "##, | ||
452 | }, | ||
453 | LintCompletion { | ||
454 | label: "cfg_sanitize", | ||
455 | description: r##"# `cfg_sanitize` | ||
456 | |||
457 | The tracking issue for this feature is: [#39699] | ||
458 | |||
459 | [#39699]: https://github.com/rust-lang/rust/issues/39699 | ||
460 | |||
461 | ------------------------ | ||
462 | |||
463 | The `cfg_sanitize` feature makes it possible to execute different code | ||
464 | depending on whether a particular sanitizer is enabled or not. | ||
465 | |||
466 | ## Examples | ||
467 | |||
468 | ```rust | ||
469 | #![feature(cfg_sanitize)] | ||
470 | |||
471 | #[cfg(sanitize = "thread")] | ||
472 | fn a() { | ||
473 | // ... | ||
474 | } | ||
475 | |||
476 | #[cfg(not(sanitize = "thread"))] | ||
477 | fn a() { | ||
478 | // ... | ||
479 | } | ||
480 | |||
481 | fn b() { | ||
482 | if cfg!(sanitize = "leak") { | ||
483 | // ... | ||
484 | } else { | ||
485 | // ... | ||
486 | } | ||
487 | } | ||
488 | ``` | ||
489 | "##, | ||
490 | }, | ||
491 | LintCompletion { | ||
492 | label: "cfg_panic", | ||
493 | description: r##"# `cfg_panic` | ||
494 | |||
495 | The tracking issue for this feature is: [#77443] | ||
496 | |||
497 | [#77443]: https://github.com/rust-lang/rust/issues/77443 | ||
498 | |||
499 | ------------------------ | ||
500 | |||
501 | The `cfg_panic` feature makes it possible to execute different code | ||
502 | depending on the panic strategy. | ||
503 | |||
504 | Possible values at the moment are `"unwind"` or `"abort"`, although | ||
505 | it is possible that new panic strategies may be added to Rust in the | ||
506 | future. | ||
507 | |||
508 | ## Examples | ||
509 | |||
510 | ```rust | ||
511 | #![feature(cfg_panic)] | ||
512 | |||
513 | #[cfg(panic = "unwind")] | ||
514 | fn a() { | ||
515 | // ... | ||
516 | } | ||
517 | |||
518 | #[cfg(not(panic = "unwind"))] | ||
519 | fn a() { | ||
520 | // ... | ||
521 | } | ||
522 | |||
523 | fn b() { | ||
524 | if cfg!(panic = "abort") { | ||
525 | // ... | ||
526 | } else { | ||
527 | // ... | ||
528 | } | ||
529 | } | ||
530 | ``` | ||
531 | "##, | ||
532 | }, | ||
533 | LintCompletion { | ||
534 | label: "ffi_pure", | ||
535 | description: r##"# `ffi_pure` | ||
536 | |||
537 | The tracking issue for this feature is: [#58329] | ||
538 | |||
539 | ------ | ||
540 | |||
541 | The `#[ffi_pure]` attribute applies clang's `pure` attribute to foreign | ||
542 | functions declarations. | ||
543 | |||
544 | That is, `#[ffi_pure]` functions shall have no effects except for its return | ||
545 | value, which shall not change across two consecutive function calls with | ||
546 | the same parameters. | ||
547 | |||
548 | Applying the `#[ffi_pure]` attribute to a function that violates these | ||
549 | requirements is undefined behavior. | ||
550 | |||
551 | This attribute enables Rust to perform common optimizations, like sub-expression | ||
552 | elimination and loop optimizations. Some common examples of pure functions are | ||
553 | `strlen` or `memcmp`. | ||
554 | |||
555 | These optimizations are only applicable when the compiler can prove that no | ||
556 | program state observable by the `#[ffi_pure]` function has changed between calls | ||
557 | of the function, which could alter the result. See also the `#[ffi_const]` | ||
558 | attribute, which provides stronger guarantees regarding the allowable behavior | ||
559 | of a function, enabling further optimization. | ||
560 | |||
561 | ## Pitfalls | ||
562 | |||
563 | A `#[ffi_pure]` function can read global memory through the function | ||
564 | parameters (e.g. pointers), globals, etc. `#[ffi_pure]` functions are not | ||
565 | referentially-transparent, and are therefore more relaxed than `#[ffi_const]` | ||
566 | functions. | ||
567 | |||
568 | However, accessing global memory through volatile or atomic reads can violate the | ||
569 | requirement that two consecutive function calls shall return the same value. | ||
570 | |||
571 | A `pure` function that returns unit has no effect on the abstract machine's | ||
572 | state. | ||
573 | |||
574 | A `#[ffi_pure]` function must not diverge, neither via a side effect (e.g. a | ||
575 | call to `abort`) nor by infinite loops. | ||
576 | |||
577 | When translating C headers to Rust FFI, it is worth verifying for which targets | ||
578 | the `pure` attribute is enabled in those headers, and using the appropriate | ||
579 | `cfg` macros in the Rust side to match those definitions. While the semantics of | ||
580 | `pure` are implemented identically by many C and C++ compilers, e.g., clang, | ||
581 | [GCC], [ARM C/C++ compiler], [IBM ILE C/C++], etc. they are not necessarily | ||
582 | implemented in this way on all of them. It is therefore also worth verifying | ||
583 | that the semantics of the C toolchain used to compile the binary being linked | ||
584 | against are compatible with those of the `#[ffi_pure]`. | ||
585 | |||
586 | |||
587 | [#58329]: https://github.com/rust-lang/rust/issues/58329 | ||
588 | [ARM C/C++ compiler]: http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0491c/Cacigdac.html | ||
589 | [GCC]: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-pure-function-attribute | ||
590 | [IBM ILE C/C++]: https://www.ibm.com/support/knowledgecenter/fr/ssw_ibm_i_71/rzarg/fn_attrib_pure.htm | ||
591 | "##, | ||
592 | }, | ||
593 | LintCompletion { | ||
594 | label: "repr128", | ||
595 | description: r##"# `repr128` | ||
596 | |||
597 | The tracking issue for this feature is: [#56071] | ||
598 | |||
599 | [#56071]: https://github.com/rust-lang/rust/issues/56071 | ||
600 | |||
601 | ------------------------ | ||
602 | |||
603 | The `repr128` feature adds support for `#[repr(u128)]` on `enum`s. | ||
604 | |||
605 | ```rust | ||
606 | #![feature(repr128)] | ||
607 | |||
608 | #[repr(u128)] | ||
609 | enum Foo { | ||
610 | Bar(u64), | ||
611 | } | ||
612 | ``` | ||
613 | "##, | ||
614 | }, | ||
615 | LintCompletion { | ||
616 | label: "generators", | ||
617 | description: r##"# `generators` | ||
618 | |||
619 | The tracking issue for this feature is: [#43122] | ||
620 | |||
621 | [#43122]: https://github.com/rust-lang/rust/issues/43122 | ||
622 | |||
623 | ------------------------ | ||
624 | |||
625 | The `generators` feature gate in Rust allows you to define generator or | ||
626 | coroutine literals. A generator is a "resumable function" that syntactically | ||
627 | resembles a closure but compiles to much different semantics in the compiler | ||
628 | itself. The primary feature of a generator is that it can be suspended during | ||
629 | execution to be resumed at a later date. Generators use the `yield` keyword to | ||
630 | "return", and then the caller can `resume` a generator to resume execution just | ||
631 | after the `yield` keyword. | ||
632 | |||
633 | Generators are an extra-unstable feature in the compiler right now. Added in | ||
634 | [RFC 2033] they're mostly intended right now as a information/constraint | ||
635 | gathering phase. The intent is that experimentation can happen on the nightly | ||
636 | compiler before actual stabilization. A further RFC will be required to | ||
637 | stabilize generators/coroutines and will likely contain at least a few small | ||
638 | tweaks to the overall design. | ||
639 | |||
640 | [RFC 2033]: https://github.com/rust-lang/rfcs/pull/2033 | ||
641 | |||
642 | A syntactical example of a generator is: | ||
643 | |||
644 | ```rust | ||
645 | #![feature(generators, generator_trait)] | ||
646 | |||
647 | use std::ops::{Generator, GeneratorState}; | ||
648 | use std::pin::Pin; | ||
649 | |||
650 | fn main() { | ||
651 | let mut generator = || { | ||
652 | yield 1; | ||
653 | return "foo" | ||
654 | }; | ||
655 | |||
656 | match Pin::new(&mut generator).resume(()) { | ||
657 | GeneratorState::Yielded(1) => {} | ||
658 | _ => panic!("unexpected value from resume"), | ||
659 | } | ||
660 | match Pin::new(&mut generator).resume(()) { | ||
661 | GeneratorState::Complete("foo") => {} | ||
662 | _ => panic!("unexpected value from resume"), | ||
663 | } | ||
664 | } | ||
665 | ``` | ||
666 | |||
667 | Generators are closure-like literals which can contain a `yield` statement. The | ||
668 | `yield` statement takes an optional expression of a value to yield out of the | ||
669 | generator. All generator literals implement the `Generator` trait in the | ||
670 | `std::ops` module. The `Generator` trait has one main method, `resume`, which | ||
671 | resumes execution of the generator at the previous suspension point. | ||
672 | |||
673 | An example of the control flow of generators is that the following example | ||
674 | prints all numbers in order: | ||
675 | |||
676 | ```rust | ||
677 | #![feature(generators, generator_trait)] | ||
678 | |||
679 | use std::ops::Generator; | ||
680 | use std::pin::Pin; | ||
681 | |||
682 | fn main() { | ||
683 | let mut generator = || { | ||
684 | println!("2"); | ||
685 | yield; | ||
686 | println!("4"); | ||
687 | }; | ||
688 | |||
689 | println!("1"); | ||
690 | Pin::new(&mut generator).resume(()); | ||
691 | println!("3"); | ||
692 | Pin::new(&mut generator).resume(()); | ||
693 | println!("5"); | ||
694 | } | ||
695 | ``` | ||
696 | |||
697 | At this time the main intended use case of generators is an implementation | ||
698 | primitive for async/await syntax, but generators will likely be extended to | ||
699 | ergonomic implementations of iterators and other primitives in the future. | ||
700 | Feedback on the design and usage is always appreciated! | ||
701 | |||
702 | ### The `Generator` trait | ||
703 | |||
704 | The `Generator` trait in `std::ops` currently looks like: | ||
705 | |||
706 | ```rust | ||
707 | # #![feature(arbitrary_self_types, generator_trait)] | ||
708 | # use std::ops::GeneratorState; | ||
709 | # use std::pin::Pin; | ||
710 | |||
711 | pub trait Generator<R = ()> { | ||
712 | type Yield; | ||
713 | type Return; | ||
714 | fn resume(self: Pin<&mut Self>, resume: R) -> GeneratorState<Self::Yield, Self::Return>; | ||
715 | } | ||
716 | ``` | ||
717 | |||
718 | The `Generator::Yield` type is the type of values that can be yielded with the | ||
719 | `yield` statement. The `Generator::Return` type is the returned type of the | ||
720 | generator. This is typically the last expression in a generator's definition or | ||
721 | any value passed to `return` in a generator. The `resume` function is the entry | ||
722 | point for executing the `Generator` itself. | ||
723 | |||
724 | The return value of `resume`, `GeneratorState`, looks like: | ||
725 | |||
726 | ```rust | ||
727 | pub enum GeneratorState<Y, R> { | ||
728 | Yielded(Y), | ||
729 | Complete(R), | ||
730 | } | ||
731 | ``` | ||
732 | |||
733 | The `Yielded` variant indicates that the generator can later be resumed. This | ||
734 | corresponds to a `yield` point in a generator. The `Complete` variant indicates | ||
735 | that the generator is complete and cannot be resumed again. Calling `resume` | ||
736 | after a generator has returned `Complete` will likely result in a panic of the | ||
737 | program. | ||
738 | |||
739 | ### Closure-like semantics | ||
740 | |||
741 | The closure-like syntax for generators alludes to the fact that they also have | ||
742 | closure-like semantics. Namely: | ||
743 | |||
744 | * When created, a generator executes no code. A closure literal does not | ||
745 | actually execute any of the closure's code on construction, and similarly a | ||
746 | generator literal does not execute any code inside the generator when | ||
747 | constructed. | ||
748 | |||
749 | * Generators can capture outer variables by reference or by move, and this can | ||
750 | be tweaked with the `move` keyword at the beginning of the closure. Like | ||
751 | closures all generators will have an implicit environment which is inferred by | ||
752 | the compiler. Outer variables can be moved into a generator for use as the | ||
753 | generator progresses. | ||
754 | |||
755 | * Generator literals produce a value with a unique type which implements the | ||
756 | `std::ops::Generator` trait. This allows actual execution of the generator | ||
757 | through the `Generator::resume` method as well as also naming it in return | ||
758 | types and such. | ||
759 | |||
760 | * Traits like `Send` and `Sync` are automatically implemented for a `Generator` | ||
761 | depending on the captured variables of the environment. Unlike closures, | ||
762 | generators also depend on variables live across suspension points. This means | ||
763 | that although the ambient environment may be `Send` or `Sync`, the generator | ||
764 | itself may not be due to internal variables live across `yield` points being | ||
765 | not-`Send` or not-`Sync`. Note that generators do | ||
766 | not implement traits like `Copy` or `Clone` automatically. | ||
767 | |||
768 | * Whenever a generator is dropped it will drop all captured environment | ||
769 | variables. | ||
770 | |||
771 | ### Generators as state machines | ||
772 | |||
773 | In the compiler, generators are currently compiled as state machines. Each | ||
774 | `yield` expression will correspond to a different state that stores all live | ||
775 | variables over that suspension point. Resumption of a generator will dispatch on | ||
776 | the current state and then execute internally until a `yield` is reached, at | ||
777 | which point all state is saved off in the generator and a value is returned. | ||
778 | |||
779 | Let's take a look at an example to see what's going on here: | ||
780 | |||
781 | ```rust | ||
782 | #![feature(generators, generator_trait)] | ||
783 | |||
784 | use std::ops::Generator; | ||
785 | use std::pin::Pin; | ||
786 | |||
787 | fn main() { | ||
788 | let ret = "foo"; | ||
789 | let mut generator = move || { | ||
790 | yield 1; | ||
791 | return ret | ||
792 | }; | ||
793 | |||
794 | Pin::new(&mut generator).resume(()); | ||
795 | Pin::new(&mut generator).resume(()); | ||
796 | } | ||
797 | ``` | ||
798 | |||
799 | This generator literal will compile down to something similar to: | ||
800 | |||
801 | ```rust | ||
802 | #![feature(arbitrary_self_types, generators, generator_trait)] | ||
803 | |||
804 | use std::ops::{Generator, GeneratorState}; | ||
805 | use std::pin::Pin; | ||
806 | |||
807 | fn main() { | ||
808 | let ret = "foo"; | ||
809 | let mut generator = { | ||
810 | enum __Generator { | ||
811 | Start(&'static str), | ||
812 | Yield1(&'static str), | ||
813 | Done, | ||
814 | } | ||
815 | |||
816 | impl Generator for __Generator { | ||
817 | type Yield = i32; | ||
818 | type Return = &'static str; | ||
819 | |||
820 | fn resume(mut self: Pin<&mut Self>, resume: ()) -> GeneratorState<i32, &'static str> { | ||
821 | use std::mem; | ||
822 | match mem::replace(&mut *self, __Generator::Done) { | ||
823 | __Generator::Start(s) => { | ||
824 | *self = __Generator::Yield1(s); | ||
825 | GeneratorState::Yielded(1) | ||
826 | } | ||
827 | |||
828 | __Generator::Yield1(s) => { | ||
829 | *self = __Generator::Done; | ||
830 | GeneratorState::Complete(s) | ||
831 | } | ||
832 | |||
833 | __Generator::Done => { | ||
834 | panic!("generator resumed after completion") | ||
835 | } | ||
836 | } | ||
837 | } | ||
838 | } | ||
839 | |||
840 | __Generator::Start(ret) | ||
841 | }; | ||
842 | |||
843 | Pin::new(&mut generator).resume(()); | ||
844 | Pin::new(&mut generator).resume(()); | ||
845 | } | ||
846 | ``` | ||
847 | |||
848 | Notably here we can see that the compiler is generating a fresh type, | ||
849 | `__Generator` in this case. This type has a number of states (represented here | ||
850 | as an `enum`) corresponding to each of the conceptual states of the generator. | ||
851 | At the beginning we're closing over our outer variable `foo` and then that | ||
852 | variable is also live over the `yield` point, so it's stored in both states. | ||
853 | |||
854 | When the generator starts it'll immediately yield 1, but it saves off its state | ||
855 | just before it does so indicating that it has reached the yield point. Upon | ||
856 | resuming again we'll execute the `return ret` which returns the `Complete` | ||
857 | state. | ||
858 | |||
859 | Here we can also note that the `Done` state, if resumed, panics immediately as | ||
860 | it's invalid to resume a completed generator. It's also worth noting that this | ||
861 | is just a rough desugaring, not a normative specification for what the compiler | ||
862 | does. | ||
863 | "##, | ||
864 | }, | ||
865 | LintCompletion { | ||
866 | label: "non_ascii_idents", | ||
867 | description: r##"# `non_ascii_idents` | ||
868 | |||
869 | The tracking issue for this feature is: [#55467] | ||
870 | |||
871 | [#55467]: https://github.com/rust-lang/rust/issues/55467 | ||
872 | |||
873 | ------------------------ | ||
874 | |||
875 | The `non_ascii_idents` feature adds support for non-ASCII identifiers. | ||
876 | |||
877 | ## Examples | ||
878 | |||
879 | ```rust | ||
880 | #![feature(non_ascii_idents)] | ||
881 | |||
882 | const ε: f64 = 0.00001f64; | ||
883 | const Π: f64 = 3.14f64; | ||
884 | ``` | ||
885 | |||
886 | ## Changes to the language reference | ||
887 | |||
888 | > **<sup>Lexer:<sup>**\ | ||
889 | > IDENTIFIER :\ | ||
890 | > XID_start XID_continue<sup>\*</sup>\ | ||
891 | > | `_` XID_continue<sup>+</sup> | ||
892 | |||
893 | An identifier is any nonempty Unicode string of the following form: | ||
894 | |||
895 | Either | ||
896 | |||
897 | * The first character has property [`XID_start`] | ||
898 | * The remaining characters have property [`XID_continue`] | ||
899 | |||
900 | Or | ||
901 | |||
902 | * The first character is `_` | ||
903 | * The identifier is more than one character, `_` alone is not an identifier | ||
904 | * The remaining characters have property [`XID_continue`] | ||
905 | |||
906 | that does _not_ occur in the set of [strict keywords]. | ||
907 | |||
908 | > **Note**: [`XID_start`] and [`XID_continue`] as character properties cover the | ||
909 | > character ranges used to form the more familiar C and Java language-family | ||
910 | > identifiers. | ||
911 | |||
912 | [`XID_start`]: http://unicode.org/cldr/utility/list-unicodeset.jsp?a=%5B%3AXID_Start%3A%5D&abb=on&g=&i= | ||
913 | [`XID_continue`]: http://unicode.org/cldr/utility/list-unicodeset.jsp?a=%5B%3AXID_Continue%3A%5D&abb=on&g=&i= | ||
914 | [strict keywords]: ../../reference/keywords.md#strict-keywords | ||
915 | "##, | ||
916 | }, | ||
917 | LintCompletion { | ||
918 | label: "compiler_builtins", | ||
919 | description: r##"# `compiler_builtins` | ||
920 | |||
921 | This feature is internal to the Rust compiler and is not intended for general use. | ||
922 | |||
923 | ------------------------ | ||
924 | "##, | ||
925 | }, | ||
926 | LintCompletion { | ||
927 | label: "or_patterns", | ||
928 | description: r##"# `or_patterns` | ||
929 | |||
930 | The tracking issue for this feature is: [#54883] | ||
931 | |||
932 | [#54883]: https://github.com/rust-lang/rust/issues/54883 | ||
933 | |||
934 | ------------------------ | ||
935 | |||
936 | The `or_pattern` language feature allows `|` to be arbitrarily nested within | ||
937 | a pattern, for example, `Some(A(0) | B(1 | 2))` becomes a valid pattern. | ||
938 | |||
939 | ## Examples | ||
940 | |||
941 | ```rust,no_run | ||
942 | #![feature(or_patterns)] | ||
943 | |||
944 | pub enum Foo { | ||
945 | Bar, | ||
946 | Baz, | ||
947 | Quux, | ||
948 | } | ||
949 | |||
950 | pub fn example(maybe_foo: Option<Foo>) { | ||
951 | match maybe_foo { | ||
952 | Some(Foo::Bar | Foo::Baz) => { | ||
953 | println!("The value contained `Bar` or `Baz`"); | ||
954 | } | ||
955 | Some(_) => { | ||
956 | println!("The value did not contain `Bar` or `Baz`"); | ||
957 | } | ||
958 | None => { | ||
959 | println!("The value was `None`"); | ||
960 | } | ||
961 | } | ||
962 | } | ||
963 | ``` | ||
964 | "##, | ||
965 | }, | ||
966 | LintCompletion { | ||
967 | label: "negative_impls", | ||
968 | description: r##"# `negative_impls` | ||
969 | |||
970 | The tracking issue for this feature is [#68318]. | ||
971 | |||
972 | [#68318]: https://github.com/rust-lang/rust/issues/68318 | ||
973 | |||
974 | ---- | ||
975 | |||
976 | With the feature gate `negative_impls`, you can write negative impls as well as positive ones: | ||
977 | |||
978 | ```rust | ||
979 | #![feature(negative_impls)] | ||
980 | trait DerefMut { } | ||
981 | impl<T: ?Sized> !DerefMut for &T { } | ||
982 | ``` | ||
983 | |||
984 | Negative impls indicate a semver guarantee that the given trait will not be implemented for the given types. Negative impls play an additional purpose for auto traits, described below. | ||
985 | |||
986 | Negative impls have the following characteristics: | ||
987 | |||
988 | * They do not have any items. | ||
989 | * They must obey the orphan rules as if they were a positive impl. | ||
990 | * They cannot "overlap" with any positive impls. | ||
991 | |||
992 | ## Semver interaction | ||
993 | |||
994 | It is a breaking change to remove a negative impl. Negative impls are a commitment not to implement the given trait for the named types. | ||
995 | |||
996 | ## Orphan and overlap rules | ||
997 | |||
998 | Negative impls must obey the same orphan rules as a positive impl. This implies you cannot add a negative impl for types defined in upstream crates and so forth. | ||
999 | |||
1000 | Similarly, negative impls cannot overlap with positive impls, again using the same "overlap" check that we ordinarily use to determine if two impls overlap. (Note that positive impls typically cannot overlap with one another either, except as permitted by specialization.) | ||
1001 | |||
1002 | ## Interaction with auto traits | ||
1003 | |||
1004 | Declaring a negative impl `impl !SomeAutoTrait for SomeType` for an | ||
1005 | auto-trait serves two purposes: | ||
1006 | |||
1007 | * as with any trait, it declares that `SomeType` will never implement `SomeAutoTrait`; | ||
1008 | * it disables the automatic `SomeType: SomeAutoTrait` impl that would otherwise have been generated. | ||
1009 | |||
1010 | Note that, at present, there is no way to indicate that a given type | ||
1011 | does not implement an auto trait *but that it may do so in the | ||
1012 | future*. For ordinary types, this is done by simply not declaring any | ||
1013 | impl at all, but that is not an option for auto traits. A workaround | ||
1014 | is that one could embed a marker type as one of the fields, where the | ||
1015 | marker type is `!AutoTrait`. | ||
1016 | |||
1017 | ## Immediate uses | ||
1018 | |||
1019 | Negative impls are used to declare that `&T: !DerefMut` and `&mut T: !Clone`, as required to fix the soundness of `Pin` described in [#66544](https://github.com/rust-lang/rust/issues/66544). | ||
1020 | |||
1021 | This serves two purposes: | ||
1022 | |||
1023 | * For proving the correctness of unsafe code, we can use that impl as evidence that no `DerefMut` or `Clone` impl exists. | ||
1024 | * It prevents downstream crates from creating such impls. | ||
1025 | "##, | ||
1026 | }, | ||
1027 | LintCompletion { | ||
1028 | label: "cmse_nonsecure_entry", | ||
1029 | description: r##"# `cmse_nonsecure_entry` | ||
1030 | |||
1031 | The tracking issue for this feature is: [#75835] | ||
1032 | |||
1033 | [#75835]: https://github.com/rust-lang/rust/issues/75835 | ||
1034 | |||
1035 | ------------------------ | ||
1036 | |||
1037 | The [TrustZone-M | ||
1038 | feature](https://developer.arm.com/documentation/100690/latest/) is available | ||
1039 | for targets with the Armv8-M architecture profile (`thumbv8m` in their target | ||
1040 | name). | ||
1041 | LLVM, the Rust compiler and the linker are providing | ||
1042 | [support](https://developer.arm.com/documentation/ecm0359818/latest/) for the | ||
1043 | TrustZone-M feature. | ||
1044 | |||
1045 | One of the things provided, with this unstable feature, is the | ||
1046 | `cmse_nonsecure_entry` attribute. This attribute marks a Secure function as an | ||
1047 | entry function (see [section | ||
1048 | 5.4](https://developer.arm.com/documentation/ecm0359818/latest/) for details). | ||
1049 | With this attribute, the compiler will do the following: | ||
1050 | * add a special symbol on the function which is the `__acle_se_` prefix and the | ||
1051 | standard function name | ||
1052 | * constrain the number of parameters to avoid using the Non-Secure stack | ||
1053 | * before returning from the function, clear registers that might contain Secure | ||
1054 | information | ||
1055 | * use the `BXNS` instruction to return | ||
1056 | |||
1057 | Because the stack can not be used to pass parameters, there will be compilation | ||
1058 | errors if: | ||
1059 | * the total size of all parameters is too big (for example more than four 32 | ||
1060 | bits integers) | ||
1061 | * the entry function is not using a C ABI | ||
1062 | |||
1063 | The special symbol `__acle_se_` will be used by the linker to generate a secure | ||
1064 | gateway veneer. | ||
1065 | |||
1066 | <!-- NOTE(ignore) this example is specific to thumbv8m targets --> | ||
1067 | |||
1068 | ``` rust,ignore | ||
1069 | #![feature(cmse_nonsecure_entry)] | ||
1070 | |||
1071 | #[no_mangle] | ||
1072 | #[cmse_nonsecure_entry] | ||
1073 | pub extern "C" fn entry_function(input: u32) -> u32 { | ||
1074 | input + 6 | ||
1075 | } | ||
1076 | ``` | ||
1077 | |||
1078 | ``` text | ||
1079 | $ rustc --emit obj --crate-type lib --target thumbv8m.main-none-eabi function.rs | ||
1080 | $ arm-none-eabi-objdump -D function.o | ||
1081 | |||
1082 | 00000000 <entry_function>: | ||
1083 | 0: b580 push {r7, lr} | ||
1084 | 2: 466f mov r7, sp | ||
1085 | 4: b082 sub sp, #8 | ||
1086 | 6: 9001 str r0, [sp, #4] | ||
1087 | 8: 1d81 adds r1, r0, #6 | ||
1088 | a: 460a mov r2, r1 | ||
1089 | c: 4281 cmp r1, r0 | ||
1090 | e: 9200 str r2, [sp, #0] | ||
1091 | 10: d30b bcc.n 2a <entry_function+0x2a> | ||
1092 | 12: e7ff b.n 14 <entry_function+0x14> | ||
1093 | 14: 9800 ldr r0, [sp, #0] | ||
1094 | 16: b002 add sp, #8 | ||
1095 | 18: e8bd 4080 ldmia.w sp!, {r7, lr} | ||
1096 | 1c: 4671 mov r1, lr | ||
1097 | 1e: 4672 mov r2, lr | ||
1098 | 20: 4673 mov r3, lr | ||
1099 | 22: 46f4 mov ip, lr | ||
1100 | 24: f38e 8800 msr CPSR_f, lr | ||
1101 | 28: 4774 bxns lr | ||
1102 | 2a: f240 0000 movw r0, #0 | ||
1103 | 2e: f2c0 0000 movt r0, #0 | ||
1104 | 32: f240 0200 movw r2, #0 | ||
1105 | 36: f2c0 0200 movt r2, #0 | ||
1106 | 3a: 211c movs r1, #28 | ||
1107 | 3c: f7ff fffe bl 0 <_ZN4core9panicking5panic17h5c028258ca2fb3f5E> | ||
1108 | 40: defe udf #254 ; 0xfe | ||
1109 | ``` | ||
1110 | "##, | ||
1111 | }, | ||
1112 | LintCompletion { | ||
1113 | label: "plugin", | ||
1114 | description: r##"# `plugin` | ||
1115 | |||
1116 | The tracking issue for this feature is: [#29597] | ||
1117 | |||
1118 | [#29597]: https://github.com/rust-lang/rust/issues/29597 | ||
1119 | |||
1120 | |||
1121 | This feature is part of "compiler plugins." It will often be used with the | ||
1122 | [`plugin_registrar`] and `rustc_private` features. | ||
1123 | |||
1124 | [`plugin_registrar`]: plugin-registrar.md | ||
1125 | |||
1126 | ------------------------ | ||
1127 | |||
1128 | `rustc` can load compiler plugins, which are user-provided libraries that | ||
1129 | extend the compiler's behavior with new lint checks, etc. | ||
1130 | |||
1131 | A plugin is a dynamic library crate with a designated *registrar* function that | ||
1132 | registers extensions with `rustc`. Other crates can load these extensions using | ||
1133 | the crate attribute `#![plugin(...)]`. See the | ||
1134 | `rustc_driver::plugin` documentation for more about the | ||
1135 | mechanics of defining and loading a plugin. | ||
1136 | |||
1137 | In the vast majority of cases, a plugin should *only* be used through | ||
1138 | `#![plugin]` and not through an `extern crate` item. Linking a plugin would | ||
1139 | pull in all of librustc_ast and librustc as dependencies of your crate. This is | ||
1140 | generally unwanted unless you are building another plugin. | ||
1141 | |||
1142 | The usual practice is to put compiler plugins in their own crate, separate from | ||
1143 | any `macro_rules!` macros or ordinary Rust code meant to be used by consumers | ||
1144 | of a library. | ||
1145 | |||
1146 | # Lint plugins | ||
1147 | |||
1148 | Plugins can extend [Rust's lint | ||
1149 | infrastructure](../../reference/attributes/diagnostics.md#lint-check-attributes) with | ||
1150 | additional checks for code style, safety, etc. Now let's write a plugin | ||
1151 | [`lint-plugin-test.rs`](https://github.com/rust-lang/rust/blob/master/src/test/ui-fulldeps/auxiliary/lint-plugin-test.rs) | ||
1152 | that warns about any item named `lintme`. | ||
1153 | |||
1154 | ```rust,ignore (requires-stage-2) | ||
1155 | #![feature(plugin_registrar)] | ||
1156 | #![feature(box_syntax, rustc_private)] | ||
1157 | |||
1158 | extern crate rustc_ast; | ||
1159 | |||
1160 | // Load rustc as a plugin to get macros | ||
1161 | extern crate rustc_driver; | ||
1162 | #[macro_use] | ||
1163 | extern crate rustc_lint; | ||
1164 | #[macro_use] | ||
1165 | extern crate rustc_session; | ||
1166 | |||
1167 | use rustc_driver::plugin::Registry; | ||
1168 | use rustc_lint::{EarlyContext, EarlyLintPass, LintArray, LintContext, LintPass}; | ||
1169 | use rustc_ast::ast; | ||
1170 | declare_lint!(TEST_LINT, Warn, "Warn about items named 'lintme'"); | ||
1171 | |||
1172 | declare_lint_pass!(Pass => [TEST_LINT]); | ||
1173 | |||
1174 | impl EarlyLintPass for Pass { | ||
1175 | fn check_item(&mut self, cx: &EarlyContext, it: &ast::Item) { | ||
1176 | if it.ident.name.as_str() == "lintme" { | ||
1177 | cx.lint(TEST_LINT, |lint| { | ||
1178 | lint.build("item is named 'lintme'").set_span(it.span).emit() | ||
1179 | }); | ||
1180 | } | ||
1181 | } | ||
1182 | } | ||
1183 | |||
1184 | #[plugin_registrar] | ||
1185 | pub fn plugin_registrar(reg: &mut Registry) { | ||
1186 | reg.lint_store.register_lints(&[&TEST_LINT]); | ||
1187 | reg.lint_store.register_early_pass(|| box Pass); | ||
1188 | } | ||
1189 | ``` | ||
1190 | |||
1191 | Then code like | ||
1192 | |||
1193 | ```rust,ignore (requires-plugin) | ||
1194 | #![feature(plugin)] | ||
1195 | #![plugin(lint_plugin_test)] | ||
1196 | |||
1197 | fn lintme() { } | ||
1198 | ``` | ||
1199 | |||
1200 | will produce a compiler warning: | ||
1201 | |||
1202 | ```txt | ||
1203 | foo.rs:4:1: 4:16 warning: item is named 'lintme', #[warn(test_lint)] on by default | ||
1204 | foo.rs:4 fn lintme() { } | ||
1205 | ^~~~~~~~~~~~~~~ | ||
1206 | ``` | ||
1207 | |||
1208 | The components of a lint plugin are: | ||
1209 | |||
1210 | * one or more `declare_lint!` invocations, which define static `Lint` structs; | ||
1211 | |||
1212 | * a struct holding any state needed by the lint pass (here, none); | ||
1213 | |||
1214 | * a `LintPass` | ||
1215 | implementation defining how to check each syntax element. A single | ||
1216 | `LintPass` may call `span_lint` for several different `Lint`s, but should | ||
1217 | register them all through the `get_lints` method. | ||
1218 | |||
1219 | Lint passes are syntax traversals, but they run at a late stage of compilation | ||
1220 | where type information is available. `rustc`'s [built-in | ||
1221 | lints](https://github.com/rust-lang/rust/blob/master/src/librustc_session/lint/builtin.rs) | ||
1222 | mostly use the same infrastructure as lint plugins, and provide examples of how | ||
1223 | to access type information. | ||
1224 | |||
1225 | Lints defined by plugins are controlled by the usual [attributes and compiler | ||
1226 | flags](../../reference/attributes/diagnostics.md#lint-check-attributes), e.g. | ||
1227 | `#[allow(test_lint)]` or `-A test-lint`. These identifiers are derived from the | ||
1228 | first argument to `declare_lint!`, with appropriate case and punctuation | ||
1229 | conversion. | ||
1230 | |||
1231 | You can run `rustc -W help foo.rs` to see a list of lints known to `rustc`, | ||
1232 | including those provided by plugins loaded by `foo.rs`. | ||
1233 | "##, | ||
1234 | }, | ||
1235 | LintCompletion { | ||
1236 | label: "intrinsics", | ||
1237 | description: r##"# `intrinsics` | ||
1238 | |||
1239 | The tracking issue for this feature is: None. | ||
1240 | |||
1241 | Intrinsics are never intended to be stable directly, but intrinsics are often | ||
1242 | exported in some sort of stable manner. Prefer using the stable interfaces to | ||
1243 | the intrinsic directly when you can. | ||
1244 | |||
1245 | ------------------------ | ||
1246 | |||
1247 | |||
1248 | These are imported as if they were FFI functions, with the special | ||
1249 | `rust-intrinsic` ABI. For example, if one was in a freestanding | ||
1250 | context, but wished to be able to `transmute` between types, and | ||
1251 | perform efficient pointer arithmetic, one would import those functions | ||
1252 | via a declaration like | ||
1253 | |||
1254 | ```rust | ||
1255 | #![feature(intrinsics)] | ||
1256 | # fn main() {} | ||
1257 | |||
1258 | extern "rust-intrinsic" { | ||
1259 | fn transmute<T, U>(x: T) -> U; | ||
1260 | |||
1261 | fn offset<T>(dst: *const T, offset: isize) -> *const T; | ||
1262 | } | ||
1263 | ``` | ||
1264 | |||
1265 | As with any other FFI functions, these are always `unsafe` to call. | ||
1266 | "##, | ||
1267 | }, | ||
1268 | LintCompletion { | ||
1269 | label: "rustc_attrs", | ||
1270 | description: r##"# `rustc_attrs` | ||
1271 | |||
1272 | This feature has no tracking issue, and is therefore internal to | ||
1273 | the compiler, not being intended for general use. | ||
1274 | |||
1275 | Note: `rustc_attrs` enables many rustc-internal attributes and this page | ||
1276 | only discuss a few of them. | ||
1277 | |||
1278 | ------------------------ | ||
1279 | |||
1280 | The `rustc_attrs` feature allows debugging rustc type layouts by using | ||
1281 | `#[rustc_layout(...)]` to debug layout at compile time (it even works | ||
1282 | with `cargo check`) as an alternative to `rustc -Z print-type-sizes` | ||
1283 | that is way more verbose. | ||
1284 | |||
1285 | Options provided by `#[rustc_layout(...)]` are `debug`, `size`, `align`, | ||
1286 | `abi`. Note that it only works on sized types without generics. | ||
1287 | |||
1288 | ## Examples | ||
1289 | |||
1290 | ```rust,compile_fail | ||
1291 | #![feature(rustc_attrs)] | ||
1292 | |||
1293 | #[rustc_layout(abi, size)] | ||
1294 | pub enum X { | ||
1295 | Y(u8, u8, u8), | ||
1296 | Z(isize), | ||
1297 | } | ||
1298 | ``` | ||
1299 | |||
1300 | When that is compiled, the compiler will error with something like | ||
1301 | |||
1302 | ```text | ||
1303 | error: abi: Aggregate { sized: true } | ||
1304 | --> src/lib.rs:4:1 | ||
1305 | | | ||
1306 | 4 | / pub enum T { | ||
1307 | 5 | | Y(u8, u8, u8), | ||
1308 | 6 | | Z(isize), | ||
1309 | 7 | | } | ||
1310 | | |_^ | ||
1311 | |||
1312 | error: size: Size { raw: 16 } | ||
1313 | --> src/lib.rs:4:1 | ||
1314 | | | ||
1315 | 4 | / pub enum T { | ||
1316 | 5 | | Y(u8, u8, u8), | ||
1317 | 6 | | Z(isize), | ||
1318 | 7 | | } | ||
1319 | | |_^ | ||
1320 | |||
1321 | error: aborting due to 2 previous errors | ||
1322 | ``` | ||
1323 | "##, | ||
1324 | }, | ||
1325 | LintCompletion { | ||
1326 | label: "const_fn", | ||
1327 | description: r##"# `const_fn` | ||
1328 | |||
1329 | The tracking issue for this feature is: [#57563] | ||
1330 | |||
1331 | [#57563]: https://github.com/rust-lang/rust/issues/57563 | ||
1332 | |||
1333 | ------------------------ | ||
1334 | |||
1335 | The `const_fn` feature enables additional functionality not stabilized in the | ||
1336 | [minimal subset of `const_fn`](https://github.com/rust-lang/rust/issues/53555) | ||
1337 | "##, | ||
1338 | }, | ||
1339 | LintCompletion { | ||
1340 | label: "abi_thiscall", | ||
1341 | description: r##"# `abi_thiscall` | ||
1342 | |||
1343 | The tracking issue for this feature is: [#42202] | ||
1344 | |||
1345 | [#42202]: https://github.com/rust-lang/rust/issues/42202 | ||
1346 | |||
1347 | ------------------------ | ||
1348 | |||
1349 | The MSVC ABI on x86 Windows uses the `thiscall` calling convention for C++ | ||
1350 | instance methods by default; it is identical to the usual (C) calling | ||
1351 | convention on x86 Windows except that the first parameter of the method, | ||
1352 | the `this` pointer, is passed in the ECX register. | ||
1353 | "##, | ||
1354 | }, | ||
1355 | LintCompletion { | ||
1356 | label: "trait_alias", | ||
1357 | description: r##"# `trait_alias` | ||
1358 | |||
1359 | The tracking issue for this feature is: [#41517] | ||
1360 | |||
1361 | [#41517]: https://github.com/rust-lang/rust/issues/41517 | ||
1362 | |||
1363 | ------------------------ | ||
1364 | |||
1365 | The `trait_alias` feature adds support for trait aliases. These allow aliases | ||
1366 | to be created for one or more traits (currently just a single regular trait plus | ||
1367 | any number of auto-traits), and used wherever traits would normally be used as | ||
1368 | either bounds or trait objects. | ||
1369 | |||
1370 | ```rust | ||
1371 | #![feature(trait_alias)] | ||
1372 | |||
1373 | trait Foo = std::fmt::Debug + Send; | ||
1374 | trait Bar = Foo + Sync; | ||
1375 | |||
1376 | // Use trait alias as bound on type parameter. | ||
1377 | fn foo<T: Foo>(v: &T) { | ||
1378 | println!("{:?}", v); | ||
1379 | } | ||
1380 | |||
1381 | pub fn main() { | ||
1382 | foo(&1); | ||
1383 | |||
1384 | // Use trait alias for trait objects. | ||
1385 | let a: &Bar = &123; | ||
1386 | println!("{:?}", a); | ||
1387 | let b = Box::new(456) as Box<dyn Foo>; | ||
1388 | println!("{:?}", b); | ||
1389 | } | ||
1390 | ``` | ||
1391 | "##, | ||
1392 | }, | ||
1393 | LintCompletion { | ||
1394 | label: "lang_items", | ||
1395 | description: r##"# `lang_items` | ||
1396 | |||
1397 | The tracking issue for this feature is: None. | ||
1398 | |||
1399 | ------------------------ | ||
1400 | |||
1401 | The `rustc` compiler has certain pluggable operations, that is, | ||
1402 | functionality that isn't hard-coded into the language, but is | ||
1403 | implemented in libraries, with a special marker to tell the compiler | ||
1404 | it exists. The marker is the attribute `#[lang = "..."]` and there are | ||
1405 | various different values of `...`, i.e. various different 'lang | ||
1406 | items'. | ||
1407 | |||
1408 | For example, `Box` pointers require two lang items, one for allocation | ||
1409 | and one for deallocation. A freestanding program that uses the `Box` | ||
1410 | sugar for dynamic allocations via `malloc` and `free`: | ||
1411 | |||
1412 | ```rust,ignore (libc-is-finicky) | ||
1413 | #![feature(lang_items, box_syntax, start, libc, core_intrinsics, rustc_private)] | ||
1414 | #![no_std] | ||
1415 | use core::intrinsics; | ||
1416 | use core::panic::PanicInfo; | ||
1417 | |||
1418 | extern crate libc; | ||
1419 | |||
1420 | #[lang = "owned_box"] | ||
1421 | pub struct Box<T>(*mut T); | ||
1422 | |||
1423 | #[lang = "exchange_malloc"] | ||
1424 | unsafe fn allocate(size: usize, _align: usize) -> *mut u8 { | ||
1425 | let p = libc::malloc(size as libc::size_t) as *mut u8; | ||
1426 | |||
1427 | // Check if `malloc` failed: | ||
1428 | if p as usize == 0 { | ||
1429 | intrinsics::abort(); | ||
1430 | } | ||
1431 | |||
1432 | p | ||
1433 | } | ||
1434 | |||
1435 | #[lang = "box_free"] | ||
1436 | unsafe fn box_free<T: ?Sized>(ptr: *mut T) { | ||
1437 | libc::free(ptr as *mut libc::c_void) | ||
1438 | } | ||
1439 | |||
1440 | #[start] | ||
1441 | fn main(_argc: isize, _argv: *const *const u8) -> isize { | ||
1442 | let _x = box 1; | ||
1443 | |||
1444 | 0 | ||
1445 | } | ||
1446 | |||
1447 | #[lang = "eh_personality"] extern fn rust_eh_personality() {} | ||
1448 | #[lang = "panic_impl"] extern fn rust_begin_panic(info: &PanicInfo) -> ! { unsafe { intrinsics::abort() } } | ||
1449 | #[no_mangle] pub extern fn rust_eh_register_frames () {} | ||
1450 | #[no_mangle] pub extern fn rust_eh_unregister_frames () {} | ||
1451 | ``` | ||
1452 | |||
1453 | Note the use of `abort`: the `exchange_malloc` lang item is assumed to | ||
1454 | return a valid pointer, and so needs to do the check internally. | ||
1455 | |||
1456 | Other features provided by lang items include: | ||
1457 | |||
1458 | - overloadable operators via traits: the traits corresponding to the | ||
1459 | `==`, `<`, dereferencing (`*`) and `+` (etc.) operators are all | ||
1460 | marked with lang items; those specific four are `eq`, `ord`, | ||
1461 | `deref`, and `add` respectively. | ||
1462 | - stack unwinding and general failure; the `eh_personality`, | ||
1463 | `panic` and `panic_bounds_check` lang items. | ||
1464 | - the traits in `std::marker` used to indicate types of | ||
1465 | various kinds; lang items `send`, `sync` and `copy`. | ||
1466 | - the marker types and variance indicators found in | ||
1467 | `std::marker`; lang items `covariant_type`, | ||
1468 | `contravariant_lifetime`, etc. | ||
1469 | |||
1470 | Lang items are loaded lazily by the compiler; e.g. if one never uses | ||
1471 | `Box` then there is no need to define functions for `exchange_malloc` | ||
1472 | and `box_free`. `rustc` will emit an error when an item is needed | ||
1473 | but not found in the current crate or any that it depends on. | ||
1474 | |||
1475 | Most lang items are defined by `libcore`, but if you're trying to build | ||
1476 | an executable without the standard library, you'll run into the need | ||
1477 | for lang items. The rest of this page focuses on this use-case, even though | ||
1478 | lang items are a bit broader than that. | ||
1479 | |||
1480 | ### Using libc | ||
1481 | |||
1482 | In order to build a `#[no_std]` executable we will need libc as a dependency. | ||
1483 | We can specify this using our `Cargo.toml` file: | ||
1484 | |||
1485 | ```toml | ||
1486 | [dependencies] | ||
1487 | libc = { version = "0.2.14", default-features = false } | ||
1488 | ``` | ||
1489 | |||
1490 | Note that the default features have been disabled. This is a critical step - | ||
1491 | **the default features of libc include the standard library and so must be | ||
1492 | disabled.** | ||
1493 | |||
1494 | ### Writing an executable without stdlib | ||
1495 | |||
1496 | Controlling the entry point is possible in two ways: the `#[start]` attribute, | ||
1497 | or overriding the default shim for the C `main` function with your own. | ||
1498 | |||
1499 | The function marked `#[start]` is passed the command line parameters | ||
1500 | in the same format as C: | ||
1501 | |||
1502 | ```rust,ignore (libc-is-finicky) | ||
1503 | #![feature(lang_items, core_intrinsics, rustc_private)] | ||
1504 | #![feature(start)] | ||
1505 | #![no_std] | ||
1506 | use core::intrinsics; | ||
1507 | use core::panic::PanicInfo; | ||
1508 | |||
1509 | // Pull in the system libc library for what crt0.o likely requires. | ||
1510 | extern crate libc; | ||
1511 | |||
1512 | // Entry point for this program. | ||
1513 | #[start] | ||
1514 | fn start(_argc: isize, _argv: *const *const u8) -> isize { | ||
1515 | 0 | ||
1516 | } | ||
1517 | |||
1518 | // These functions are used by the compiler, but not | ||
1519 | // for a bare-bones hello world. These are normally | ||
1520 | // provided by libstd. | ||
1521 | #[lang = "eh_personality"] | ||
1522 | #[no_mangle] | ||
1523 | pub extern fn rust_eh_personality() { | ||
1524 | } | ||
1525 | |||
1526 | #[lang = "panic_impl"] | ||
1527 | #[no_mangle] | ||
1528 | pub extern fn rust_begin_panic(info: &PanicInfo) -> ! { | ||
1529 | unsafe { intrinsics::abort() } | ||
1530 | } | ||
1531 | ``` | ||
1532 | |||
1533 | To override the compiler-inserted `main` shim, one has to disable it | ||
1534 | with `#![no_main]` and then create the appropriate symbol with the | ||
1535 | correct ABI and the correct name, which requires overriding the | ||
1536 | compiler's name mangling too: | ||
1537 | |||
1538 | ```rust,ignore (libc-is-finicky) | ||
1539 | #![feature(lang_items, core_intrinsics, rustc_private)] | ||
1540 | #![feature(start)] | ||
1541 | #![no_std] | ||
1542 | #![no_main] | ||
1543 | use core::intrinsics; | ||
1544 | use core::panic::PanicInfo; | ||
1545 | |||
1546 | // Pull in the system libc library for what crt0.o likely requires. | ||
1547 | extern crate libc; | ||
1548 | |||
1549 | // Entry point for this program. | ||
1550 | #[no_mangle] // ensure that this symbol is called `main` in the output | ||
1551 | pub extern fn main(_argc: i32, _argv: *const *const u8) -> i32 { | ||
1552 | 0 | ||
1553 | } | ||
1554 | |||
1555 | // These functions are used by the compiler, but not | ||
1556 | // for a bare-bones hello world. These are normally | ||
1557 | // provided by libstd. | ||
1558 | #[lang = "eh_personality"] | ||
1559 | #[no_mangle] | ||
1560 | pub extern fn rust_eh_personality() { | ||
1561 | } | ||
1562 | |||
1563 | #[lang = "panic_impl"] | ||
1564 | #[no_mangle] | ||
1565 | pub extern fn rust_begin_panic(info: &PanicInfo) -> ! { | ||
1566 | unsafe { intrinsics::abort() } | ||
1567 | } | ||
1568 | ``` | ||
1569 | |||
1570 | In many cases, you may need to manually link to the `compiler_builtins` crate | ||
1571 | when building a `no_std` binary. You may observe this via linker error messages | ||
1572 | such as "```undefined reference to `__rust_probestack'```". | ||
1573 | |||
1574 | ## More about the language items | ||
1575 | |||
1576 | The compiler currently makes a few assumptions about symbols which are | ||
1577 | available in the executable to call. Normally these functions are provided by | ||
1578 | the standard library, but without it you must define your own. These symbols | ||
1579 | are called "language items", and they each have an internal name, and then a | ||
1580 | signature that an implementation must conform to. | ||
1581 | |||
1582 | The first of these functions, `rust_eh_personality`, is used by the failure | ||
1583 | mechanisms of the compiler. This is often mapped to GCC's personality function | ||
1584 | (see the [libstd implementation][unwind] for more information), but crates | ||
1585 | which do not trigger a panic can be assured that this function is never | ||
1586 | called. The language item's name is `eh_personality`. | ||
1587 | |||
1588 | [unwind]: https://github.com/rust-lang/rust/blob/master/src/libpanic_unwind/gcc.rs | ||
1589 | |||
1590 | The second function, `rust_begin_panic`, is also used by the failure mechanisms of the | ||
1591 | compiler. When a panic happens, this controls the message that's displayed on | ||
1592 | the screen. While the language item's name is `panic_impl`, the symbol name is | ||
1593 | `rust_begin_panic`. | ||
1594 | |||
1595 | Finally, a `eh_catch_typeinfo` static is needed for certain targets which | ||
1596 | implement Rust panics on top of C++ exceptions. | ||
1597 | |||
1598 | ## List of all language items | ||
1599 | |||
1600 | This is a list of all language items in Rust along with where they are located in | ||
1601 | the source code. | ||
1602 | |||
1603 | - Primitives | ||
1604 | - `i8`: `libcore/num/mod.rs` | ||
1605 | - `i16`: `libcore/num/mod.rs` | ||
1606 | - `i32`: `libcore/num/mod.rs` | ||
1607 | - `i64`: `libcore/num/mod.rs` | ||
1608 | - `i128`: `libcore/num/mod.rs` | ||
1609 | - `isize`: `libcore/num/mod.rs` | ||
1610 | - `u8`: `libcore/num/mod.rs` | ||
1611 | - `u16`: `libcore/num/mod.rs` | ||
1612 | - `u32`: `libcore/num/mod.rs` | ||
1613 | - `u64`: `libcore/num/mod.rs` | ||
1614 | - `u128`: `libcore/num/mod.rs` | ||
1615 | - `usize`: `libcore/num/mod.rs` | ||
1616 | - `f32`: `libstd/f32.rs` | ||
1617 | - `f64`: `libstd/f64.rs` | ||
1618 | - `char`: `libcore/char.rs` | ||
1619 | - `slice`: `liballoc/slice.rs` | ||
1620 | - `str`: `liballoc/str.rs` | ||
1621 | - `const_ptr`: `libcore/ptr.rs` | ||
1622 | - `mut_ptr`: `libcore/ptr.rs` | ||
1623 | - `unsafe_cell`: `libcore/cell.rs` | ||
1624 | - Runtime | ||
1625 | - `start`: `libstd/rt.rs` | ||
1626 | - `eh_personality`: `libpanic_unwind/emcc.rs` (EMCC) | ||
1627 | - `eh_personality`: `libpanic_unwind/gcc.rs` (GNU) | ||
1628 | - `eh_personality`: `libpanic_unwind/seh.rs` (SEH) | ||
1629 | - `eh_catch_typeinfo`: `libpanic_unwind/emcc.rs` (EMCC) | ||
1630 | - `panic`: `libcore/panicking.rs` | ||
1631 | - `panic_bounds_check`: `libcore/panicking.rs` | ||
1632 | - `panic_impl`: `libcore/panicking.rs` | ||
1633 | - `panic_impl`: `libstd/panicking.rs` | ||
1634 | - Allocations | ||
1635 | - `owned_box`: `liballoc/boxed.rs` | ||
1636 | - `exchange_malloc`: `liballoc/heap.rs` | ||
1637 | - `box_free`: `liballoc/heap.rs` | ||
1638 | - Operands | ||
1639 | - `not`: `libcore/ops/bit.rs` | ||
1640 | - `bitand`: `libcore/ops/bit.rs` | ||
1641 | - `bitor`: `libcore/ops/bit.rs` | ||
1642 | - `bitxor`: `libcore/ops/bit.rs` | ||
1643 | - `shl`: `libcore/ops/bit.rs` | ||
1644 | - `shr`: `libcore/ops/bit.rs` | ||
1645 | - `bitand_assign`: `libcore/ops/bit.rs` | ||
1646 | - `bitor_assign`: `libcore/ops/bit.rs` | ||
1647 | - `bitxor_assign`: `libcore/ops/bit.rs` | ||
1648 | - `shl_assign`: `libcore/ops/bit.rs` | ||
1649 | - `shr_assign`: `libcore/ops/bit.rs` | ||
1650 | - `deref`: `libcore/ops/deref.rs` | ||
1651 | - `deref_mut`: `libcore/ops/deref.rs` | ||
1652 | - `index`: `libcore/ops/index.rs` | ||
1653 | - `index_mut`: `libcore/ops/index.rs` | ||
1654 | - `add`: `libcore/ops/arith.rs` | ||
1655 | - `sub`: `libcore/ops/arith.rs` | ||
1656 | - `mul`: `libcore/ops/arith.rs` | ||
1657 | - `div`: `libcore/ops/arith.rs` | ||
1658 | - `rem`: `libcore/ops/arith.rs` | ||
1659 | - `neg`: `libcore/ops/arith.rs` | ||
1660 | - `add_assign`: `libcore/ops/arith.rs` | ||
1661 | - `sub_assign`: `libcore/ops/arith.rs` | ||
1662 | - `mul_assign`: `libcore/ops/arith.rs` | ||
1663 | - `div_assign`: `libcore/ops/arith.rs` | ||
1664 | - `rem_assign`: `libcore/ops/arith.rs` | ||
1665 | - `eq`: `libcore/cmp.rs` | ||
1666 | - `ord`: `libcore/cmp.rs` | ||
1667 | - Functions | ||
1668 | - `fn`: `libcore/ops/function.rs` | ||
1669 | - `fn_mut`: `libcore/ops/function.rs` | ||
1670 | - `fn_once`: `libcore/ops/function.rs` | ||
1671 | - `generator_state`: `libcore/ops/generator.rs` | ||
1672 | - `generator`: `libcore/ops/generator.rs` | ||
1673 | - Other | ||
1674 | - `coerce_unsized`: `libcore/ops/unsize.rs` | ||
1675 | - `drop`: `libcore/ops/drop.rs` | ||
1676 | - `drop_in_place`: `libcore/ptr.rs` | ||
1677 | - `clone`: `libcore/clone.rs` | ||
1678 | - `copy`: `libcore/marker.rs` | ||
1679 | - `send`: `libcore/marker.rs` | ||
1680 | - `sized`: `libcore/marker.rs` | ||
1681 | - `unsize`: `libcore/marker.rs` | ||
1682 | - `sync`: `libcore/marker.rs` | ||
1683 | - `phantom_data`: `libcore/marker.rs` | ||
1684 | - `discriminant_kind`: `libcore/marker.rs` | ||
1685 | - `freeze`: `libcore/marker.rs` | ||
1686 | - `debug_trait`: `libcore/fmt/mod.rs` | ||
1687 | - `non_zero`: `libcore/nonzero.rs` | ||
1688 | - `arc`: `liballoc/sync.rs` | ||
1689 | - `rc`: `liballoc/rc.rs` | ||
1690 | "##, | ||
1691 | }, | ||
1692 | LintCompletion { | ||
1693 | label: "doc_spotlight", | ||
1694 | description: r##"# `doc_spotlight` | ||
1695 | |||
1696 | The tracking issue for this feature is: [#45040] | ||
1697 | |||
1698 | The `doc_spotlight` feature allows the use of the `spotlight` parameter to the `#[doc]` attribute, | ||
1699 | to "spotlight" a specific trait on the return values of functions. Adding a `#[doc(spotlight)]` | ||
1700 | attribute to a trait definition will make rustdoc print extra information for functions which return | ||
1701 | a type that implements that trait. For example, this attribute is applied to the `Iterator`, | ||
1702 | `io::Read`, `io::Write`, and `Future` traits in the standard library. | ||
1703 | |||
1704 | You can do this on your own traits, like this: | ||
1705 | |||
1706 | ``` | ||
1707 | #![feature(doc_spotlight)] | ||
1708 | |||
1709 | #[doc(spotlight)] | ||
1710 | pub trait MyTrait {} | ||
1711 | |||
1712 | pub struct MyStruct; | ||
1713 | impl MyTrait for MyStruct {} | ||
1714 | |||
1715 | /// The docs for this function will have an extra line about `MyStruct` implementing `MyTrait`, | ||
1716 | /// without having to write that yourself! | ||
1717 | pub fn my_fn() -> MyStruct { MyStruct } | ||
1718 | ``` | ||
1719 | |||
1720 | This feature was originally implemented in PR [#45039]. | ||
1721 | |||
1722 | [#45040]: https://github.com/rust-lang/rust/issues/45040 | ||
1723 | [#45039]: https://github.com/rust-lang/rust/pull/45039 | ||
1724 | "##, | ||
1725 | }, | ||
1726 | LintCompletion { | ||
1727 | label: "c_variadic", | ||
1728 | description: r##"# `c_variadic` | ||
1729 | |||
1730 | The tracking issue for this feature is: [#44930] | ||
1731 | |||
1732 | [#44930]: https://github.com/rust-lang/rust/issues/44930 | ||
1733 | |||
1734 | ------------------------ | ||
1735 | |||
1736 | The `c_variadic` language feature enables C-variadic functions to be | ||
1737 | defined in Rust. The may be called both from within Rust and via FFI. | ||
1738 | |||
1739 | ## Examples | ||
1740 | |||
1741 | ```rust | ||
1742 | #![feature(c_variadic)] | ||
1743 | |||
1744 | pub unsafe extern "C" fn add(n: usize, mut args: ...) -> usize { | ||
1745 | let mut sum = 0; | ||
1746 | for _ in 0..n { | ||
1747 | sum += args.arg::<usize>(); | ||
1748 | } | ||
1749 | sum | ||
1750 | } | ||
1751 | ``` | ||
1752 | "##, | ||
1753 | }, | ||
1754 | LintCompletion { | ||
1755 | label: "intra_doc_pointers", | ||
1756 | description: r##"# `intra-doc-pointers` | ||
1757 | |||
1758 | The tracking issue for this feature is: [#80896] | ||
1759 | |||
1760 | [#80896]: https://github.com/rust-lang/rust/issues/80896 | ||
1761 | |||
1762 | ------------------------ | ||
1763 | |||
1764 | Rustdoc does not currently allow disambiguating between `*const` and `*mut`, and | ||
1765 | raw pointers in intra-doc links are unstable until it does. | ||
1766 | |||
1767 | ```rust | ||
1768 | #![feature(intra_doc_pointers)] | ||
1769 | //! [pointer::add] | ||
1770 | ``` | ||
1771 | "##, | ||
1772 | }, | ||
1773 | LintCompletion { | ||
1774 | label: "box_syntax", | ||
1775 | description: r##"# `box_syntax` | ||
1776 | |||
1777 | The tracking issue for this feature is: [#49733] | ||
1778 | |||
1779 | [#49733]: https://github.com/rust-lang/rust/issues/49733 | ||
1780 | |||
1781 | See also [`box_patterns`](box-patterns.md) | ||
1782 | |||
1783 | ------------------------ | ||
1784 | |||
1785 | Currently the only stable way to create a `Box` is via the `Box::new` method. | ||
1786 | Also it is not possible in stable Rust to destructure a `Box` in a match | ||
1787 | pattern. The unstable `box` keyword can be used to create a `Box`. An example | ||
1788 | usage would be: | ||
1789 | |||
1790 | ```rust | ||
1791 | #![feature(box_syntax)] | ||
1792 | |||
1793 | fn main() { | ||
1794 | let b = box 5; | ||
1795 | } | ||
1796 | ``` | ||
1797 | "##, | ||
1798 | }, | ||
1799 | LintCompletion { | ||
1800 | label: "unsized_locals", | ||
1801 | description: r##"# `unsized_locals` | ||
1802 | |||
1803 | The tracking issue for this feature is: [#48055] | ||
1804 | |||
1805 | [#48055]: https://github.com/rust-lang/rust/issues/48055 | ||
1806 | |||
1807 | ------------------------ | ||
1808 | |||
1809 | This implements [RFC1909]. When turned on, you can have unsized arguments and locals: | ||
1810 | |||
1811 | [RFC1909]: https://github.com/rust-lang/rfcs/blob/master/text/1909-unsized-rvalues.md | ||
1812 | |||
1813 | ```rust | ||
1814 | #![allow(incomplete_features)] | ||
1815 | #![feature(unsized_locals, unsized_fn_params)] | ||
1816 | |||
1817 | use std::any::Any; | ||
1818 | |||
1819 | fn main() { | ||
1820 | let x: Box<dyn Any> = Box::new(42); | ||
1821 | let x: dyn Any = *x; | ||
1822 | // ^ unsized local variable | ||
1823 | // ^^ unsized temporary | ||
1824 | foo(x); | ||
1825 | } | ||
1826 | |||
1827 | fn foo(_: dyn Any) {} | ||
1828 | // ^^^^^^ unsized argument | ||
1829 | ``` | ||
1830 | |||
1831 | The RFC still forbids the following unsized expressions: | ||
1832 | |||
1833 | ```rust,compile_fail | ||
1834 | #![feature(unsized_locals)] | ||
1835 | |||
1836 | use std::any::Any; | ||
1837 | |||
1838 | struct MyStruct<T: ?Sized> { | ||
1839 | content: T, | ||
1840 | } | ||
1841 | |||
1842 | struct MyTupleStruct<T: ?Sized>(T); | ||
1843 | |||
1844 | fn answer() -> Box<dyn Any> { | ||
1845 | Box::new(42) | ||
1846 | } | ||
1847 | |||
1848 | fn main() { | ||
1849 | // You CANNOT have unsized statics. | ||
1850 | static X: dyn Any = *answer(); // ERROR | ||
1851 | const Y: dyn Any = *answer(); // ERROR | ||
1852 | |||
1853 | // You CANNOT have struct initialized unsized. | ||
1854 | MyStruct { content: *answer() }; // ERROR | ||
1855 | MyTupleStruct(*answer()); // ERROR | ||
1856 | (42, *answer()); // ERROR | ||
1857 | |||
1858 | // You CANNOT have unsized return types. | ||
1859 | fn my_function() -> dyn Any { *answer() } // ERROR | ||
1860 | |||
1861 | // You CAN have unsized local variables... | ||
1862 | let mut x: dyn Any = *answer(); // OK | ||
1863 | // ...but you CANNOT reassign to them. | ||
1864 | x = *answer(); // ERROR | ||
1865 | |||
1866 | // You CANNOT even initialize them separately. | ||
1867 | let y: dyn Any; // OK | ||
1868 | y = *answer(); // ERROR | ||
1869 | |||
1870 | // Not mentioned in the RFC, but by-move captured variables are also Sized. | ||
1871 | let x: dyn Any = *answer(); | ||
1872 | (move || { // ERROR | ||
1873 | let y = x; | ||
1874 | })(); | ||
1875 | |||
1876 | // You CAN create a closure with unsized arguments, | ||
1877 | // but you CANNOT call it. | ||
1878 | // This is an implementation detail and may be changed in the future. | ||
1879 | let f = |x: dyn Any| {}; | ||
1880 | f(*answer()); // ERROR | ||
1881 | } | ||
1882 | ``` | ||
1883 | |||
1884 | ## By-value trait objects | ||
1885 | |||
1886 | With this feature, you can have by-value `self` arguments without `Self: Sized` bounds. | ||
1887 | |||
1888 | ```rust | ||
1889 | #![feature(unsized_fn_params)] | ||
1890 | |||
1891 | trait Foo { | ||
1892 | fn foo(self) {} | ||
1893 | } | ||
1894 | |||
1895 | impl<T: ?Sized> Foo for T {} | ||
1896 | |||
1897 | fn main() { | ||
1898 | let slice: Box<[i32]> = Box::new([1, 2, 3]); | ||
1899 | <[i32] as Foo>::foo(*slice); | ||
1900 | } | ||
1901 | ``` | ||
1902 | |||
1903 | And `Foo` will also be object-safe. | ||
1904 | |||
1905 | ```rust | ||
1906 | #![feature(unsized_fn_params)] | ||
1907 | |||
1908 | trait Foo { | ||
1909 | fn foo(self) {} | ||
1910 | } | ||
1911 | |||
1912 | impl<T: ?Sized> Foo for T {} | ||
1913 | |||
1914 | fn main () { | ||
1915 | let slice: Box<dyn Foo> = Box::new([1, 2, 3]); | ||
1916 | // doesn't compile yet | ||
1917 | <dyn Foo as Foo>::foo(*slice); | ||
1918 | } | ||
1919 | ``` | ||
1920 | |||
1921 | One of the objectives of this feature is to allow `Box<dyn FnOnce>`. | ||
1922 | |||
1923 | ## Variable length arrays | ||
1924 | |||
1925 | The RFC also describes an extension to the array literal syntax: `[e; dyn n]`. In the syntax, `n` isn't necessarily a constant expression. The array is dynamically allocated on the stack and has the type of `[T]`, instead of `[T; n]`. | ||
1926 | |||
1927 | ```rust,ignore (not-yet-implemented) | ||
1928 | #![feature(unsized_locals)] | ||
1929 | |||
1930 | fn mergesort<T: Ord>(a: &mut [T]) { | ||
1931 | let mut tmp = [T; dyn a.len()]; | ||
1932 | // ... | ||
1933 | } | ||
1934 | |||
1935 | fn main() { | ||
1936 | let mut a = [3, 1, 5, 6]; | ||
1937 | mergesort(&mut a); | ||
1938 | assert_eq!(a, [1, 3, 5, 6]); | ||
1939 | } | ||
1940 | ``` | ||
1941 | |||
1942 | VLAs are not implemented yet. The syntax isn't final, either. We may need an alternative syntax for Rust 2015 because, in Rust 2015, expressions like `[e; dyn(1)]` would be ambiguous. One possible alternative proposed in the RFC is `[e; n]`: if `n` captures one or more local variables, then it is considered as `[e; dyn n]`. | ||
1943 | |||
1944 | ## Advisory on stack usage | ||
1945 | |||
1946 | It's advised not to casually use the `#![feature(unsized_locals)]` feature. Typical use-cases are: | ||
1947 | |||
1948 | - When you need a by-value trait objects. | ||
1949 | - When you really need a fast allocation of small temporary arrays. | ||
1950 | |||
1951 | Another pitfall is repetitive allocation and temporaries. Currently the compiler simply extends the stack frame every time it encounters an unsized assignment. So for example, the code | ||
1952 | |||
1953 | ```rust | ||
1954 | #![feature(unsized_locals)] | ||
1955 | |||
1956 | fn main() { | ||
1957 | let x: Box<[i32]> = Box::new([1, 2, 3, 4, 5]); | ||
1958 | let _x = {{{{{{{{{{*x}}}}}}}}}}; | ||
1959 | } | ||
1960 | ``` | ||
1961 | |||
1962 | and the code | ||
1963 | |||
1964 | ```rust | ||
1965 | #![feature(unsized_locals)] | ||
1966 | |||
1967 | fn main() { | ||
1968 | for _ in 0..10 { | ||
1969 | let x: Box<[i32]> = Box::new([1, 2, 3, 4, 5]); | ||
1970 | let _x = *x; | ||
1971 | } | ||
1972 | } | ||
1973 | ``` | ||
1974 | |||
1975 | will unnecessarily extend the stack frame. | ||
1976 | "##, | ||
1977 | }, | ||
1978 | LintCompletion { | ||
1979 | label: "arbitrary_enum_discriminant", | ||
1980 | description: r##"# `arbitrary_enum_discriminant` | ||
1981 | |||
1982 | The tracking issue for this feature is: [#60553] | ||
1983 | |||
1984 | [#60553]: https://github.com/rust-lang/rust/issues/60553 | ||
1985 | |||
1986 | ------------------------ | ||
1987 | |||
1988 | The `arbitrary_enum_discriminant` feature permits tuple-like and | ||
1989 | struct-like enum variants with `#[repr(<int-type>)]` to have explicit discriminants. | ||
1990 | |||
1991 | ## Examples | ||
1992 | |||
1993 | ```rust | ||
1994 | #![feature(arbitrary_enum_discriminant)] | ||
1995 | |||
1996 | #[allow(dead_code)] | ||
1997 | #[repr(u8)] | ||
1998 | enum Enum { | ||
1999 | Unit = 3, | ||
2000 | Tuple(u16) = 2, | ||
2001 | Struct { | ||
2002 | a: u8, | ||
2003 | b: u16, | ||
2004 | } = 1, | ||
2005 | } | ||
2006 | |||
2007 | impl Enum { | ||
2008 | fn tag(&self) -> u8 { | ||
2009 | unsafe { *(self as *const Self as *const u8) } | ||
2010 | } | ||
2011 | } | ||
2012 | |||
2013 | assert_eq!(3, Enum::Unit.tag()); | ||
2014 | assert_eq!(2, Enum::Tuple(5).tag()); | ||
2015 | assert_eq!(1, Enum::Struct{a: 7, b: 11}.tag()); | ||
2016 | ``` | ||
2017 | "##, | ||
2018 | }, | ||
2019 | LintCompletion { | ||
2020 | label: "unboxed_closures", | ||
2021 | description: r##"# `unboxed_closures` | ||
2022 | |||
2023 | The tracking issue for this feature is [#29625] | ||
2024 | |||
2025 | See Also: [`fn_traits`](../library-features/fn-traits.md) | ||
2026 | |||
2027 | [#29625]: https://github.com/rust-lang/rust/issues/29625 | ||
2028 | |||
2029 | ---- | ||
2030 | |||
2031 | The `unboxed_closures` feature allows you to write functions using the `"rust-call"` ABI, | ||
2032 | required for implementing the [`Fn*`] family of traits. `"rust-call"` functions must have | ||
2033 | exactly one (non self) argument, a tuple representing the argument list. | ||
2034 | |||
2035 | [`Fn*`]: https://doc.rust-lang.org/std/ops/trait.Fn.html | ||
2036 | |||
2037 | ```rust | ||
2038 | #![feature(unboxed_closures)] | ||
2039 | |||
2040 | extern "rust-call" fn add_args(args: (u32, u32)) -> u32 { | ||
2041 | args.0 + args.1 | ||
2042 | } | ||
2043 | |||
2044 | fn main() {} | ||
2045 | ``` | ||
2046 | "##, | ||
2047 | }, | ||
2048 | LintCompletion { | ||
2049 | label: "custom_test_frameworks", | ||
2050 | description: r##"# `custom_test_frameworks` | ||
2051 | |||
2052 | The tracking issue for this feature is: [#50297] | ||
2053 | |||
2054 | [#50297]: https://github.com/rust-lang/rust/issues/50297 | ||
2055 | |||
2056 | ------------------------ | ||
2057 | |||
2058 | The `custom_test_frameworks` feature allows the use of `#[test_case]` and `#![test_runner]`. | ||
2059 | Any function, const, or static can be annotated with `#[test_case]` causing it to be aggregated (like `#[test]`) | ||
2060 | and be passed to the test runner determined by the `#![test_runner]` crate attribute. | ||
2061 | |||
2062 | ```rust | ||
2063 | #![feature(custom_test_frameworks)] | ||
2064 | #![test_runner(my_runner)] | ||
2065 | |||
2066 | fn my_runner(tests: &[&i32]) { | ||
2067 | for t in tests { | ||
2068 | if **t == 0 { | ||
2069 | println!("PASSED"); | ||
2070 | } else { | ||
2071 | println!("FAILED"); | ||
2072 | } | ||
2073 | } | ||
2074 | } | ||
2075 | |||
2076 | #[test_case] | ||
2077 | const WILL_PASS: i32 = 0; | ||
2078 | |||
2079 | #[test_case] | ||
2080 | const WILL_FAIL: i32 = 4; | ||
2081 | ``` | ||
2082 | "##, | ||
2083 | }, | ||
2084 | LintCompletion { | ||
2085 | label: "abi_msp430_interrupt", | ||
2086 | description: r##"# `abi_msp430_interrupt` | ||
2087 | |||
2088 | The tracking issue for this feature is: [#38487] | ||
2089 | |||
2090 | [#38487]: https://github.com/rust-lang/rust/issues/38487 | ||
2091 | |||
2092 | ------------------------ | ||
2093 | |||
2094 | In the MSP430 architecture, interrupt handlers have a special calling | ||
2095 | convention. You can use the `"msp430-interrupt"` ABI to make the compiler apply | ||
2096 | the right calling convention to the interrupt handlers you define. | ||
2097 | |||
2098 | <!-- NOTE(ignore) this example is specific to the msp430 target --> | ||
2099 | |||
2100 | ``` rust,ignore | ||
2101 | #![feature(abi_msp430_interrupt)] | ||
2102 | #![no_std] | ||
2103 | |||
2104 | // Place the interrupt handler at the appropriate memory address | ||
2105 | // (Alternatively, you can use `#[used]` and remove `pub` and `#[no_mangle]`) | ||
2106 | #[link_section = "__interrupt_vector_10"] | ||
2107 | #[no_mangle] | ||
2108 | pub static TIM0_VECTOR: extern "msp430-interrupt" fn() = tim0; | ||
2109 | |||
2110 | // The interrupt handler | ||
2111 | extern "msp430-interrupt" fn tim0() { | ||
2112 | // .. | ||
2113 | } | ||
2114 | ``` | ||
2115 | |||
2116 | ``` text | ||
2117 | $ msp430-elf-objdump -CD ./target/msp430/release/app | ||
2118 | Disassembly of section __interrupt_vector_10: | ||
2119 | |||
2120 | 0000fff2 <TIM0_VECTOR>: | ||
2121 | fff2: 00 c0 interrupt service routine at 0xc000 | ||
2122 | |||
2123 | Disassembly of section .text: | ||
2124 | |||
2125 | 0000c000 <int::tim0>: | ||
2126 | c000: 00 13 reti | ||
2127 | ``` | ||
2128 | "##, | ||
2129 | }, | ||
2130 | LintCompletion { | ||
2131 | label: "impl_trait_in_bindings", | ||
2132 | description: r##"# `impl_trait_in_bindings` | ||
2133 | |||
2134 | The tracking issue for this feature is: [#63065] | ||
2135 | |||
2136 | [#63065]: https://github.com/rust-lang/rust/issues/63065 | ||
2137 | |||
2138 | ------------------------ | ||
2139 | |||
2140 | The `impl_trait_in_bindings` feature gate lets you use `impl Trait` syntax in | ||
2141 | `let`, `static`, and `const` bindings. | ||
2142 | |||
2143 | A simple example is: | ||
2144 | |||
2145 | ```rust | ||
2146 | #![feature(impl_trait_in_bindings)] | ||
2147 | |||
2148 | use std::fmt::Debug; | ||
2149 | |||
2150 | fn main() { | ||
2151 | let a: impl Debug + Clone = 42; | ||
2152 | let b = a.clone(); | ||
2153 | println!("{:?}", b); // prints `42` | ||
2154 | } | ||
2155 | ``` | ||
2156 | |||
2157 | Note however that because the types of `a` and `b` are opaque in the above | ||
2158 | example, calling inherent methods or methods outside of the specified traits | ||
2159 | (e.g., `a.abs()` or `b.abs()`) is not allowed, and yields an error. | ||
2160 | "##, | ||
2161 | }, | ||
2162 | LintCompletion { | ||
2163 | label: "cfg_version", | ||
2164 | description: r##"# `cfg_version` | ||
2165 | |||
2166 | The tracking issue for this feature is: [#64796] | ||
2167 | |||
2168 | [#64796]: https://github.com/rust-lang/rust/issues/64796 | ||
2169 | |||
2170 | ------------------------ | ||
2171 | |||
2172 | The `cfg_version` feature makes it possible to execute different code | ||
2173 | depending on the compiler version. | ||
2174 | |||
2175 | ## Examples | ||
2176 | |||
2177 | ```rust | ||
2178 | #![feature(cfg_version)] | ||
2179 | |||
2180 | #[cfg(version("1.42"))] | ||
2181 | fn a() { | ||
2182 | // ... | ||
2183 | } | ||
2184 | |||
2185 | #[cfg(not(version("1.42")))] | ||
2186 | fn a() { | ||
2187 | // ... | ||
2188 | } | ||
2189 | |||
2190 | fn b() { | ||
2191 | if cfg!(version("1.42")) { | ||
2192 | // ... | ||
2193 | } else { | ||
2194 | // ... | ||
2195 | } | ||
2196 | } | ||
2197 | ``` | ||
2198 | "##, | ||
2199 | }, | ||
2200 | LintCompletion { | ||
2201 | label: "link_cfg", | ||
2202 | description: r##"# `link_cfg` | ||
2203 | |||
2204 | This feature is internal to the Rust compiler and is not intended for general use. | ||
2205 | |||
2206 | ------------------------ | ||
2207 | "##, | ||
2208 | }, | ||
2209 | LintCompletion { | ||
2210 | label: "infer_static_outlives_requirements", | ||
2211 | description: r##"# `infer_static_outlives_requirements` | ||
2212 | |||
2213 | The tracking issue for this feature is: [#54185] | ||
2214 | |||
2215 | [#54185]: https://github.com/rust-lang/rust/issues/54185 | ||
2216 | |||
2217 | ------------------------ | ||
2218 | The `infer_static_outlives_requirements` feature indicates that certain | ||
2219 | `'static` outlives requirements can be inferred by the compiler rather than | ||
2220 | stating them explicitly. | ||
2221 | |||
2222 | Note: It is an accompanying feature to `infer_outlives_requirements`, | ||
2223 | which must be enabled to infer outlives requirements. | ||
2224 | |||
2225 | For example, currently generic struct definitions that contain | ||
2226 | references, require where-clauses of the form T: 'static. By using | ||
2227 | this feature the outlives predicates will be inferred, although | ||
2228 | they may still be written explicitly. | ||
2229 | |||
2230 | ```rust,ignore (pseudo-Rust) | ||
2231 | struct Foo<U> where U: 'static { // <-- currently required | ||
2232 | bar: Bar<U> | ||
2233 | } | ||
2234 | struct Bar<T: 'static> { | ||
2235 | x: T, | ||
2236 | } | ||
2237 | ``` | ||
2238 | |||
2239 | |||
2240 | ## Examples: | ||
2241 | |||
2242 | ```rust,ignore (pseudo-Rust) | ||
2243 | #![feature(infer_outlives_requirements)] | ||
2244 | #![feature(infer_static_outlives_requirements)] | ||
2245 | |||
2246 | #[rustc_outlives] | ||
2247 | // Implicitly infer U: 'static | ||
2248 | struct Foo<U> { | ||
2249 | bar: Bar<U> | ||
2250 | } | ||
2251 | struct Bar<T: 'static> { | ||
2252 | x: T, | ||
2253 | } | ||
2254 | ``` | ||
2255 | "##, | ||
2256 | }, | ||
2257 | LintCompletion { | ||
2258 | label: "marker_trait_attr", | ||
2259 | description: r##"# `marker_trait_attr` | ||
2260 | |||
2261 | The tracking issue for this feature is: [#29864] | ||
2262 | |||
2263 | [#29864]: https://github.com/rust-lang/rust/issues/29864 | ||
2264 | |||
2265 | ------------------------ | ||
2266 | |||
2267 | Normally, Rust keeps you from adding trait implementations that could | ||
2268 | overlap with each other, as it would be ambiguous which to use. This | ||
2269 | feature, however, carves out an exception to that rule: a trait can | ||
2270 | opt-in to having overlapping implementations, at the cost that those | ||
2271 | implementations are not allowed to override anything (and thus the | ||
2272 | trait itself cannot have any associated items, as they're pointless | ||
2273 | when they'd need to do the same thing for every type anyway). | ||
2274 | |||
2275 | ```rust | ||
2276 | #![feature(marker_trait_attr)] | ||
2277 | |||
2278 | #[marker] trait CheapToClone: Clone {} | ||
2279 | |||
2280 | impl<T: Copy> CheapToClone for T {} | ||
2281 | |||
2282 | // These could potentially overlap with the blanket implementation above, | ||
2283 | // so are only allowed because CheapToClone is a marker trait. | ||
2284 | impl<T: CheapToClone, U: CheapToClone> CheapToClone for (T, U) {} | ||
2285 | impl<T: CheapToClone> CheapToClone for std::ops::Range<T> {} | ||
2286 | |||
2287 | fn cheap_clone<T: CheapToClone>(t: T) -> T { | ||
2288 | t.clone() | ||
2289 | } | ||
2290 | ``` | ||
2291 | |||
2292 | This is expected to replace the unstable `overlapping_marker_traits` | ||
2293 | feature, which applied to all empty traits (without needing an opt-in). | ||
2294 | "##, | ||
2295 | }, | ||
2296 | LintCompletion { | ||
2297 | label: "doc_masked", | ||
2298 | description: r##"# `doc_masked` | ||
2299 | |||
2300 | The tracking issue for this feature is: [#44027] | ||
2301 | |||
2302 | ----- | ||
2303 | |||
2304 | The `doc_masked` feature allows a crate to exclude types from a given crate from appearing in lists | ||
2305 | of trait implementations. The specifics of the feature are as follows: | ||
2306 | |||
2307 | 1. When rustdoc encounters an `extern crate` statement annotated with a `#[doc(masked)]` attribute, | ||
2308 | it marks the crate as being masked. | ||
2309 | |||
2310 | 2. When listing traits a given type implements, rustdoc ensures that traits from masked crates are | ||
2311 | not emitted into the documentation. | ||
2312 | |||
2313 | 3. When listing types that implement a given trait, rustdoc ensures that types from masked crates | ||
2314 | are not emitted into the documentation. | ||
2315 | |||
2316 | This feature was introduced in PR [#44026] to ensure that compiler-internal and | ||
2317 | implementation-specific types and traits were not included in the standard library's documentation. | ||
2318 | Such types would introduce broken links into the documentation. | ||
2319 | |||
2320 | [#44026]: https://github.com/rust-lang/rust/pull/44026 | ||
2321 | [#44027]: https://github.com/rust-lang/rust/pull/44027 | ||
2322 | "##, | ||
2323 | }, | ||
2324 | LintCompletion { | ||
2325 | label: "abi_ptx", | ||
2326 | description: r##"# `abi_ptx` | ||
2327 | |||
2328 | The tracking issue for this feature is: [#38788] | ||
2329 | |||
2330 | [#38788]: https://github.com/rust-lang/rust/issues/38788 | ||
2331 | |||
2332 | ------------------------ | ||
2333 | |||
2334 | When emitting PTX code, all vanilla Rust functions (`fn`) get translated to | ||
2335 | "device" functions. These functions are *not* callable from the host via the | ||
2336 | CUDA API so a crate with only device functions is not too useful! | ||
2337 | |||
2338 | OTOH, "global" functions *can* be called by the host; you can think of them | ||
2339 | as the real public API of your crate. To produce a global function use the | ||
2340 | `"ptx-kernel"` ABI. | ||
2341 | |||
2342 | <!-- NOTE(ignore) this example is specific to the nvptx targets --> | ||
2343 | |||
2344 | ``` rust,ignore | ||
2345 | #![feature(abi_ptx)] | ||
2346 | #![no_std] | ||
2347 | |||
2348 | pub unsafe extern "ptx-kernel" fn global_function() { | ||
2349 | device_function(); | ||
2350 | } | ||
2351 | |||
2352 | pub fn device_function() { | ||
2353 | // .. | ||
2354 | } | ||
2355 | ``` | ||
2356 | |||
2357 | ``` text | ||
2358 | $ xargo rustc --target nvptx64-nvidia-cuda --release -- --emit=asm | ||
2359 | |||
2360 | $ cat $(find -name '*.s') | ||
2361 | // | ||
2362 | // Generated by LLVM NVPTX Back-End | ||
2363 | // | ||
2364 | |||
2365 | .version 3.2 | ||
2366 | .target sm_20 | ||
2367 | .address_size 64 | ||
2368 | |||
2369 | // .globl _ZN6kernel15global_function17h46111ebe6516b382E | ||
2370 | |||
2371 | .visible .entry _ZN6kernel15global_function17h46111ebe6516b382E() | ||
2372 | { | ||
2373 | |||
2374 | |||
2375 | ret; | ||
2376 | } | ||
2377 | |||
2378 | // .globl _ZN6kernel15device_function17hd6a0e4993bbf3f78E | ||
2379 | .visible .func _ZN6kernel15device_function17hd6a0e4993bbf3f78E() | ||
2380 | { | ||
2381 | |||
2382 | |||
2383 | ret; | ||
2384 | } | ||
2385 | ``` | ||
2386 | "##, | ||
2387 | }, | ||
2388 | LintCompletion { | ||
2389 | label: "profiler_runtime", | ||
2390 | description: r##"# `profiler_runtime` | ||
2391 | |||
2392 | The tracking issue for this feature is: [#42524](https://github.com/rust-lang/rust/issues/42524). | ||
2393 | |||
2394 | ------------------------ | ||
2395 | "##, | ||
2396 | }, | ||
2397 | LintCompletion { | ||
2398 | label: "crate_visibility_modifier", | ||
2399 | description: r##"# `crate_visibility_modifier` | ||
2400 | |||
2401 | The tracking issue for this feature is: [#53120] | ||
2402 | |||
2403 | [#53120]: https://github.com/rust-lang/rust/issues/53120 | ||
2404 | |||
2405 | ----- | ||
2406 | |||
2407 | The `crate_visibility_modifier` feature allows the `crate` keyword to be used | ||
2408 | as a visibility modifier synonymous to `pub(crate)`, indicating that a type | ||
2409 | (function, _&c._) is to be visible to the entire enclosing crate, but not to | ||
2410 | other crates. | ||
2411 | |||
2412 | ```rust | ||
2413 | #![feature(crate_visibility_modifier)] | ||
2414 | |||
2415 | crate struct Foo { | ||
2416 | bar: usize, | ||
2417 | } | ||
2418 | ``` | ||
2419 | "##, | ||
2420 | }, | ||
2421 | LintCompletion { | ||
2422 | label: "doc_cfg", | ||
2423 | description: r##"# `doc_cfg` | ||
2424 | |||
2425 | The tracking issue for this feature is: [#43781] | ||
2426 | |||
2427 | ------ | ||
2428 | |||
2429 | The `doc_cfg` feature allows an API be documented as only available in some specific platforms. | ||
2430 | This attribute has two effects: | ||
2431 | |||
2432 | 1. In the annotated item's documentation, there will be a message saying "This is supported on | ||
2433 | (platform) only". | ||
2434 | |||
2435 | 2. The item's doc-tests will only run on the specific platform. | ||
2436 | |||
2437 | In addition to allowing the use of the `#[doc(cfg)]` attribute, this feature enables the use of a | ||
2438 | special conditional compilation flag, `#[cfg(doc)]`, set whenever building documentation on your | ||
2439 | crate. | ||
2440 | |||
2441 | This feature was introduced as part of PR [#43348] to allow the platform-specific parts of the | ||
2442 | standard library be documented. | ||
2443 | |||
2444 | ```rust | ||
2445 | #![feature(doc_cfg)] | ||
2446 | |||
2447 | #[cfg(any(windows, doc))] | ||
2448 | #[doc(cfg(windows))] | ||
2449 | /// The application's icon in the notification area (a.k.a. system tray). | ||
2450 | /// | ||
2451 | /// # Examples | ||
2452 | /// | ||
2453 | /// ```no_run | ||
2454 | /// extern crate my_awesome_ui_library; | ||
2455 | /// use my_awesome_ui_library::current_app; | ||
2456 | /// use my_awesome_ui_library::windows::notification; | ||
2457 | /// | ||
2458 | /// let icon = current_app().get::<notification::Icon>(); | ||
2459 | /// icon.show(); | ||
2460 | /// icon.show_message("Hello"); | ||
2461 | /// ``` | ||
2462 | pub struct Icon { | ||
2463 | // ... | ||
2464 | } | ||
2465 | ``` | ||
2466 | |||
2467 | [#43781]: https://github.com/rust-lang/rust/issues/43781 | ||
2468 | [#43348]: https://github.com/rust-lang/rust/issues/43348 | ||
2469 | "##, | ||
2470 | }, | ||
2471 | LintCompletion { | ||
2472 | label: "unsized_tuple_coercion", | ||
2473 | description: r##"# `unsized_tuple_coercion` | ||
2474 | |||
2475 | The tracking issue for this feature is: [#42877] | ||
2476 | |||
2477 | [#42877]: https://github.com/rust-lang/rust/issues/42877 | ||
2478 | |||
2479 | ------------------------ | ||
2480 | |||
2481 | This is a part of [RFC0401]. According to the RFC, there should be an implementation like this: | ||
2482 | |||
2483 | ```rust,ignore (partial-example) | ||
2484 | impl<..., T, U: ?Sized> Unsized<(..., U)> for (..., T) where T: Unsized<U> {} | ||
2485 | ``` | ||
2486 | |||
2487 | This implementation is currently gated behind `#[feature(unsized_tuple_coercion)]` to avoid insta-stability. Therefore you can use it like this: | ||
2488 | |||
2489 | ```rust | ||
2490 | #![feature(unsized_tuple_coercion)] | ||
2491 | |||
2492 | fn main() { | ||
2493 | let x : ([i32; 3], [i32; 3]) = ([1, 2, 3], [4, 5, 6]); | ||
2494 | let y : &([i32; 3], [i32]) = &x; | ||
2495 | assert_eq!(y.1[0], 4); | ||
2496 | } | ||
2497 | ``` | ||
2498 | |||
2499 | [RFC0401]: https://github.com/rust-lang/rfcs/blob/master/text/0401-coercions.md | ||
2500 | "##, | ||
2501 | }, | ||
2502 | LintCompletion { | ||
2503 | label: "no_sanitize", | ||
2504 | description: r##"# `no_sanitize` | ||
2505 | |||
2506 | The tracking issue for this feature is: [#39699] | ||
2507 | |||
2508 | [#39699]: https://github.com/rust-lang/rust/issues/39699 | ||
2509 | |||
2510 | ------------------------ | ||
2511 | |||
2512 | The `no_sanitize` attribute can be used to selectively disable sanitizer | ||
2513 | instrumentation in an annotated function. This might be useful to: avoid | ||
2514 | instrumentation overhead in a performance critical function, or avoid | ||
2515 | instrumenting code that contains constructs unsupported by given sanitizer. | ||
2516 | |||
2517 | The precise effect of this annotation depends on particular sanitizer in use. | ||
2518 | For example, with `no_sanitize(thread)`, the thread sanitizer will no longer | ||
2519 | instrument non-atomic store / load operations, but it will instrument atomic | ||
2520 | operations to avoid reporting false positives and provide meaning full stack | ||
2521 | traces. | ||
2522 | |||
2523 | ## Examples | ||
2524 | |||
2525 | ``` rust | ||
2526 | #![feature(no_sanitize)] | ||
2527 | |||
2528 | #[no_sanitize(address)] | ||
2529 | fn foo() { | ||
2530 | // ... | ||
2531 | } | ||
2532 | ``` | ||
2533 | "##, | ||
2534 | }, | ||
2535 | LintCompletion { | ||
2536 | label: "try_blocks", | ||
2537 | description: r##"# `try_blocks` | ||
2538 | |||
2539 | The tracking issue for this feature is: [#31436] | ||
2540 | |||
2541 | [#31436]: https://github.com/rust-lang/rust/issues/31436 | ||
2542 | |||
2543 | ------------------------ | ||
2544 | |||
2545 | The `try_blocks` feature adds support for `try` blocks. A `try` | ||
2546 | block creates a new scope one can use the `?` operator in. | ||
2547 | |||
2548 | ```rust,edition2018 | ||
2549 | #![feature(try_blocks)] | ||
2550 | |||
2551 | use std::num::ParseIntError; | ||
2552 | |||
2553 | let result: Result<i32, ParseIntError> = try { | ||
2554 | "1".parse::<i32>()? | ||
2555 | + "2".parse::<i32>()? | ||
2556 | + "3".parse::<i32>()? | ||
2557 | }; | ||
2558 | assert_eq!(result, Ok(6)); | ||
2559 | |||
2560 | let result: Result<i32, ParseIntError> = try { | ||
2561 | "1".parse::<i32>()? | ||
2562 | + "foo".parse::<i32>()? | ||
2563 | + "3".parse::<i32>()? | ||
2564 | }; | ||
2565 | assert!(result.is_err()); | ||
2566 | ``` | ||
2567 | "##, | ||
2568 | }, | ||
2569 | LintCompletion { | ||
2570 | label: "transparent_unions", | ||
2571 | description: r##"# `transparent_unions` | ||
2572 | |||
2573 | The tracking issue for this feature is [#60405] | ||
2574 | |||
2575 | [#60405]: https://github.com/rust-lang/rust/issues/60405 | ||
2576 | |||
2577 | ---- | ||
2578 | |||
2579 | The `transparent_unions` feature allows you mark `union`s as | ||
2580 | `#[repr(transparent)]`. A `union` may be `#[repr(transparent)]` in exactly the | ||
2581 | same conditions in which a `struct` may be `#[repr(transparent)]` (generally, | ||
2582 | this means the `union` must have exactly one non-zero-sized field). Some | ||
2583 | concrete illustrations follow. | ||
2584 | |||
2585 | ```rust | ||
2586 | #![feature(transparent_unions)] | ||
2587 | |||
2588 | // This union has the same representation as `f32`. | ||
2589 | #[repr(transparent)] | ||
2590 | union SingleFieldUnion { | ||
2591 | field: f32, | ||
2592 | } | ||
2593 | |||
2594 | // This union has the same representation as `usize`. | ||
2595 | #[repr(transparent)] | ||
2596 | union MultiFieldUnion { | ||
2597 | field: usize, | ||
2598 | nothing: (), | ||
2599 | } | ||
2600 | ``` | ||
2601 | |||
2602 | For consistency with transparent `struct`s, `union`s must have exactly one | ||
2603 | non-zero-sized field. If all fields are zero-sized, the `union` must not be | ||
2604 | `#[repr(transparent)]`: | ||
2605 | |||
2606 | ```rust | ||
2607 | #![feature(transparent_unions)] | ||
2608 | |||
2609 | // This (non-transparent) union is already valid in stable Rust: | ||
2610 | pub union GoodUnion { | ||
2611 | pub nothing: (), | ||
2612 | } | ||
2613 | |||
2614 | // Error: transparent union needs exactly one non-zero-sized field, but has 0 | ||
2615 | // #[repr(transparent)] | ||
2616 | // pub union BadUnion { | ||
2617 | // pub nothing: (), | ||
2618 | // } | ||
2619 | ``` | ||
2620 | |||
2621 | The one exception is if the `union` is generic over `T` and has a field of type | ||
2622 | `T`, it may be `#[repr(transparent)]` even if `T` is a zero-sized type: | ||
2623 | |||
2624 | ```rust | ||
2625 | #![feature(transparent_unions)] | ||
2626 | |||
2627 | // This union has the same representation as `T`. | ||
2628 | #[repr(transparent)] | ||
2629 | pub union GenericUnion<T: Copy> { // Unions with non-`Copy` fields are unstable. | ||
2630 | pub field: T, | ||
2631 | pub nothing: (), | ||
2632 | } | ||
2633 | |||
2634 | // This is okay even though `()` is a zero-sized type. | ||
2635 | pub const THIS_IS_OKAY: GenericUnion<()> = GenericUnion { field: () }; | ||
2636 | ``` | ||
2637 | |||
2638 | Like transarent `struct`s, a transparent `union` of type `U` has the same | ||
2639 | layout, size, and ABI as its single non-ZST field. If it is generic over a type | ||
2640 | `T`, and all its fields are ZSTs except for exactly one field of type `T`, then | ||
2641 | it has the same layout and ABI as `T` (even if `T` is a ZST when monomorphized). | ||
2642 | |||
2643 | Like transparent `struct`s, transparent `union`s are FFI-safe if and only if | ||
2644 | their underlying representation type is also FFI-safe. | ||
2645 | |||
2646 | A `union` may not be eligible for the same nonnull-style optimizations that a | ||
2647 | `struct` or `enum` (with the same fields) are eligible for. Adding | ||
2648 | `#[repr(transparent)]` to `union` does not change this. To give a more concrete | ||
2649 | example, it is unspecified whether `size_of::<T>()` is equal to | ||
2650 | `size_of::<Option<T>>()`, where `T` is a `union` (regardless of whether or not | ||
2651 | it is transparent). The Rust compiler is free to perform this optimization if | ||
2652 | possible, but is not required to, and different compiler versions may differ in | ||
2653 | their application of these optimizations. | ||
2654 | "##, | ||
2655 | }, | ||
2656 | LintCompletion { | ||
2657 | label: "const_eval_limit", | ||
2658 | description: r##"# `const_eval_limit` | ||
2659 | |||
2660 | The tracking issue for this feature is: [#67217] | ||
2661 | |||
2662 | [#67217]: https://github.com/rust-lang/rust/issues/67217 | ||
2663 | |||
2664 | The `const_eval_limit` allows someone to limit the evaluation steps the CTFE undertakes to evaluate a `const fn`. | ||
2665 | "##, | ||
2666 | }, | ||
2667 | LintCompletion { | ||
2668 | label: "link_args", | ||
2669 | description: r##"# `link_args` | ||
2670 | |||
2671 | The tracking issue for this feature is: [#29596] | ||
2672 | |||
2673 | [#29596]: https://github.com/rust-lang/rust/issues/29596 | ||
2674 | |||
2675 | ------------------------ | ||
2676 | |||
2677 | You can tell `rustc` how to customize linking, and that is via the `link_args` | ||
2678 | attribute. This attribute is applied to `extern` blocks and specifies raw flags | ||
2679 | which need to get passed to the linker when producing an artifact. An example | ||
2680 | usage would be: | ||
2681 | |||
2682 | ```rust,no_run | ||
2683 | #![feature(link_args)] | ||
2684 | |||
2685 | #[link_args = "-foo -bar -baz"] | ||
2686 | extern "C" {} | ||
2687 | # fn main() {} | ||
2688 | ``` | ||
2689 | |||
2690 | Note that this feature is currently hidden behind the `feature(link_args)` gate | ||
2691 | because this is not a sanctioned way of performing linking. Right now `rustc` | ||
2692 | shells out to the system linker (`gcc` on most systems, `link.exe` on MSVC), so | ||
2693 | it makes sense to provide extra command line arguments, but this will not | ||
2694 | always be the case. In the future `rustc` may use LLVM directly to link native | ||
2695 | libraries, in which case `link_args` will have no meaning. You can achieve the | ||
2696 | same effect as the `link_args` attribute with the `-C link-args` argument to | ||
2697 | `rustc`. | ||
2698 | |||
2699 | It is highly recommended to *not* use this attribute, and rather use the more | ||
2700 | formal `#[link(...)]` attribute on `extern` blocks instead. | ||
2701 | "##, | ||
2702 | }, | ||
2703 | LintCompletion { | ||
2704 | label: "internal_output_capture", | ||
2705 | description: r##"# `internal_output_capture` | ||
2706 | |||
2707 | This feature is internal to the Rust compiler and is not intended for general use. | ||
2708 | |||
2709 | ------------------------ | ||
2710 | "##, | ||
2711 | }, | ||
2712 | LintCompletion { | ||
2713 | label: "windows_handle", | ||
2714 | description: r##"# `windows_handle` | ||
2715 | |||
2716 | This feature is internal to the Rust compiler and is not intended for general use. | ||
2717 | |||
2718 | ------------------------ | ||
2719 | "##, | ||
2720 | }, | ||
2721 | LintCompletion { | ||
2722 | label: "asm", | ||
2723 | description: r##"# `asm` | ||
2724 | |||
2725 | The tracking issue for this feature is: [#72016] | ||
2726 | |||
2727 | [#72016]: https://github.com/rust-lang/rust/issues/72016 | ||
2728 | |||
2729 | ------------------------ | ||
2730 | |||
2731 | For extremely low-level manipulations and performance reasons, one | ||
2732 | might wish to control the CPU directly. Rust supports using inline | ||
2733 | assembly to do this via the `asm!` macro. | ||
2734 | |||
2735 | # Guide-level explanation | ||
2736 | [guide-level-explanation]: #guide-level-explanation | ||
2737 | |||
2738 | Rust provides support for inline assembly via the `asm!` macro. | ||
2739 | It can be used to embed handwritten assembly in the assembly output generated by the compiler. | ||
2740 | Generally this should not be necessary, but might be where the required performance or timing | ||
2741 | cannot be otherwise achieved. Accessing low level hardware primitives, e.g. in kernel code, may also demand this functionality. | ||
2742 | |||
2743 | > **Note**: the examples here are given in x86/x86-64 assembly, but other architectures are also supported. | ||
2744 | |||
2745 | Inline assembly is currently supported on the following architectures: | ||
2746 | - x86 and x86-64 | ||
2747 | - ARM | ||
2748 | - AArch64 | ||
2749 | - RISC-V | ||
2750 | - NVPTX | ||
2751 | - Hexagon | ||
2752 | - MIPS32r2 and MIPS64r2 | ||
2753 | - wasm32 | ||
2754 | |||
2755 | ## Basic usage | ||
2756 | |||
2757 | Let us start with the simplest possible example: | ||
2758 | |||
2759 | ```rust,allow_fail | ||
2760 | # #![feature(asm)] | ||
2761 | unsafe { | ||
2762 | asm!("nop"); | ||
2763 | } | ||
2764 | ``` | ||
2765 | |||
2766 | This will insert a NOP (no operation) instruction into the assembly generated by the compiler. | ||
2767 | Note that all `asm!` invocations have to be inside an `unsafe` block, as they could insert | ||
2768 | arbitrary instructions and break various invariants. The instructions to be inserted are listed | ||
2769 | in the first argument of the `asm!` macro as a string literal. | ||
2770 | |||
2771 | ## Inputs and outputs | ||
2772 | |||
2773 | Now inserting an instruction that does nothing is rather boring. Let us do something that | ||
2774 | actually acts on data: | ||
2775 | |||
2776 | ```rust,allow_fail | ||
2777 | # #![feature(asm)] | ||
2778 | let x: u64; | ||
2779 | unsafe { | ||
2780 | asm!("mov {}, 5", out(reg) x); | ||
2781 | } | ||
2782 | assert_eq!(x, 5); | ||
2783 | ``` | ||
2784 | |||
2785 | This will write the value `5` into the `u64` variable `x`. | ||
2786 | You can see that the string literal we use to specify instructions is actually a template string. | ||
2787 | It is governed by the same rules as Rust [format strings][format-syntax]. | ||
2788 | The arguments that are inserted into the template however look a bit different then you may | ||
2789 | be familiar with. First we need to specify if the variable is an input or an output of the | ||
2790 | inline assembly. In this case it is an output. We declared this by writing `out`. | ||
2791 | We also need to specify in what kind of register the assembly expects the variable. | ||
2792 | In this case we put it in an arbitrary general purpose register by specifying `reg`. | ||
2793 | The compiler will choose an appropriate register to insert into | ||
2794 | the template and will read the variable from there after the inline assembly finishes executing. | ||
2795 | |||
2796 | Let us see another example that also uses an input: | ||
2797 | |||
2798 | ```rust,allow_fail | ||
2799 | # #![feature(asm)] | ||
2800 | let i: u64 = 3; | ||
2801 | let o: u64; | ||
2802 | unsafe { | ||
2803 | asm!( | ||
2804 | "mov {0}, {1}", | ||
2805 | "add {0}, {number}", | ||
2806 | out(reg) o, | ||
2807 | in(reg) i, | ||
2808 | number = const 5, | ||
2809 | ); | ||
2810 | } | ||
2811 | assert_eq!(o, 8); | ||
2812 | ``` | ||
2813 | |||
2814 | This will add `5` to the input in variable `i` and write the result to variable `o`. | ||
2815 | The particular way this assembly does this is first copying the value from `i` to the output, | ||
2816 | and then adding `5` to it. | ||
2817 | |||
2818 | The example shows a few things: | ||
2819 | |||
2820 | First, we can see that `asm!` allows multiple template string arguments; each | ||
2821 | one is treated as a separate line of assembly code, as if they were all joined | ||
2822 | together with newlines between them. This makes it easy to format assembly | ||
2823 | code. | ||
2824 | |||
2825 | Second, we can see that inputs are declared by writing `in` instead of `out`. | ||
2826 | |||
2827 | Third, one of our operands has a type we haven't seen yet, `const`. | ||
2828 | This tells the compiler to expand this argument to value directly inside the assembly template. | ||
2829 | This is only possible for constants and literals. | ||
2830 | |||
2831 | Fourth, we can see that we can specify an argument number, or name as in any format string. | ||
2832 | For inline assembly templates this is particularly useful as arguments are often used more than once. | ||
2833 | For more complex inline assembly using this facility is generally recommended, as it improves | ||
2834 | readability, and allows reordering instructions without changing the argument order. | ||
2835 | |||
2836 | We can further refine the above example to avoid the `mov` instruction: | ||
2837 | |||
2838 | ```rust,allow_fail | ||
2839 | # #![feature(asm)] | ||
2840 | let mut x: u64 = 3; | ||
2841 | unsafe { | ||
2842 | asm!("add {0}, {number}", inout(reg) x, number = const 5); | ||
2843 | } | ||
2844 | assert_eq!(x, 8); | ||
2845 | ``` | ||
2846 | |||
2847 | We can see that `inout` is used to specify an argument that is both input and output. | ||
2848 | This is different from specifying an input and output separately in that it is guaranteed to assign both to the same register. | ||
2849 | |||
2850 | It is also possible to specify different variables for the input and output parts of an `inout` operand: | ||
2851 | |||
2852 | ```rust,allow_fail | ||
2853 | # #![feature(asm)] | ||
2854 | let x: u64 = 3; | ||
2855 | let y: u64; | ||
2856 | unsafe { | ||
2857 | asm!("add {0}, {number}", inout(reg) x => y, number = const 5); | ||
2858 | } | ||
2859 | assert_eq!(y, 8); | ||
2860 | ``` | ||
2861 | |||
2862 | ## Late output operands | ||
2863 | |||
2864 | The Rust compiler is conservative with its allocation of operands. It is assumed that an `out` | ||
2865 | can be written at any time, and can therefore not share its location with any other argument. | ||
2866 | However, to guarantee optimal performance it is important to use as few registers as possible, | ||
2867 | so they won't have to be saved and reloaded around the inline assembly block. | ||
2868 | To achieve this Rust provides a `lateout` specifier. This can be used on any output that is | ||
2869 | written only after all inputs have been consumed. | ||
2870 | There is also a `inlateout` variant of this specifier. | ||
2871 | |||
2872 | Here is an example where `inlateout` *cannot* be used: | ||
2873 | |||
2874 | ```rust,allow_fail | ||
2875 | # #![feature(asm)] | ||
2876 | let mut a: u64 = 4; | ||
2877 | let b: u64 = 4; | ||
2878 | let c: u64 = 4; | ||
2879 | unsafe { | ||
2880 | asm!( | ||
2881 | "add {0}, {1}", | ||
2882 | "add {0}, {2}", | ||
2883 | inout(reg) a, | ||
2884 | in(reg) b, | ||
2885 | in(reg) c, | ||
2886 | ); | ||
2887 | } | ||
2888 | assert_eq!(a, 12); | ||
2889 | ``` | ||
2890 | |||
2891 | Here the compiler is free to allocate the same register for inputs `b` and `c` since it knows they have the same value. However it must allocate a separate register for `a` since it uses `inout` and not `inlateout`. If `inlateout` was used, then `a` and `c` could be allocated to the same register, in which case the first instruction to overwrite the value of `c` and cause the assembly code to produce the wrong result. | ||
2892 | |||
2893 | However the following example can use `inlateout` since the output is only modified after all input registers have been read: | ||
2894 | |||
2895 | ```rust,allow_fail | ||
2896 | # #![feature(asm)] | ||
2897 | let mut a: u64 = 4; | ||
2898 | let b: u64 = 4; | ||
2899 | unsafe { | ||
2900 | asm!("add {0}, {1}", inlateout(reg) a, in(reg) b); | ||
2901 | } | ||
2902 | assert_eq!(a, 8); | ||
2903 | ``` | ||
2904 | |||
2905 | As you can see, this assembly fragment will still work correctly if `a` and `b` are assigned to the same register. | ||
2906 | |||
2907 | ## Explicit register operands | ||
2908 | |||
2909 | Some instructions require that the operands be in a specific register. | ||
2910 | Therefore, Rust inline assembly provides some more specific constraint specifiers. | ||
2911 | While `reg` is generally available on any architecture, these are highly architecture specific. E.g. for x86 the general purpose registers `eax`, `ebx`, `ecx`, `edx`, `ebp`, `esi`, and `edi` | ||
2912 | among others can be addressed by their name. | ||
2913 | |||
2914 | ```rust,allow_fail,no_run | ||
2915 | # #![feature(asm)] | ||
2916 | let cmd = 0xd1; | ||
2917 | unsafe { | ||
2918 | asm!("out 0x64, eax", in("eax") cmd); | ||
2919 | } | ||
2920 | ``` | ||
2921 | |||
2922 | In this example we call the `out` instruction to output the content of the `cmd` variable | ||
2923 | to port `0x64`. Since the `out` instruction only accepts `eax` (and its sub registers) as operand | ||
2924 | we had to use the `eax` constraint specifier. | ||
2925 | |||
2926 | Note that unlike other operand types, explicit register operands cannot be used in the template string: you can't use `{}` and should write the register name directly instead. Also, they must appear at the end of the operand list after all other operand types. | ||
2927 | |||
2928 | Consider this example which uses the x86 `mul` instruction: | ||
2929 | |||
2930 | ```rust,allow_fail | ||
2931 | # #![feature(asm)] | ||
2932 | fn mul(a: u64, b: u64) -> u128 { | ||
2933 | let lo: u64; | ||
2934 | let hi: u64; | ||
2935 | |||
2936 | unsafe { | ||
2937 | asm!( | ||
2938 | // The x86 mul instruction takes rax as an implicit input and writes | ||
2939 | // the 128-bit result of the multiplication to rax:rdx. | ||
2940 | "mul {}", | ||
2941 | in(reg) a, | ||
2942 | inlateout("rax") b => lo, | ||
2943 | lateout("rdx") hi | ||
2944 | ); | ||
2945 | } | ||
2946 | |||
2947 | ((hi as u128) << 64) + lo as u128 | ||
2948 | } | ||
2949 | ``` | ||
2950 | |||
2951 | This uses the `mul` instruction to multiply two 64-bit inputs with a 128-bit result. | ||
2952 | The only explicit operand is a register, that we fill from the variable `a`. | ||
2953 | The second operand is implicit, and must be the `rax` register, which we fill from the variable `b`. | ||
2954 | The lower 64 bits of the result are stored in `rax` from which we fill the variable `lo`. | ||
2955 | The higher 64 bits are stored in `rdx` from which we fill the variable `hi`. | ||
2956 | |||
2957 | ## Clobbered registers | ||
2958 | |||
2959 | In many cases inline assembly will modify state that is not needed as an output. | ||
2960 | Usually this is either because we have to use a scratch register in the assembly, | ||
2961 | or instructions modify state that we don't need to further examine. | ||
2962 | This state is generally referred to as being "clobbered". | ||
2963 | We need to tell the compiler about this since it may need to save and restore this state | ||
2964 | around the inline assembly block. | ||
2965 | |||
2966 | ```rust,allow_fail | ||
2967 | # #![feature(asm)] | ||
2968 | let ebx: u32; | ||
2969 | let ecx: u32; | ||
2970 | |||
2971 | unsafe { | ||
2972 | asm!( | ||
2973 | "cpuid", | ||
2974 | // EAX 4 selects the "Deterministic Cache Parameters" CPUID leaf | ||
2975 | inout("eax") 4 => _, | ||
2976 | // ECX 0 selects the L0 cache information. | ||
2977 | inout("ecx") 0 => ecx, | ||
2978 | lateout("ebx") ebx, | ||
2979 | lateout("edx") _, | ||
2980 | ); | ||
2981 | } | ||
2982 | |||
2983 | println!( | ||
2984 | "L1 Cache: {}", | ||
2985 | ((ebx >> 22) + 1) * (((ebx >> 12) & 0x3ff) + 1) * ((ebx & 0xfff) + 1) * (ecx + 1) | ||
2986 | ); | ||
2987 | ``` | ||
2988 | |||
2989 | In the example above we use the `cpuid` instruction to get the L1 cache size. | ||
2990 | This instruction writes to `eax`, `ebx`, `ecx`, and `edx`, but for the cache size we only care about the contents of `ebx` and `ecx`. | ||
2991 | |||
2992 | However we still need to tell the compiler that `eax` and `edx` have been modified so that it can save any values that were in these registers before the asm. This is done by declaring these as outputs but with `_` instead of a variable name, which indicates that the output value is to be discarded. | ||
2993 | |||
2994 | This can also be used with a general register class (e.g. `reg`) to obtain a scratch register for use inside the asm code: | ||
2995 | |||
2996 | ```rust,allow_fail | ||
2997 | # #![feature(asm)] | ||
2998 | // Multiply x by 6 using shifts and adds | ||
2999 | let mut x: u64 = 4; | ||
3000 | unsafe { | ||
3001 | asm!( | ||
3002 | "mov {tmp}, {x}", | ||
3003 | "shl {tmp}, 1", | ||
3004 | "shl {x}, 2", | ||
3005 | "add {x}, {tmp}", | ||
3006 | x = inout(reg) x, | ||
3007 | tmp = out(reg) _, | ||
3008 | ); | ||
3009 | } | ||
3010 | assert_eq!(x, 4 * 6); | ||
3011 | ``` | ||
3012 | |||
3013 | ## Symbol operands | ||
3014 | |||
3015 | A special operand type, `sym`, allows you to use the symbol name of a `fn` or `static` in inline assembly code. | ||
3016 | This allows you to call a function or access a global variable without needing to keep its address in a register. | ||
3017 | |||
3018 | ```rust,allow_fail | ||
3019 | # #![feature(asm)] | ||
3020 | extern "C" fn foo(arg: i32) { | ||
3021 | println!("arg = {}", arg); | ||
3022 | } | ||
3023 | |||
3024 | fn call_foo(arg: i32) { | ||
3025 | unsafe { | ||
3026 | asm!( | ||
3027 | "call {}", | ||
3028 | sym foo, | ||
3029 | // 1st argument in rdi, which is caller-saved | ||
3030 | inout("rdi") arg => _, | ||
3031 | // All caller-saved registers must be marked as clobberred | ||
3032 | out("rax") _, out("rcx") _, out("rdx") _, out("rsi") _, | ||
3033 | out("r8") _, out("r9") _, out("r10") _, out("r11") _, | ||
3034 | out("xmm0") _, out("xmm1") _, out("xmm2") _, out("xmm3") _, | ||
3035 | out("xmm4") _, out("xmm5") _, out("xmm6") _, out("xmm7") _, | ||
3036 | out("xmm8") _, out("xmm9") _, out("xmm10") _, out("xmm11") _, | ||
3037 | out("xmm12") _, out("xmm13") _, out("xmm14") _, out("xmm15") _, | ||
3038 | ) | ||
3039 | } | ||
3040 | } | ||
3041 | ``` | ||
3042 | |||
3043 | Note that the `fn` or `static` item does not need to be public or `#[no_mangle]`: | ||
3044 | the compiler will automatically insert the appropriate mangled symbol name into the assembly code. | ||
3045 | |||
3046 | ## Register template modifiers | ||
3047 | |||
3048 | In some cases, fine control is needed over the way a register name is formatted when inserted into the template string. This is needed when an architecture's assembly language has several names for the same register, each typically being a "view" over a subset of the register (e.g. the low 32 bits of a 64-bit register). | ||
3049 | |||
3050 | By default the compiler will always choose the name that refers to the full register size (e.g. `rax` on x86-64, `eax` on x86, etc). | ||
3051 | |||
3052 | This default can be overriden by using modifiers on the template string operands, just like you would with format strings: | ||
3053 | |||
3054 | ```rust,allow_fail | ||
3055 | # #![feature(asm)] | ||
3056 | let mut x: u16 = 0xab; | ||
3057 | |||
3058 | unsafe { | ||
3059 | asm!("mov {0:h}, {0:l}", inout(reg_abcd) x); | ||
3060 | } | ||
3061 | |||
3062 | assert_eq!(x, 0xabab); | ||
3063 | ``` | ||
3064 | |||
3065 | In this example, we use the `reg_abcd` register class to restrict the register allocator to the 4 legacy x86 register (`ax`, `bx`, `cx`, `dx`) of which the first two bytes can be addressed independently. | ||
3066 | |||
3067 | Let us assume that the register allocator has chosen to allocate `x` in the `ax` register. | ||
3068 | The `h` modifier will emit the register name for the high byte of that register and the `l` modifier will emit the register name for the low byte. The asm code will therefore be expanded as `mov ah, al` which copies the low byte of the value into the high byte. | ||
3069 | |||
3070 | If you use a smaller data type (e.g. `u16`) with an operand and forget the use template modifiers, the compiler will emit a warning and suggest the correct modifier to use. | ||
3071 | |||
3072 | ## Memory address operands | ||
3073 | |||
3074 | Sometimes assembly instructions require operands passed via memory addresses/memory locations. | ||
3075 | You have to manually use the memory address syntax specified by the respectively architectures. | ||
3076 | For example, in x86/x86_64 and intel assembly syntax, you should wrap inputs/outputs in `[]` | ||
3077 | to indicate they are memory operands: | ||
3078 | |||
3079 | ```rust,allow_fail | ||
3080 | # #![feature(asm, llvm_asm)] | ||
3081 | # fn load_fpu_control_word(control: u16) { | ||
3082 | unsafe { | ||
3083 | asm!("fldcw [{}]", in(reg) &control, options(nostack)); | ||
3084 | |||
3085 | // Previously this would have been written with the deprecated `llvm_asm!` like this | ||
3086 | llvm_asm!("fldcw $0" :: "m" (control) :: "volatile"); | ||
3087 | } | ||
3088 | # } | ||
3089 | ``` | ||
3090 | |||
3091 | ## Options | ||
3092 | |||
3093 | By default, an inline assembly block is treated the same way as an external FFI function call with a custom calling convention: it may read/write memory, have observable side effects, etc. However in many cases, it is desirable to give the compiler more information about what the assembly code is actually doing so that it can optimize better. | ||
3094 | |||
3095 | Let's take our previous example of an `add` instruction: | ||
3096 | |||
3097 | ```rust,allow_fail | ||
3098 | # #![feature(asm)] | ||
3099 | let mut a: u64 = 4; | ||
3100 | let b: u64 = 4; | ||
3101 | unsafe { | ||
3102 | asm!( | ||
3103 | "add {0}, {1}", | ||
3104 | inlateout(reg) a, in(reg) b, | ||
3105 | options(pure, nomem, nostack), | ||
3106 | ); | ||
3107 | } | ||
3108 | assert_eq!(a, 8); | ||
3109 | ``` | ||
3110 | |||
3111 | Options can be provided as an optional final argument to the `asm!` macro. We specified three options here: | ||
3112 | - `pure` means that the asm code has no observable side effects and that its output depends only on its inputs. This allows the compiler optimizer to call the inline asm fewer times or even eliminate it entirely. | ||
3113 | - `nomem` means that the asm code does not read or write to memory. By default the compiler will assume that inline assembly can read or write any memory address that is accessible to it (e.g. through a pointer passed as an operand, or a global). | ||
3114 | - `nostack` means that the asm code does not push any data onto the stack. This allows the compiler to use optimizations such as the stack red zone on x86-64 to avoid stack pointer adjustments. | ||
3115 | |||
3116 | These allow the compiler to better optimize code using `asm!`, for example by eliminating pure `asm!` blocks whose outputs are not needed. | ||
3117 | |||
3118 | See the reference for the full list of available options and their effects. | ||
3119 | |||
3120 | # Reference-level explanation | ||
3121 | [reference-level-explanation]: #reference-level-explanation | ||
3122 | |||
3123 | Inline assembler is implemented as an unsafe macro `asm!()`. | ||
3124 | The first argument to this macro is a template string literal used to build the final assembly. | ||
3125 | The following arguments specify input and output operands. | ||
3126 | When required, options are specified as the final argument. | ||
3127 | |||
3128 | The following ABNF specifies the general syntax: | ||
3129 | |||
3130 | ```text | ||
3131 | dir_spec := "in" / "out" / "lateout" / "inout" / "inlateout" | ||
3132 | reg_spec := <register class> / "<explicit register>" | ||
3133 | operand_expr := expr / "_" / expr "=>" expr / expr "=>" "_" | ||
3134 | reg_operand := dir_spec "(" reg_spec ")" operand_expr | ||
3135 | operand := reg_operand / "const" const_expr / "sym" path | ||
3136 | option := "pure" / "nomem" / "readonly" / "preserves_flags" / "noreturn" / "nostack" / "att_syntax" | ||
3137 | options := "options(" option *["," option] [","] ")" | ||
3138 | asm := "asm!(" format_string *("," format_string) *("," [ident "="] operand) ["," options] [","] ")" | ||
3139 | ``` | ||
3140 | |||
3141 | The macro will initially be supported only on ARM, AArch64, Hexagon, x86, x86-64 and RISC-V targets. Support for more targets may be added in the future. The compiler will emit an error if `asm!` is used on an unsupported target. | ||
3142 | |||
3143 | [format-syntax]: https://doc.rust-lang.org/std/fmt/#syntax | ||
3144 | |||
3145 | ## Template string arguments | ||
3146 | |||
3147 | The assembler template uses the same syntax as [format strings][format-syntax] (i.e. placeholders are specified by curly braces). The corresponding arguments are accessed in order, by index, or by name. However, implicit named arguments (introduced by [RFC #2795][rfc-2795]) are not supported. | ||
3148 | |||
3149 | An `asm!` invocation may have one or more template string arguments; an `asm!` with multiple template string arguments is treated as if all the strings were concatenated with a `\n` between them. The expected usage is for each template string argument to correspond to a line of assembly code. All template string arguments must appear before any other arguments. | ||
3150 | |||
3151 | As with format strings, named arguments must appear after positional arguments. Explicit register operands must appear at the end of the operand list, after named arguments if any. | ||
3152 | |||
3153 | Explicit register operands cannot be used by placeholders in the template string. All other named and positional operands must appear at least once in the template string, otherwise a compiler error is generated. | ||
3154 | |||
3155 | The exact assembly code syntax is target-specific and opaque to the compiler except for the way operands are substituted into the template string to form the code passed to the assembler. | ||
3156 | |||
3157 | The 5 targets specified in this RFC (x86, ARM, AArch64, RISC-V, Hexagon) all use the assembly code syntax of the GNU assembler (GAS). On x86, the `.intel_syntax noprefix` mode of GAS is used by default. On ARM, the `.syntax unified` mode is used. These targets impose an additional restriction on the assembly code: any assembler state (e.g. the current section which can be changed with `.section`) must be restored to its original value at the end of the asm string. Assembly code that does not conform to the GAS syntax will result in assembler-specific behavior. | ||
3158 | |||
3159 | [rfc-2795]: https://github.com/rust-lang/rfcs/pull/2795 | ||
3160 | |||
3161 | ## Operand type | ||
3162 | |||
3163 | Several types of operands are supported: | ||
3164 | |||
3165 | * `in(<reg>) <expr>` | ||
3166 | - `<reg>` can refer to a register class or an explicit register. The allocated register name is substituted into the asm template string. | ||
3167 | - The allocated register will contain the value of `<expr>` at the start of the asm code. | ||
3168 | - The allocated register must contain the same value at the end of the asm code (except if a `lateout` is allocated to the same register). | ||
3169 | * `out(<reg>) <expr>` | ||
3170 | - `<reg>` can refer to a register class or an explicit register. The allocated register name is substituted into the asm template string. | ||
3171 | - The allocated register will contain an undefined value at the start of the asm code. | ||
3172 | - `<expr>` must be a (possibly uninitialized) place expression, to which the contents of the allocated register is written to at the end of the asm code. | ||
3173 | - An underscore (`_`) may be specified instead of an expression, which will cause the contents of the register to be discarded at the end of the asm code (effectively acting as a clobber). | ||
3174 | * `lateout(<reg>) <expr>` | ||
3175 | - Identical to `out` except that the register allocator can reuse a register allocated to an `in`. | ||
3176 | - You should only write to the register after all inputs are read, otherwise you may clobber an input. | ||
3177 | * `inout(<reg>) <expr>` | ||
3178 | - `<reg>` can refer to a register class or an explicit register. The allocated register name is substituted into the asm template string. | ||
3179 | - The allocated register will contain the value of `<expr>` at the start of the asm code. | ||
3180 | - `<expr>` must be a mutable initialized place expression, to which the contents of the allocated register is written to at the end of the asm code. | ||
3181 | * `inout(<reg>) <in expr> => <out expr>` | ||
3182 | - Same as `inout` except that the initial value of the register is taken from the value of `<in expr>`. | ||
3183 | - `<out expr>` must be a (possibly uninitialized) place expression, to which the contents of the allocated register is written to at the end of the asm code. | ||
3184 | - An underscore (`_`) may be specified instead of an expression for `<out expr>`, which will cause the contents of the register to be discarded at the end of the asm code (effectively acting as a clobber). | ||
3185 | - `<in expr>` and `<out expr>` may have different types. | ||
3186 | * `inlateout(<reg>) <expr>` / `inlateout(<reg>) <in expr> => <out expr>` | ||
3187 | - Identical to `inout` except that the register allocator can reuse a register allocated to an `in` (this can happen if the compiler knows the `in` has the same initial value as the `inlateout`). | ||
3188 | - You should only write to the register after all inputs are read, otherwise you may clobber an input. | ||
3189 | * `const <expr>` | ||
3190 | - `<expr>` must be an integer or floating-point constant expression. | ||
3191 | - The value of the expression is formatted as a string and substituted directly into the asm template string. | ||
3192 | * `sym <path>` | ||
3193 | - `<path>` must refer to a `fn` or `static`. | ||
3194 | - A mangled symbol name referring to the item is substituted into the asm template string. | ||
3195 | - The substituted string does not include any modifiers (e.g. GOT, PLT, relocations, etc). | ||
3196 | - `<path>` is allowed to point to a `#[thread_local]` static, in which case the asm code can combine the symbol with relocations (e.g. `@plt`, `@TPOFF`) to read from thread-local data. | ||
3197 | |||
3198 | Operand expressions are evaluated from left to right, just like function call arguments. After the `asm!` has executed, outputs are written to in left to right order. This is significant if two outputs point to the same place: that place will contain the value of the rightmost output. | ||
3199 | |||
3200 | ## Register operands | ||
3201 | |||
3202 | Input and output operands can be specified either as an explicit register or as a register class from which the register allocator can select a register. Explicit registers are specified as string literals (e.g. `"eax"`) while register classes are specified as identifiers (e.g. `reg`). Using string literals for register names enables support for architectures that use special characters in register names, such as MIPS (`$0`, `$1`, etc). | ||
3203 | |||
3204 | Note that explicit registers treat register aliases (e.g. `r14` vs `lr` on ARM) and smaller views of a register (e.g. `eax` vs `rax`) as equivalent to the base register. It is a compile-time error to use the same explicit register for two input operands or two output operands. Additionally, it is also a compile-time error to use overlapping registers (e.g. ARM VFP) in input operands or in output operands. | ||
3205 | |||
3206 | Only the following types are allowed as operands for inline assembly: | ||
3207 | - Integers (signed and unsigned) | ||
3208 | - Floating-point numbers | ||
3209 | - Pointers (thin only) | ||
3210 | - Function pointers | ||
3211 | - SIMD vectors (structs defined with `#[repr(simd)]` and which implement `Copy`). This includes architecture-specific vector types defined in `std::arch` such as `__m128` (x86) or `int8x16_t` (ARM). | ||
3212 | |||
3213 | Here is the list of currently supported register classes: | ||
3214 | |||
3215 | | Architecture | Register class | Registers | LLVM constraint code | | ||
3216 | | ------------ | -------------- | --------- | -------------------- | | ||
3217 | | x86 | `reg` | `ax`, `bx`, `cx`, `dx`, `si`, `di`, `r[8-15]` (x86-64 only) | `r` | | ||
3218 | | x86 | `reg_abcd` | `ax`, `bx`, `cx`, `dx` | `Q` | | ||
3219 | | x86-32 | `reg_byte` | `al`, `bl`, `cl`, `dl`, `ah`, `bh`, `ch`, `dh` | `q` | | ||
3220 | | x86-64 | `reg_byte` | `al`, `bl`, `cl`, `dl`, `sil`, `dil`, `r[8-15]b`, `ah`\*, `bh`\*, `ch`\*, `dh`\* | `q` | | ||
3221 | | x86 | `xmm_reg` | `xmm[0-7]` (x86) `xmm[0-15]` (x86-64) | `x` | | ||
3222 | | x86 | `ymm_reg` | `ymm[0-7]` (x86) `ymm[0-15]` (x86-64) | `x` | | ||
3223 | | x86 | `zmm_reg` | `zmm[0-7]` (x86) `zmm[0-31]` (x86-64) | `v` | | ||
3224 | | x86 | `kreg` | `k[1-7]` | `Yk` | | ||
3225 | | AArch64 | `reg` | `x[0-28]`, `x30` | `r` | | ||
3226 | | AArch64 | `vreg` | `v[0-31]` | `w` | | ||
3227 | | AArch64 | `vreg_low16` | `v[0-15]` | `x` | | ||
3228 | | ARM | `reg` | `r[0-5]` `r7`\*, `r[8-10]`, `r11`\*, `r12`, `r14` | `r` | | ||
3229 | | ARM (Thumb) | `reg_thumb` | `r[0-r7]` | `l` | | ||
3230 | | ARM (ARM) | `reg_thumb` | `r[0-r10]`, `r12`, `r14` | `l` | | ||
3231 | | ARM | `sreg` | `s[0-31]` | `t` | | ||
3232 | | ARM | `sreg_low16` | `s[0-15]` | `x` | | ||
3233 | | ARM | `dreg` | `d[0-31]` | `w` | | ||
3234 | | ARM | `dreg_low16` | `d[0-15]` | `t` | | ||
3235 | | ARM | `dreg_low8` | `d[0-8]` | `x` | | ||
3236 | | ARM | `qreg` | `q[0-15]` | `w` | | ||
3237 | | ARM | `qreg_low8` | `q[0-7]` | `t` | | ||
3238 | | ARM | `qreg_low4` | `q[0-3]` | `x` | | ||
3239 | | MIPS | `reg` | `$[2-25]` | `r` | | ||
3240 | | MIPS | `freg` | `$f[0-31]` | `f` | | ||
3241 | | NVPTX | `reg16` | None\* | `h` | | ||
3242 | | NVPTX | `reg32` | None\* | `r` | | ||
3243 | | NVPTX | `reg64` | None\* | `l` | | ||
3244 | | RISC-V | `reg` | `x1`, `x[5-7]`, `x[9-15]`, `x[16-31]` (non-RV32E) | `r` | | ||
3245 | | RISC-V | `freg` | `f[0-31]` | `f` | | ||
3246 | | Hexagon | `reg` | `r[0-28]` | `r` | | ||
3247 | | wasm32 | `local` | None\* | `r` | | ||
3248 | |||
3249 | > **Note**: On x86 we treat `reg_byte` differently from `reg` because the compiler can allocate `al` and `ah` separately whereas `reg` reserves the whole register. | ||
3250 | > | ||
3251 | > Note #2: On x86-64 the high byte registers (e.g. `ah`) are only available when used as an explicit register. Specifying the `reg_byte` register class for an operand will always allocate a low byte register. | ||
3252 | > | ||
3253 | > Note #3: NVPTX doesn't have a fixed register set, so named registers are not supported. | ||
3254 | > | ||
3255 | > Note #4: On ARM the frame pointer is either `r7` or `r11` depending on the platform. | ||
3256 | > | ||
3257 | > Note #5: WebAssembly doesn't have registers, so named registers are not supported. | ||
3258 | |||
3259 | Additional register classes may be added in the future based on demand (e.g. MMX, x87, etc). | ||
3260 | |||
3261 | Each register class has constraints on which value types they can be used with. This is necessary because the way a value is loaded into a register depends on its type. For example, on big-endian systems, loading a `i32x4` and a `i8x16` into a SIMD register may result in different register contents even if the byte-wise memory representation of both values is identical. The availability of supported types for a particular register class may depend on what target features are currently enabled. | ||
3262 | |||
3263 | | Architecture | Register class | Target feature | Allowed types | | ||
3264 | | ------------ | -------------- | -------------- | ------------- | | ||
3265 | | x86-32 | `reg` | None | `i16`, `i32`, `f32` | | ||
3266 | | x86-64 | `reg` | None | `i16`, `i32`, `f32`, `i64`, `f64` | | ||
3267 | | x86 | `reg_byte` | None | `i8` | | ||
3268 | | x86 | `xmm_reg` | `sse` | `i32`, `f32`, `i64`, `f64`, <br> `i8x16`, `i16x8`, `i32x4`, `i64x2`, `f32x4`, `f64x2` | | ||
3269 | | x86 | `ymm_reg` | `avx` | `i32`, `f32`, `i64`, `f64`, <br> `i8x16`, `i16x8`, `i32x4`, `i64x2`, `f32x4`, `f64x2` <br> `i8x32`, `i16x16`, `i32x8`, `i64x4`, `f32x8`, `f64x4` | | ||
3270 | | x86 | `zmm_reg` | `avx512f` | `i32`, `f32`, `i64`, `f64`, <br> `i8x16`, `i16x8`, `i32x4`, `i64x2`, `f32x4`, `f64x2` <br> `i8x32`, `i16x16`, `i32x8`, `i64x4`, `f32x8`, `f64x4` <br> `i8x64`, `i16x32`, `i32x16`, `i64x8`, `f32x16`, `f64x8` | | ||
3271 | | x86 | `kreg` | `axv512f` | `i8`, `i16` | | ||
3272 | | x86 | `kreg` | `axv512bw` | `i32`, `i64` | | ||
3273 | | AArch64 | `reg` | None | `i8`, `i16`, `i32`, `f32`, `i64`, `f64` | | ||
3274 | | AArch64 | `vreg` | `fp` | `i8`, `i16`, `i32`, `f32`, `i64`, `f64`, <br> `i8x8`, `i16x4`, `i32x2`, `i64x1`, `f32x2`, `f64x1`, <br> `i8x16`, `i16x8`, `i32x4`, `i64x2`, `f32x4`, `f64x2` | | ||
3275 | | ARM | `reg` | None | `i8`, `i16`, `i32`, `f32` | | ||
3276 | | ARM | `sreg` | `vfp2` | `i32`, `f32` | | ||
3277 | | ARM | `dreg` | `vfp2` | `i64`, `f64`, `i8x8`, `i16x4`, `i32x2`, `i64x1`, `f32x2` | | ||
3278 | | ARM | `qreg` | `neon` | `i8x16`, `i16x8`, `i32x4`, `i64x2`, `f32x4` | | ||
3279 | | MIPS32 | `reg` | None | `i8`, `i16`, `i32`, `f32` | | ||
3280 | | MIPS32 | `freg` | None | `f32`, `f64` | | ||
3281 | | MIPS64 | `reg` | None | `i8`, `i16`, `i32`, `i64`, `f32`, `f64` | | ||
3282 | | MIPS64 | `freg` | None | `f32`, `f64` | | ||
3283 | | NVPTX | `reg16` | None | `i8`, `i16` | | ||
3284 | | NVPTX | `reg32` | None | `i8`, `i16`, `i32`, `f32` | | ||
3285 | | NVPTX | `reg64` | None | `i8`, `i16`, `i32`, `f32`, `i64`, `f64` | | ||
3286 | | RISC-V32 | `reg` | None | `i8`, `i16`, `i32`, `f32` | | ||
3287 | | RISC-V64 | `reg` | None | `i8`, `i16`, `i32`, `f32`, `i64`, `f64` | | ||
3288 | | RISC-V | `freg` | `f` | `f32` | | ||
3289 | | RISC-V | `freg` | `d` | `f64` | | ||
3290 | | Hexagon | `reg` | None | `i8`, `i16`, `i32`, `f32` | | ||
3291 | | wasm32 | `local` | None | `i8` `i16` `i32` `i64` `f32` `f64` | | ||
3292 | |||
3293 | > **Note**: For the purposes of the above table pointers, function pointers and `isize`/`usize` are treated as the equivalent integer type (`i16`/`i32`/`i64` depending on the target). | ||
3294 | |||
3295 | If a value is of a smaller size than the register it is allocated in then the upper bits of that register will have an undefined value for inputs and will be ignored for outputs. The only exception is the `freg` register class on RISC-V where `f32` values are NaN-boxed in a `f64` as required by the RISC-V architecture. | ||
3296 | |||
3297 | When separate input and output expressions are specified for an `inout` operand, both expressions must have the same type. The only exception is if both operands are pointers or integers, in which case they are only required to have the same size. This restriction exists because the register allocators in LLVM and GCC sometimes cannot handle tied operands with different types. | ||
3298 | |||
3299 | ## Register names | ||
3300 | |||
3301 | Some registers have multiple names. These are all treated by the compiler as identical to the base register name. Here is the list of all supported register aliases: | ||
3302 | |||
3303 | | Architecture | Base register | Aliases | | ||
3304 | | ------------ | ------------- | ------- | | ||
3305 | | x86 | `ax` | `eax`, `rax` | | ||
3306 | | x86 | `bx` | `ebx`, `rbx` | | ||
3307 | | x86 | `cx` | `ecx`, `rcx` | | ||
3308 | | x86 | `dx` | `edx`, `rdx` | | ||
3309 | | x86 | `si` | `esi`, `rsi` | | ||
3310 | | x86 | `di` | `edi`, `rdi` | | ||
3311 | | x86 | `bp` | `bpl`, `ebp`, `rbp` | | ||
3312 | | x86 | `sp` | `spl`, `esp`, `rsp` | | ||
3313 | | x86 | `ip` | `eip`, `rip` | | ||
3314 | | x86 | `st(0)` | `st` | | ||
3315 | | x86 | `r[8-15]` | `r[8-15]b`, `r[8-15]w`, `r[8-15]d` | | ||
3316 | | x86 | `xmm[0-31]` | `ymm[0-31]`, `zmm[0-31]` | | ||
3317 | | AArch64 | `x[0-30]` | `w[0-30]` | | ||
3318 | | AArch64 | `x29` | `fp` | | ||
3319 | | AArch64 | `x30` | `lr` | | ||
3320 | | AArch64 | `sp` | `wsp` | | ||
3321 | | AArch64 | `xzr` | `wzr` | | ||
3322 | | AArch64 | `v[0-31]` | `b[0-31]`, `h[0-31]`, `s[0-31]`, `d[0-31]`, `q[0-31]` | | ||
3323 | | ARM | `r[0-3]` | `a[1-4]` | | ||
3324 | | ARM | `r[4-9]` | `v[1-6]` | | ||
3325 | | ARM | `r9` | `rfp` | | ||
3326 | | ARM | `r10` | `sl` | | ||
3327 | | ARM | `r11` | `fp` | | ||
3328 | | ARM | `r12` | `ip` | | ||
3329 | | ARM | `r13` | `sp` | | ||
3330 | | ARM | `r14` | `lr` | | ||
3331 | | ARM | `r15` | `pc` | | ||
3332 | | RISC-V | `x0` | `zero` | | ||
3333 | | RISC-V | `x1` | `ra` | | ||
3334 | | RISC-V | `x2` | `sp` | | ||
3335 | | RISC-V | `x3` | `gp` | | ||
3336 | | RISC-V | `x4` | `tp` | | ||
3337 | | RISC-V | `x[5-7]` | `t[0-2]` | | ||
3338 | | RISC-V | `x8` | `fp`, `s0` | | ||
3339 | | RISC-V | `x9` | `s1` | | ||
3340 | | RISC-V | `x[10-17]` | `a[0-7]` | | ||
3341 | | RISC-V | `x[18-27]` | `s[2-11]` | | ||
3342 | | RISC-V | `x[28-31]` | `t[3-6]` | | ||
3343 | | RISC-V | `f[0-7]` | `ft[0-7]` | | ||
3344 | | RISC-V | `f[8-9]` | `fs[0-1]` | | ||
3345 | | RISC-V | `f[10-17]` | `fa[0-7]` | | ||
3346 | | RISC-V | `f[18-27]` | `fs[2-11]` | | ||
3347 | | RISC-V | `f[28-31]` | `ft[8-11]` | | ||
3348 | | Hexagon | `r29` | `sp` | | ||
3349 | | Hexagon | `r30` | `fr` | | ||
3350 | | Hexagon | `r31` | `lr` | | ||
3351 | |||
3352 | Some registers cannot be used for input or output operands: | ||
3353 | |||
3354 | | Architecture | Unsupported register | Reason | | ||
3355 | | ------------ | -------------------- | ------ | | ||
3356 | | All | `sp` | The stack pointer must be restored to its original value at the end of an asm code block. | | ||
3357 | | All | `bp` (x86), `x29` (AArch64), `x8` (RISC-V), `fr` (Hexagon), `$fp` (MIPS) | The frame pointer cannot be used as an input or output. | | ||
3358 | | ARM | `r7` or `r11` | On ARM the frame pointer can be either `r7` or `r11` depending on the target. The frame pointer cannot be used as an input or output. | | ||
3359 | | ARM | `r6` | `r6` is used internally by LLVM as a base pointer and therefore cannot be used as an input or output. | | ||
3360 | | x86 | `k0` | This is a constant zero register which can't be modified. | | ||
3361 | | x86 | `ip` | This is the program counter, not a real register. | | ||
3362 | | x86 | `mm[0-7]` | MMX registers are not currently supported (but may be in the future). | | ||
3363 | | x86 | `st([0-7])` | x87 registers are not currently supported (but may be in the future). | | ||
3364 | | AArch64 | `xzr` | This is a constant zero register which can't be modified. | | ||
3365 | | ARM | `pc` | This is the program counter, not a real register. | | ||
3366 | | MIPS | `$0` or `$zero` | This is a constant zero register which can't be modified. | | ||
3367 | | MIPS | `$1` or `$at` | Reserved for assembler. | | ||
3368 | | MIPS | `$26`/`$k0`, `$27`/`$k1` | OS-reserved registers. | | ||
3369 | | MIPS | `$28`/`$gp` | Global pointer cannot be used as inputs or outputs. | | ||
3370 | | MIPS | `$ra` | Return address cannot be used as inputs or outputs. | | ||
3371 | | RISC-V | `x0` | This is a constant zero register which can't be modified. | | ||
3372 | | RISC-V | `gp`, `tp` | These registers are reserved and cannot be used as inputs or outputs. | | ||
3373 | | Hexagon | `lr` | This is the link register which cannot be used as an input or output. | | ||
3374 | |||
3375 | In some cases LLVM will allocate a "reserved register" for `reg` operands even though this register cannot be explicitly specified. Assembly code making use of reserved registers should be careful since `reg` operands may alias with those registers. Reserved registers are: | ||
3376 | - The frame pointer on all architectures. | ||
3377 | - `r6` on ARM. | ||
3378 | |||
3379 | ## Template modifiers | ||
3380 | |||
3381 | The placeholders can be augmented by modifiers which are specified after the `:` in the curly braces. These modifiers do not affect register allocation, but change the way operands are formatted when inserted into the template string. Only one modifier is allowed per template placeholder. | ||
3382 | |||
3383 | The supported modifiers are a subset of LLVM's (and GCC's) [asm template argument modifiers][llvm-argmod], but do not use the same letter codes. | ||
3384 | |||
3385 | | Architecture | Register class | Modifier | Example output | LLVM modifier | | ||
3386 | | ------------ | -------------- | -------- | -------------- | ------------- | | ||
3387 | | x86-32 | `reg` | None | `eax` | `k` | | ||
3388 | | x86-64 | `reg` | None | `rax` | `q` | | ||
3389 | | x86-32 | `reg_abcd` | `l` | `al` | `b` | | ||
3390 | | x86-64 | `reg` | `l` | `al` | `b` | | ||
3391 | | x86 | `reg_abcd` | `h` | `ah` | `h` | | ||
3392 | | x86 | `reg` | `x` | `ax` | `w` | | ||
3393 | | x86 | `reg` | `e` | `eax` | `k` | | ||
3394 | | x86-64 | `reg` | `r` | `rax` | `q` | | ||
3395 | | x86 | `reg_byte` | None | `al` / `ah` | None | | ||
3396 | | x86 | `xmm_reg` | None | `xmm0` | `x` | | ||
3397 | | x86 | `ymm_reg` | None | `ymm0` | `t` | | ||
3398 | | x86 | `zmm_reg` | None | `zmm0` | `g` | | ||
3399 | | x86 | `*mm_reg` | `x` | `xmm0` | `x` | | ||
3400 | | x86 | `*mm_reg` | `y` | `ymm0` | `t` | | ||
3401 | | x86 | `*mm_reg` | `z` | `zmm0` | `g` | | ||
3402 | | x86 | `kreg` | None | `k1` | None | | ||
3403 | | AArch64 | `reg` | None | `x0` | `x` | | ||
3404 | | AArch64 | `reg` | `w` | `w0` | `w` | | ||
3405 | | AArch64 | `reg` | `x` | `x0` | `x` | | ||
3406 | | AArch64 | `vreg` | None | `v0` | None | | ||
3407 | | AArch64 | `vreg` | `v` | `v0` | None | | ||
3408 | | AArch64 | `vreg` | `b` | `b0` | `b` | | ||
3409 | | AArch64 | `vreg` | `h` | `h0` | `h` | | ||
3410 | | AArch64 | `vreg` | `s` | `s0` | `s` | | ||
3411 | | AArch64 | `vreg` | `d` | `d0` | `d` | | ||
3412 | | AArch64 | `vreg` | `q` | `q0` | `q` | | ||
3413 | | ARM | `reg` | None | `r0` | None | | ||
3414 | | ARM | `sreg` | None | `s0` | None | | ||
3415 | | ARM | `dreg` | None | `d0` | `P` | | ||
3416 | | ARM | `qreg` | None | `q0` | `q` | | ||
3417 | | ARM | `qreg` | `e` / `f` | `d0` / `d1` | `e` / `f` | | ||
3418 | | MIPS | `reg` | None | `$2` | None | | ||
3419 | | MIPS | `freg` | None | `$f0` | None | | ||
3420 | | NVPTX | `reg16` | None | `rs0` | None | | ||
3421 | | NVPTX | `reg32` | None | `r0` | None | | ||
3422 | | NVPTX | `reg64` | None | `rd0` | None | | ||
3423 | | RISC-V | `reg` | None | `x1` | None | | ||
3424 | | RISC-V | `freg` | None | `f0` | None | | ||
3425 | | Hexagon | `reg` | None | `r0` | None | | ||
3426 | |||
3427 | > Notes: | ||
3428 | > - on ARM `e` / `f`: this prints the low or high doubleword register name of a NEON quad (128-bit) register. | ||
3429 | > - on x86: our behavior for `reg` with no modifiers differs from what GCC does. GCC will infer the modifier based on the operand value type, while we default to the full register size. | ||
3430 | > - on x86 `xmm_reg`: the `x`, `t` and `g` LLVM modifiers are not yet implemented in LLVM (they are supported by GCC only), but this should be a simple change. | ||
3431 | |||
3432 | As stated in the previous section, passing an input value smaller than the register width will result in the upper bits of the register containing undefined values. This is not a problem if the inline asm only accesses the lower bits of the register, which can be done by using a template modifier to use a subregister name in the asm code (e.g. `ax` instead of `rax`). Since this an easy pitfall, the compiler will suggest a template modifier to use where appropriate given the input type. If all references to an operand already have modifiers then the warning is suppressed for that operand. | ||
3433 | |||
3434 | [llvm-argmod]: http://llvm.org/docs/LangRef.html#asm-template-argument-modifiers | ||
3435 | |||
3436 | ## Options | ||
3437 | |||
3438 | Flags are used to further influence the behavior of the inline assembly block. | ||
3439 | Currently the following options are defined: | ||
3440 | - `pure`: The `asm` block has no side effects, and its outputs depend only on its direct inputs (i.e. the values themselves, not what they point to) or values read from memory (unless the `nomem` options is also set). This allows the compiler to execute the `asm` block fewer times than specified in the program (e.g. by hoisting it out of a loop) or even eliminate it entirely if the outputs are not used. | ||
3441 | - `nomem`: The `asm` blocks does not read or write to any memory. This allows the compiler to cache the values of modified global variables in registers across the `asm` block since it knows that they are not read or written to by the `asm`. | ||
3442 | - `readonly`: The `asm` block does not write to any memory. This allows the compiler to cache the values of unmodified global variables in registers across the `asm` block since it knows that they are not written to by the `asm`. | ||
3443 | - `preserves_flags`: The `asm` block does not modify the flags register (defined in the rules below). This allows the compiler to avoid recomputing the condition flags after the `asm` block. | ||
3444 | - `noreturn`: The `asm` block never returns, and its return type is defined as `!` (never). Behavior is undefined if execution falls through past the end of the asm code. A `noreturn` asm block behaves just like a function which doesn't return; notably, local variables in scope are not dropped before it is invoked. | ||
3445 | - `nostack`: The `asm` block does not push data to the stack, or write to the stack red-zone (if supported by the target). If this option is *not* used then the stack pointer is guaranteed to be suitably aligned (according to the target ABI) for a function call. | ||
3446 | - `att_syntax`: This option is only valid on x86, and causes the assembler to use the `.att_syntax prefix` mode of the GNU assembler. Register operands are substituted in with a leading `%`. | ||
3447 | |||
3448 | The compiler performs some additional checks on options: | ||
3449 | - The `nomem` and `readonly` options are mutually exclusive: it is a compile-time error to specify both. | ||
3450 | - The `pure` option must be combined with either the `nomem` or `readonly` options, otherwise a compile-time error is emitted. | ||
3451 | - It is a compile-time error to specify `pure` on an asm block with no outputs or only discarded outputs (`_`). | ||
3452 | - It is a compile-time error to specify `noreturn` on an asm block with outputs. | ||
3453 | |||
3454 | ## Rules for inline assembly | ||
3455 | |||
3456 | - Any registers not specified as inputs will contain an undefined value on entry to the asm block. | ||
3457 | - An "undefined value" in the context of inline assembly means that the register can (non-deterministically) have any one of the possible values allowed by the architecture. Notably it is not the same as an LLVM `undef` which can have a different value every time you read it (since such a concept does not exist in assembly code). | ||
3458 | - Any registers not specified as outputs must have the same value upon exiting the asm block as they had on entry, otherwise behavior is undefined. | ||
3459 | - This only applies to registers which can be specified as an input or output. Other registers follow target-specific rules. | ||
3460 | - Note that a `lateout` may be allocated to the same register as an `in`, in which case this rule does not apply. Code should not rely on this however since it depends on the results of register allocation. | ||
3461 | - Behavior is undefined if execution unwinds out of an asm block. | ||
3462 | - This also applies if the assembly code calls a function which then unwinds. | ||
3463 | - The set of memory locations that assembly code is allowed the read and write are the same as those allowed for an FFI function. | ||
3464 | - Refer to the unsafe code guidelines for the exact rules. | ||
3465 | - If the `readonly` option is set, then only memory reads are allowed. | ||
3466 | - If the `nomem` option is set then no reads or writes to memory are allowed. | ||
3467 | - These rules do not apply to memory which is private to the asm code, such as stack space allocated within the asm block. | ||
3468 | - The compiler cannot assume that the instructions in the asm are the ones that will actually end up executed. | ||
3469 | - This effectively means that the compiler must treat the `asm!` as a black box and only take the interface specification into account, not the instructions themselves. | ||
3470 | - Runtime code patching is allowed, via target-specific mechanisms (outside the scope of this RFC). | ||
3471 | - Unless the `nostack` option is set, asm code is allowed to use stack space below the stack pointer. | ||
3472 | - On entry to the asm block the stack pointer is guaranteed to be suitably aligned (according to the target ABI) for a function call. | ||
3473 | - You are responsible for making sure you don't overflow the stack (e.g. use stack probing to ensure you hit a guard page). | ||
3474 | - You should adjust the stack pointer when allocating stack memory as required by the target ABI. | ||
3475 | - The stack pointer must be restored to its original value before leaving the asm block. | ||
3476 | - If the `noreturn` option is set then behavior is undefined if execution falls through to the end of the asm block. | ||
3477 | - If the `pure` option is set then behavior is undefined if the `asm` has side-effects other than its direct outputs. Behavior is also undefined if two executions of the `asm` code with the same inputs result in different outputs. | ||
3478 | - When used with the `nomem` option, "inputs" are just the direct inputs of the `asm!`. | ||
3479 | - When used with the `readonly` option, "inputs" comprise the direct inputs of the `asm!` and any memory that the `asm!` block is allowed to read. | ||
3480 | - These flags registers must be restored upon exiting the asm block if the `preserves_flags` option is set: | ||
3481 | - x86 | ||
3482 | - Status flags in `EFLAGS` (CF, PF, AF, ZF, SF, OF). | ||
3483 | - Floating-point status word (all). | ||
3484 | - Floating-point exception flags in `MXCSR` (PE, UE, OE, ZE, DE, IE). | ||
3485 | - ARM | ||
3486 | - Condition flags in `CPSR` (N, Z, C, V) | ||
3487 | - Saturation flag in `CPSR` (Q) | ||
3488 | - Greater than or equal flags in `CPSR` (GE). | ||
3489 | - Condition flags in `FPSCR` (N, Z, C, V) | ||
3490 | - Saturation flag in `FPSCR` (QC) | ||
3491 | - Floating-point exception flags in `FPSCR` (IDC, IXC, UFC, OFC, DZC, IOC). | ||
3492 | - AArch64 | ||
3493 | - Condition flags (`NZCV` register). | ||
3494 | - Floating-point status (`FPSR` register). | ||
3495 | - RISC-V | ||
3496 | - Floating-point exception flags in `fcsr` (`fflags`). | ||
3497 | - On x86, the direction flag (DF in `EFLAGS`) is clear on entry to an asm block and must be clear on exit. | ||
3498 | - Behavior is undefined if the direction flag is set on exiting an asm block. | ||
3499 | - The requirement of restoring the stack pointer and non-output registers to their original value only applies when exiting an `asm!` block. | ||
3500 | - This means that `asm!` blocks that never return (even if not marked `noreturn`) don't need to preserve these registers. | ||
3501 | - When returning to a different `asm!` block than you entered (e.g. for context switching), these registers must contain the value they had upon entering the `asm!` block that you are *exiting*. | ||
3502 | - You cannot exit an `asm!` block that has not been entered. Neither can you exit an `asm!` block that has already been exited. | ||
3503 | - You are responsible for switching any target-specific state (e.g. thread-local storage, stack bounds). | ||
3504 | - The set of memory locations that you may access is the intersection of those allowed by the `asm!` blocks you entered and exited. | ||
3505 | - You cannot assume that an `asm!` block will appear exactly once in the output binary. The compiler is allowed to instantiate multiple copies of the `asm!` block, for example when the function containing it is inlined in multiple places. | ||
3506 | - As a consequence, you should only use [local labels] inside inline assembly code. Defining symbols in assembly code may lead to assembler and/or linker errors due to duplicate symbol definitions. | ||
3507 | |||
3508 | > **Note**: As a general rule, the flags covered by `preserves_flags` are those which are *not* preserved when performing a function call. | ||
3509 | |||
3510 | [local labels]: https://sourceware.org/binutils/docs/as/Symbol-Names.html#Local-Labels | ||
3511 | "##, | ||
3512 | }, | ||
3513 | LintCompletion { | ||
3514 | label: "flt2dec", | ||
3515 | description: r##"# `flt2dec` | ||
3516 | |||
3517 | This feature is internal to the Rust compiler and is not intended for general use. | ||
3518 | |||
3519 | ------------------------ | ||
3520 | "##, | ||
3521 | }, | ||
3522 | LintCompletion { | ||
3523 | label: "global_asm", | ||
3524 | description: r##"# `global_asm` | ||
3525 | |||
3526 | The tracking issue for this feature is: [#35119] | ||
3527 | |||
3528 | [#35119]: https://github.com/rust-lang/rust/issues/35119 | ||
3529 | |||
3530 | ------------------------ | ||
3531 | |||
3532 | The `global_asm!` macro allows the programmer to write arbitrary | ||
3533 | assembly outside the scope of a function body, passing it through | ||
3534 | `rustc` and `llvm` to the assembler. The macro is a no-frills | ||
3535 | interface to LLVM's concept of [module-level inline assembly]. That is, | ||
3536 | all caveats applicable to LLVM's module-level inline assembly apply | ||
3537 | to `global_asm!`. | ||
3538 | |||
3539 | [module-level inline assembly]: http://llvm.org/docs/LangRef.html#module-level-inline-assembly | ||
3540 | |||
3541 | `global_asm!` fills a role not currently satisfied by either `asm!` | ||
3542 | or `#[naked]` functions. The programmer has _all_ features of the | ||
3543 | assembler at their disposal. The linker will expect to resolve any | ||
3544 | symbols defined in the inline assembly, modulo any symbols marked as | ||
3545 | external. It also means syntax for directives and assembly follow the | ||
3546 | conventions of the assembler in your toolchain. | ||
3547 | |||
3548 | A simple usage looks like this: | ||
3549 | |||
3550 | ```rust,ignore (requires-external-file) | ||
3551 | #![feature(global_asm)] | ||
3552 | # // you also need relevant target_arch cfgs | ||
3553 | global_asm!(include_str!("something_neato.s")); | ||
3554 | ``` | ||
3555 | |||
3556 | And a more complicated usage looks like this: | ||
3557 | |||
3558 | ```rust,no_run | ||
3559 | #![feature(global_asm)] | ||
3560 | # #[cfg(any(target_arch="x86", target_arch="x86_64"))] | ||
3561 | # mod x86 { | ||
3562 | |||
3563 | pub mod sally { | ||
3564 | global_asm!(r#" | ||
3565 | .global foo | ||
3566 | foo: | ||
3567 | jmp baz | ||
3568 | "#); | ||
3569 | |||
3570 | #[no_mangle] | ||
3571 | pub unsafe extern "C" fn baz() {} | ||
3572 | } | ||
3573 | |||
3574 | // the symbols `foo` and `bar` are global, no matter where | ||
3575 | // `global_asm!` was used. | ||
3576 | extern "C" { | ||
3577 | fn foo(); | ||
3578 | fn bar(); | ||
3579 | } | ||
3580 | |||
3581 | pub mod harry { | ||
3582 | global_asm!(r#" | ||
3583 | .global bar | ||
3584 | bar: | ||
3585 | jmp quux | ||
3586 | "#); | ||
3587 | |||
3588 | #[no_mangle] | ||
3589 | pub unsafe extern "C" fn quux() {} | ||
3590 | } | ||
3591 | # } | ||
3592 | ``` | ||
3593 | |||
3594 | You may use `global_asm!` multiple times, anywhere in your crate, in | ||
3595 | whatever way suits you. The effect is as if you concatenated all | ||
3596 | usages and placed the larger, single usage in the crate root. | ||
3597 | |||
3598 | ------------------------ | ||
3599 | |||
3600 | If you don't need quite as much power and flexibility as | ||
3601 | `global_asm!` provides, and you don't mind restricting your inline | ||
3602 | assembly to `fn` bodies only, you might try the | ||
3603 | [asm](asm.md) feature instead. | ||
3604 | "##, | ||
3605 | }, | ||
3606 | LintCompletion { | ||
3607 | label: "derive_eq", | ||
3608 | description: r##"# `derive_eq` | ||
3609 | |||
3610 | This feature is internal to the Rust compiler and is not intended for general use. | ||
3611 | |||
3612 | ------------------------ | ||
3613 | "##, | ||
3614 | }, | ||
3615 | LintCompletion { | ||
3616 | label: "default_free_fn", | ||
3617 | description: r##"# `default_free_fn` | ||
3618 | |||
3619 | The tracking issue for this feature is: [#73014] | ||
3620 | |||
3621 | [#73014]: https://github.com/rust-lang/rust/issues/73014 | ||
3622 | |||
3623 | ------------------------ | ||
3624 | |||
3625 | Adds a free `default()` function to the `std::default` module. This function | ||
3626 | just forwards to [`Default::default()`], but may remove repetition of the word | ||
3627 | "default" from the call site. | ||
3628 | |||
3629 | [`Default::default()`]: https://doc.rust-lang.org/nightly/std/default/trait.Default.html#tymethod.default | ||
3630 | |||
3631 | Here is an example: | ||
3632 | |||
3633 | ```rust | ||
3634 | #![feature(default_free_fn)] | ||
3635 | use std::default::default; | ||
3636 | |||
3637 | #[derive(Default)] | ||
3638 | struct AppConfig { | ||
3639 | foo: FooConfig, | ||
3640 | bar: BarConfig, | ||
3641 | } | ||
3642 | |||
3643 | #[derive(Default)] | ||
3644 | struct FooConfig { | ||
3645 | foo: i32, | ||
3646 | } | ||
3647 | |||
3648 | #[derive(Default)] | ||
3649 | struct BarConfig { | ||
3650 | bar: f32, | ||
3651 | baz: u8, | ||
3652 | } | ||
3653 | |||
3654 | fn main() { | ||
3655 | let options = AppConfig { | ||
3656 | foo: default(), | ||
3657 | bar: BarConfig { | ||
3658 | bar: 10.1, | ||
3659 | ..default() | ||
3660 | }, | ||
3661 | }; | ||
3662 | } | ||
3663 | ``` | ||
3664 | "##, | ||
3665 | }, | ||
3666 | LintCompletion { | ||
3667 | label: "char_error_internals", | ||
3668 | description: r##"# `char_error_internals` | ||
3669 | |||
3670 | This feature is internal to the Rust compiler and is not intended for general use. | ||
3671 | |||
3672 | ------------------------ | ||
3673 | "##, | ||
3674 | }, | ||
3675 | LintCompletion { | ||
3676 | label: "libstd_sys_internals", | ||
3677 | description: r##"# `libstd_sys_internals` | ||
3678 | |||
3679 | This feature is internal to the Rust compiler and is not intended for general use. | ||
3680 | |||
3681 | ------------------------ | ||
3682 | "##, | ||
3683 | }, | ||
3684 | LintCompletion { | ||
3685 | label: "is_sorted", | ||
3686 | description: r##"# `is_sorted` | ||
3687 | |||
3688 | The tracking issue for this feature is: [#53485] | ||
3689 | |||
3690 | [#53485]: https://github.com/rust-lang/rust/issues/53485 | ||
3691 | |||
3692 | ------------------------ | ||
3693 | |||
3694 | Add the methods `is_sorted`, `is_sorted_by` and `is_sorted_by_key` to `[T]`; | ||
3695 | add the methods `is_sorted`, `is_sorted_by` and `is_sorted_by_key` to | ||
3696 | `Iterator`. | ||
3697 | "##, | ||
3698 | }, | ||
3699 | LintCompletion { | ||
3700 | label: "c_void_variant", | ||
3701 | description: r##"# `c_void_variant` | ||
3702 | |||
3703 | This feature is internal to the Rust compiler and is not intended for general use. | ||
3704 | |||
3705 | ------------------------ | ||
3706 | "##, | ||
3707 | }, | ||
3708 | LintCompletion { | ||
3709 | label: "concat_idents", | ||
3710 | description: r##"# `concat_idents` | ||
3711 | |||
3712 | The tracking issue for this feature is: [#29599] | ||
3713 | |||
3714 | [#29599]: https://github.com/rust-lang/rust/issues/29599 | ||
3715 | |||
3716 | ------------------------ | ||
3717 | |||
3718 | The `concat_idents` feature adds a macro for concatenating multiple identifiers | ||
3719 | into one identifier. | ||
3720 | |||
3721 | ## Examples | ||
3722 | |||
3723 | ```rust | ||
3724 | #![feature(concat_idents)] | ||
3725 | |||
3726 | fn main() { | ||
3727 | fn foobar() -> u32 { 23 } | ||
3728 | let f = concat_idents!(foo, bar); | ||
3729 | assert_eq!(f(), 23); | ||
3730 | } | ||
3731 | ``` | ||
3732 | "##, | ||
3733 | }, | ||
3734 | LintCompletion { | ||
3735 | label: "format_args_capture", | ||
3736 | description: r##"# `format_args_capture` | ||
3737 | |||
3738 | The tracking issue for this feature is: [#67984] | ||
3739 | |||
3740 | [#67984]: https://github.com/rust-lang/rust/issues/67984 | ||
3741 | |||
3742 | ------------------------ | ||
3743 | |||
3744 | Enables `format_args!` (and macros which use `format_args!` in their implementation, such | ||
3745 | as `format!`, `print!` and `panic!`) to capture variables from the surrounding scope. | ||
3746 | This avoids the need to pass named parameters when the binding in question | ||
3747 | already exists in scope. | ||
3748 | |||
3749 | ```rust | ||
3750 | #![feature(format_args_capture)] | ||
3751 | |||
3752 | let (person, species, name) = ("Charlie Brown", "dog", "Snoopy"); | ||
3753 | |||
3754 | // captures named argument `person` | ||
3755 | print!("Hello {person}"); | ||
3756 | |||
3757 | // captures named arguments `species` and `name` | ||
3758 | format!("The {species}'s name is {name}."); | ||
3759 | ``` | ||
3760 | |||
3761 | This also works for formatting parameters such as width and precision: | ||
3762 | |||
3763 | ```rust | ||
3764 | #![feature(format_args_capture)] | ||
3765 | |||
3766 | let precision = 2; | ||
3767 | let s = format!("{:.precision$}", 1.324223); | ||
3768 | |||
3769 | assert_eq!(&s, "1.32"); | ||
3770 | ``` | ||
3771 | |||
3772 | A non-exhaustive list of macros which benefit from this functionality include: | ||
3773 | - `format!` | ||
3774 | - `print!` and `println!` | ||
3775 | - `eprint!` and `eprintln!` | ||
3776 | - `write!` and `writeln!` | ||
3777 | - `panic!` | ||
3778 | - `unreachable!` | ||
3779 | - `unimplemented!` | ||
3780 | - `todo!` | ||
3781 | - `assert!` and similar | ||
3782 | - macros in many thirdparty crates, such as `log` | ||
3783 | "##, | ||
3784 | }, | ||
3785 | LintCompletion { | ||
3786 | label: "print_internals", | ||
3787 | description: r##"# `print_internals` | ||
3788 | |||
3789 | This feature is internal to the Rust compiler and is not intended for general use. | ||
3790 | |||
3791 | ------------------------ | ||
3792 | "##, | ||
3793 | }, | ||
3794 | LintCompletion { | ||
3795 | label: "llvm_asm", | ||
3796 | description: r##"# `llvm_asm` | ||
3797 | |||
3798 | The tracking issue for this feature is: [#70173] | ||
3799 | |||
3800 | [#70173]: https://github.com/rust-lang/rust/issues/70173 | ||
3801 | |||
3802 | ------------------------ | ||
3803 | |||
3804 | For extremely low-level manipulations and performance reasons, one | ||
3805 | might wish to control the CPU directly. Rust supports using inline | ||
3806 | assembly to do this via the `llvm_asm!` macro. | ||
3807 | |||
3808 | ```rust,ignore (pseudo-code) | ||
3809 | llvm_asm!(assembly template | ||
3810 | : output operands | ||
3811 | : input operands | ||
3812 | : clobbers | ||
3813 | : options | ||
3814 | ); | ||
3815 | ``` | ||
3816 | |||
3817 | Any use of `llvm_asm` is feature gated (requires `#![feature(llvm_asm)]` on the | ||
3818 | crate to allow) and of course requires an `unsafe` block. | ||
3819 | |||
3820 | > **Note**: the examples here are given in x86/x86-64 assembly, but | ||
3821 | > all platforms are supported. | ||
3822 | |||
3823 | ## Assembly template | ||
3824 | |||
3825 | The `assembly template` is the only required parameter and must be a | ||
3826 | literal string (i.e. `""`) | ||
3827 | |||
3828 | ```rust | ||
3829 | #![feature(llvm_asm)] | ||
3830 | |||
3831 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] | ||
3832 | fn foo() { | ||
3833 | unsafe { | ||
3834 | llvm_asm!("NOP"); | ||
3835 | } | ||
3836 | } | ||
3837 | |||
3838 | // Other platforms: | ||
3839 | #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))] | ||
3840 | fn foo() { /* ... */ } | ||
3841 | |||
3842 | fn main() { | ||
3843 | // ... | ||
3844 | foo(); | ||
3845 | // ... | ||
3846 | } | ||
3847 | ``` | ||
3848 | |||
3849 | (The `feature(llvm_asm)` and `#[cfg]`s are omitted from now on.) | ||
3850 | |||
3851 | Output operands, input operands, clobbers and options are all optional | ||
3852 | but you must add the right number of `:` if you skip them: | ||
3853 | |||
3854 | ```rust | ||
3855 | # #![feature(llvm_asm)] | ||
3856 | # #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] | ||
3857 | # fn main() { unsafe { | ||
3858 | llvm_asm!("xor %eax, %eax" | ||
3859 | : | ||
3860 | : | ||
3861 | : "eax" | ||
3862 | ); | ||
3863 | # } } | ||
3864 | # #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))] | ||
3865 | # fn main() {} | ||
3866 | ``` | ||
3867 | |||
3868 | Whitespace also doesn't matter: | ||
3869 | |||
3870 | ```rust | ||
3871 | # #![feature(llvm_asm)] | ||
3872 | # #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] | ||
3873 | # fn main() { unsafe { | ||
3874 | llvm_asm!("xor %eax, %eax" ::: "eax"); | ||
3875 | # } } | ||
3876 | # #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))] | ||
3877 | # fn main() {} | ||
3878 | ``` | ||
3879 | |||
3880 | ## Operands | ||
3881 | |||
3882 | Input and output operands follow the same format: `: | ||
3883 | "constraints1"(expr1), "constraints2"(expr2), ..."`. Output operand | ||
3884 | expressions must be mutable place, or not yet assigned: | ||
3885 | |||
3886 | ```rust | ||
3887 | # #![feature(llvm_asm)] | ||
3888 | # #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] | ||
3889 | fn add(a: i32, b: i32) -> i32 { | ||
3890 | let c: i32; | ||
3891 | unsafe { | ||
3892 | llvm_asm!("add $2, $0" | ||
3893 | : "=r"(c) | ||
3894 | : "0"(a), "r"(b) | ||
3895 | ); | ||
3896 | } | ||
3897 | c | ||
3898 | } | ||
3899 | # #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))] | ||
3900 | # fn add(a: i32, b: i32) -> i32 { a + b } | ||
3901 | |||
3902 | fn main() { | ||
3903 | assert_eq!(add(3, 14159), 14162) | ||
3904 | } | ||
3905 | ``` | ||
3906 | |||
3907 | If you would like to use real operands in this position, however, | ||
3908 | you are required to put curly braces `{}` around the register that | ||
3909 | you want, and you are required to put the specific size of the | ||
3910 | operand. This is useful for very low level programming, where | ||
3911 | which register you use is important: | ||
3912 | |||
3913 | ```rust | ||
3914 | # #![feature(llvm_asm)] | ||
3915 | # #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] | ||
3916 | # unsafe fn read_byte_in(port: u16) -> u8 { | ||
3917 | let result: u8; | ||
3918 | llvm_asm!("in %dx, %al" : "={al}"(result) : "{dx}"(port)); | ||
3919 | result | ||
3920 | # } | ||
3921 | ``` | ||
3922 | |||
3923 | ## Clobbers | ||
3924 | |||
3925 | Some instructions modify registers which might otherwise have held | ||
3926 | different values so we use the clobbers list to indicate to the | ||
3927 | compiler not to assume any values loaded into those registers will | ||
3928 | stay valid. | ||
3929 | |||
3930 | ```rust | ||
3931 | # #![feature(llvm_asm)] | ||
3932 | # #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] | ||
3933 | # fn main() { unsafe { | ||
3934 | // Put the value 0x200 in eax: | ||
3935 | llvm_asm!("mov $$0x200, %eax" : /* no outputs */ : /* no inputs */ : "eax"); | ||
3936 | # } } | ||
3937 | # #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))] | ||
3938 | # fn main() {} | ||
3939 | ``` | ||
3940 | |||
3941 | Input and output registers need not be listed since that information | ||
3942 | is already communicated by the given constraints. Otherwise, any other | ||
3943 | registers used either implicitly or explicitly should be listed. | ||
3944 | |||
3945 | If the assembly changes the condition code register `cc` should be | ||
3946 | specified as one of the clobbers. Similarly, if the assembly modifies | ||
3947 | memory, `memory` should also be specified. | ||
3948 | |||
3949 | ## Options | ||
3950 | |||
3951 | The last section, `options` is specific to Rust. The format is comma | ||
3952 | separated literal strings (i.e. `:"foo", "bar", "baz"`). It's used to | ||
3953 | specify some extra info about the inline assembly: | ||
3954 | |||
3955 | Current valid options are: | ||
3956 | |||
3957 | 1. `volatile` - specifying this is analogous to | ||
3958 | `__asm__ __volatile__ (...)` in gcc/clang. | ||
3959 | 2. `alignstack` - certain instructions expect the stack to be | ||
3960 | aligned a certain way (i.e. SSE) and specifying this indicates to | ||
3961 | the compiler to insert its usual stack alignment code | ||
3962 | 3. `intel` - use intel syntax instead of the default AT&T. | ||
3963 | |||
3964 | ```rust | ||
3965 | # #![feature(llvm_asm)] | ||
3966 | # #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] | ||
3967 | # fn main() { | ||
3968 | let result: i32; | ||
3969 | unsafe { | ||
3970 | llvm_asm!("mov eax, 2" : "={eax}"(result) : : : "intel") | ||
3971 | } | ||
3972 | println!("eax is currently {}", result); | ||
3973 | # } | ||
3974 | # #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))] | ||
3975 | # fn main() {} | ||
3976 | ``` | ||
3977 | |||
3978 | ## More Information | ||
3979 | |||
3980 | The current implementation of the `llvm_asm!` macro is a direct binding to [LLVM's | ||
3981 | inline assembler expressions][llvm-docs], so be sure to check out [their | ||
3982 | documentation as well][llvm-docs] for more information about clobbers, | ||
3983 | constraints, etc. | ||
3984 | |||
3985 | [llvm-docs]: http://llvm.org/docs/LangRef.html#inline-assembler-expressions | ||
3986 | |||
3987 | If you need more power and don't mind losing some of the niceties of | ||
3988 | `llvm_asm!`, check out [global_asm](global-asm.md). | ||
3989 | "##, | ||
3990 | }, | ||
3991 | LintCompletion { | ||
3992 | label: "core_intrinsics", | ||
3993 | description: r##"# `core_intrinsics` | ||
3994 | |||
3995 | This feature is internal to the Rust compiler and is not intended for general use. | ||
3996 | |||
3997 | ------------------------ | ||
3998 | "##, | ||
3999 | }, | ||
4000 | LintCompletion { | ||
4001 | label: "trace_macros", | ||
4002 | description: r##"# `trace_macros` | ||
4003 | |||
4004 | The tracking issue for this feature is [#29598]. | ||
4005 | |||
4006 | [#29598]: https://github.com/rust-lang/rust/issues/29598 | ||
4007 | |||
4008 | ------------------------ | ||
4009 | |||
4010 | With `trace_macros` you can trace the expansion of macros in your code. | ||
4011 | |||
4012 | ## Examples | ||
4013 | |||
4014 | ```rust | ||
4015 | #![feature(trace_macros)] | ||
4016 | |||
4017 | fn main() { | ||
4018 | trace_macros!(true); | ||
4019 | println!("Hello, Rust!"); | ||
4020 | trace_macros!(false); | ||
4021 | } | ||
4022 | ``` | ||
4023 | |||
4024 | The `cargo build` output: | ||
4025 | |||
4026 | ```txt | ||
4027 | note: trace_macro | ||
4028 | --> src/main.rs:5:5 | ||
4029 | | | ||
4030 | 5 | println!("Hello, Rust!"); | ||
4031 | | ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
4032 | | | ||
4033 | = note: expanding `println! { "Hello, Rust!" }` | ||
4034 | = note: to `print ! ( concat ! ( "Hello, Rust!" , "\n" ) )` | ||
4035 | = note: expanding `print! { concat ! ( "Hello, Rust!" , "\n" ) }` | ||
4036 | = note: to `$crate :: io :: _print ( format_args ! ( concat ! ( "Hello, Rust!" , "\n" ) ) | ||
4037 | )` | ||
4038 | |||
4039 | Finished dev [unoptimized + debuginfo] target(s) in 0.60 secs | ||
4040 | ``` | ||
4041 | "##, | ||
4042 | }, | ||
4043 | LintCompletion { | ||
4044 | label: "update_panic_count", | ||
4045 | description: r##"# `update_panic_count` | ||
4046 | |||
4047 | This feature is internal to the Rust compiler and is not intended for general use. | ||
4048 | |||
4049 | ------------------------ | ||
4050 | "##, | ||
4051 | }, | ||
4052 | LintCompletion { | ||
4053 | label: "core_private_bignum", | ||
4054 | description: r##"# `core_private_bignum` | ||
4055 | |||
4056 | This feature is internal to the Rust compiler and is not intended for general use. | ||
4057 | |||
4058 | ------------------------ | ||
4059 | "##, | ||
4060 | }, | ||
4061 | LintCompletion { | ||
4062 | label: "sort_internals", | ||
4063 | description: r##"# `sort_internals` | ||
4064 | |||
4065 | This feature is internal to the Rust compiler and is not intended for general use. | ||
4066 | |||
4067 | ------------------------ | ||
4068 | "##, | ||
4069 | }, | ||
4070 | LintCompletion { | ||
4071 | label: "windows_net", | ||
4072 | description: r##"# `windows_net` | ||
4073 | |||
4074 | This feature is internal to the Rust compiler and is not intended for general use. | ||
4075 | |||
4076 | ------------------------ | ||
4077 | "##, | ||
4078 | }, | ||
4079 | LintCompletion { | ||
4080 | label: "c_variadic", | ||
4081 | description: r##"# `c_variadic` | ||
4082 | |||
4083 | The tracking issue for this feature is: [#44930] | ||
4084 | |||
4085 | [#44930]: https://github.com/rust-lang/rust/issues/44930 | ||
4086 | |||
4087 | ------------------------ | ||
4088 | |||
4089 | The `c_variadic` library feature exposes the `VaList` structure, | ||
4090 | Rust's analogue of C's `va_list` type. | ||
4091 | |||
4092 | ## Examples | ||
4093 | |||
4094 | ```rust | ||
4095 | #![feature(c_variadic)] | ||
4096 | |||
4097 | use std::ffi::VaList; | ||
4098 | |||
4099 | pub unsafe extern "C" fn vadd(n: usize, mut args: VaList) -> usize { | ||
4100 | let mut sum = 0; | ||
4101 | for _ in 0..n { | ||
4102 | sum += args.arg::<usize>(); | ||
4103 | } | ||
4104 | sum | ||
4105 | } | ||
4106 | ``` | ||
4107 | "##, | ||
4108 | }, | ||
4109 | LintCompletion { | ||
4110 | label: "core_private_diy_float", | ||
4111 | description: r##"# `core_private_diy_float` | ||
4112 | |||
4113 | This feature is internal to the Rust compiler and is not intended for general use. | ||
4114 | |||
4115 | ------------------------ | ||
4116 | "##, | ||
4117 | }, | ||
4118 | LintCompletion { | ||
4119 | label: "profiler_runtime_lib", | ||
4120 | description: r##"# `profiler_runtime_lib` | ||
4121 | |||
4122 | This feature is internal to the Rust compiler and is not intended for general use. | ||
4123 | |||
4124 | ------------------------ | ||
4125 | "##, | ||
4126 | }, | ||
4127 | LintCompletion { | ||
4128 | label: "thread_local_internals", | ||
4129 | description: r##"# `thread_local_internals` | ||
4130 | |||
4131 | This feature is internal to the Rust compiler and is not intended for general use. | ||
4132 | |||
4133 | ------------------------ | ||
4134 | "##, | ||
4135 | }, | ||
4136 | LintCompletion { | ||
4137 | label: "int_error_internals", | ||
4138 | description: r##"# `int_error_internals` | ||
4139 | |||
4140 | This feature is internal to the Rust compiler and is not intended for general use. | ||
4141 | |||
4142 | ------------------------ | ||
4143 | "##, | ||
4144 | }, | ||
4145 | LintCompletion { | ||
4146 | label: "windows_stdio", | ||
4147 | description: r##"# `windows_stdio` | ||
4148 | |||
4149 | This feature is internal to the Rust compiler and is not intended for general use. | ||
4150 | |||
4151 | ------------------------ | ||
4152 | "##, | ||
4153 | }, | ||
4154 | LintCompletion { | ||
4155 | label: "fmt_internals", | ||
4156 | description: r##"# `fmt_internals` | ||
4157 | |||
4158 | This feature is internal to the Rust compiler and is not intended for general use. | ||
4159 | |||
4160 | ------------------------ | ||
4161 | "##, | ||
4162 | }, | ||
4163 | LintCompletion { | ||
4164 | label: "fd_read", | ||
4165 | description: r##"# `fd_read` | ||
4166 | |||
4167 | This feature is internal to the Rust compiler and is not intended for general use. | ||
4168 | |||
4169 | ------------------------ | ||
4170 | "##, | ||
4171 | }, | ||
4172 | LintCompletion { | ||
4173 | label: "str_internals", | ||
4174 | description: r##"# `str_internals` | ||
4175 | |||
4176 | This feature is internal to the Rust compiler and is not intended for general use. | ||
4177 | |||
4178 | ------------------------ | ||
4179 | "##, | ||
4180 | }, | ||
4181 | LintCompletion { | ||
4182 | label: "test", | ||
4183 | description: r##"# `test` | ||
4184 | |||
4185 | The tracking issue for this feature is: None. | ||
4186 | |||
4187 | ------------------------ | ||
4188 | |||
4189 | The internals of the `test` crate are unstable, behind the `test` flag. The | ||
4190 | most widely used part of the `test` crate are benchmark tests, which can test | ||
4191 | the performance of your code. Let's make our `src/lib.rs` look like this | ||
4192 | (comments elided): | ||
4193 | |||
4194 | ```rust,no_run | ||
4195 | #![feature(test)] | ||
4196 | |||
4197 | extern crate test; | ||
4198 | |||
4199 | pub fn add_two(a: i32) -> i32 { | ||
4200 | a + 2 | ||
4201 | } | ||
4202 | |||
4203 | #[cfg(test)] | ||
4204 | mod tests { | ||
4205 | use super::*; | ||
4206 | use test::Bencher; | ||
4207 | |||
4208 | #[test] | ||
4209 | fn it_works() { | ||
4210 | assert_eq!(4, add_two(2)); | ||
4211 | } | ||
4212 | |||
4213 | #[bench] | ||
4214 | fn bench_add_two(b: &mut Bencher) { | ||
4215 | b.iter(|| add_two(2)); | ||
4216 | } | ||
4217 | } | ||
4218 | ``` | ||
4219 | |||
4220 | Note the `test` feature gate, which enables this unstable feature. | ||
4221 | |||
4222 | We've imported the `test` crate, which contains our benchmarking support. | ||
4223 | We have a new function as well, with the `bench` attribute. Unlike regular | ||
4224 | tests, which take no arguments, benchmark tests take a `&mut Bencher`. This | ||
4225 | `Bencher` provides an `iter` method, which takes a closure. This closure | ||
4226 | contains the code we'd like to benchmark. | ||
4227 | |||
4228 | We can run benchmark tests with `cargo bench`: | ||
4229 | |||
4230 | ```bash | ||
4231 | $ cargo bench | ||
4232 | Compiling adder v0.0.1 (file:///home/steve/tmp/adder) | ||
4233 | Running target/release/adder-91b3e234d4ed382a | ||
4234 | |||
4235 | running 2 tests | ||
4236 | test tests::it_works ... ignored | ||
4237 | test tests::bench_add_two ... bench: 1 ns/iter (+/- 0) | ||
4238 | |||
4239 | test result: ok. 0 passed; 0 failed; 1 ignored; 1 measured | ||
4240 | ``` | ||
4241 | |||
4242 | Our non-benchmark test was ignored. You may have noticed that `cargo bench` | ||
4243 | takes a bit longer than `cargo test`. This is because Rust runs our benchmark | ||
4244 | a number of times, and then takes the average. Because we're doing so little | ||
4245 | work in this example, we have a `1 ns/iter (+/- 0)`, but this would show | ||
4246 | the variance if there was one. | ||
4247 | |||
4248 | Advice on writing benchmarks: | ||
4249 | |||
4250 | |||
4251 | * Move setup code outside the `iter` loop; only put the part you want to measure inside | ||
4252 | * Make the code do "the same thing" on each iteration; do not accumulate or change state | ||
4253 | * Make the outer function idempotent too; the benchmark runner is likely to run | ||
4254 | it many times | ||
4255 | * Make the inner `iter` loop short and fast so benchmark runs are fast and the | ||
4256 | calibrator can adjust the run-length at fine resolution | ||
4257 | * Make the code in the `iter` loop do something simple, to assist in pinpointing | ||
4258 | performance improvements (or regressions) | ||
4259 | |||
4260 | ## Gotcha: optimizations | ||
4261 | |||
4262 | There's another tricky part to writing benchmarks: benchmarks compiled with | ||
4263 | optimizations activated can be dramatically changed by the optimizer so that | ||
4264 | the benchmark is no longer benchmarking what one expects. For example, the | ||
4265 | compiler might recognize that some calculation has no external effects and | ||
4266 | remove it entirely. | ||
4267 | |||
4268 | ```rust,no_run | ||
4269 | #![feature(test)] | ||
4270 | |||
4271 | extern crate test; | ||
4272 | use test::Bencher; | ||
4273 | |||
4274 | #[bench] | ||
4275 | fn bench_xor_1000_ints(b: &mut Bencher) { | ||
4276 | b.iter(|| { | ||
4277 | (0..1000).fold(0, |old, new| old ^ new); | ||
4278 | }); | ||
4279 | } | ||
4280 | ``` | ||
4281 | |||
4282 | gives the following results | ||
4283 | |||
4284 | ```text | ||
4285 | running 1 test | ||
4286 | test bench_xor_1000_ints ... bench: 0 ns/iter (+/- 0) | ||
4287 | |||
4288 | test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured | ||
4289 | ``` | ||
4290 | |||
4291 | The benchmarking runner offers two ways to avoid this. Either, the closure that | ||
4292 | the `iter` method receives can return an arbitrary value which forces the | ||
4293 | optimizer to consider the result used and ensures it cannot remove the | ||
4294 | computation entirely. This could be done for the example above by adjusting the | ||
4295 | `b.iter` call to | ||
4296 | |||
4297 | ```rust | ||
4298 | # struct X; | ||
4299 | # impl X { fn iter<T, F>(&self, _: F) where F: FnMut() -> T {} } let b = X; | ||
4300 | b.iter(|| { | ||
4301 | // Note lack of `;` (could also use an explicit `return`). | ||
4302 | (0..1000).fold(0, |old, new| old ^ new) | ||
4303 | }); | ||
4304 | ``` | ||
4305 | |||
4306 | Or, the other option is to call the generic `test::black_box` function, which | ||
4307 | is an opaque "black box" to the optimizer and so forces it to consider any | ||
4308 | argument as used. | ||
4309 | |||
4310 | ```rust | ||
4311 | #![feature(test)] | ||
4312 | |||
4313 | extern crate test; | ||
4314 | |||
4315 | # fn main() { | ||
4316 | # struct X; | ||
4317 | # impl X { fn iter<T, F>(&self, _: F) where F: FnMut() -> T {} } let b = X; | ||
4318 | b.iter(|| { | ||
4319 | let n = test::black_box(1000); | ||
4320 | |||
4321 | (0..n).fold(0, |a, b| a ^ b) | ||
4322 | }) | ||
4323 | # } | ||
4324 | ``` | ||
4325 | |||
4326 | Neither of these read or modify the value, and are very cheap for small values. | ||
4327 | Larger values can be passed indirectly to reduce overhead (e.g. | ||
4328 | `black_box(&huge_struct)`). | ||
4329 | |||
4330 | Performing either of the above changes gives the following benchmarking results | ||
4331 | |||
4332 | ```text | ||
4333 | running 1 test | ||
4334 | test bench_xor_1000_ints ... bench: 131 ns/iter (+/- 3) | ||
4335 | |||
4336 | test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured | ||
4337 | ``` | ||
4338 | |||
4339 | However, the optimizer can still modify a testcase in an undesirable manner | ||
4340 | even when using either of the above. | ||
4341 | "##, | ||
4342 | }, | ||
4343 | LintCompletion { | ||
4344 | label: "windows_c", | ||
4345 | description: r##"# `windows_c` | ||
4346 | |||
4347 | This feature is internal to the Rust compiler and is not intended for general use. | ||
4348 | |||
4349 | ------------------------ | ||
4350 | "##, | ||
4351 | }, | ||
4352 | LintCompletion { | ||
4353 | label: "dec2flt", | ||
4354 | description: r##"# `dec2flt` | ||
4355 | |||
4356 | This feature is internal to the Rust compiler and is not intended for general use. | ||
4357 | |||
4358 | ------------------------ | ||
4359 | "##, | ||
4360 | }, | ||
4361 | LintCompletion { | ||
4362 | label: "derive_clone_copy", | ||
4363 | description: r##"# `derive_clone_copy` | ||
4364 | |||
4365 | This feature is internal to the Rust compiler and is not intended for general use. | ||
4366 | |||
4367 | ------------------------ | ||
4368 | "##, | ||
4369 | }, | ||
4370 | LintCompletion { | ||
4371 | label: "allocator_api", | ||
4372 | description: r##"# `allocator_api` | ||
4373 | |||
4374 | The tracking issue for this feature is [#32838] | ||
4375 | |||
4376 | [#32838]: https://github.com/rust-lang/rust/issues/32838 | ||
4377 | |||
4378 | ------------------------ | ||
4379 | |||
4380 | Sometimes you want the memory for one collection to use a different | ||
4381 | allocator than the memory for another collection. In this case, | ||
4382 | replacing the global allocator is not a workable option. Instead, | ||
4383 | you need to pass in an instance of an `AllocRef` to each collection | ||
4384 | for which you want a custom allocator. | ||
4385 | |||
4386 | TBD | ||
4387 | "##, | ||
4388 | }, | ||
4389 | LintCompletion { | ||
4390 | label: "core_panic", | ||
4391 | description: r##"# `core_panic` | ||
4392 | |||
4393 | This feature is internal to the Rust compiler and is not intended for general use. | ||
4394 | |||
4395 | ------------------------ | ||
4396 | "##, | ||
4397 | }, | ||
4398 | LintCompletion { | ||
4399 | label: "fn_traits", | ||
4400 | description: r##"# `fn_traits` | ||
4401 | |||
4402 | The tracking issue for this feature is [#29625] | ||
4403 | |||
4404 | See Also: [`unboxed_closures`](../language-features/unboxed-closures.md) | ||
4405 | |||
4406 | [#29625]: https://github.com/rust-lang/rust/issues/29625 | ||
4407 | |||
4408 | ---- | ||
4409 | |||
4410 | The `fn_traits` feature allows for implementation of the [`Fn*`] traits | ||
4411 | for creating custom closure-like types. | ||
4412 | |||
4413 | [`Fn*`]: https://doc.rust-lang.org/std/ops/trait.Fn.html | ||
4414 | |||
4415 | ```rust | ||
4416 | #![feature(unboxed_closures)] | ||
4417 | #![feature(fn_traits)] | ||
4418 | |||
4419 | struct Adder { | ||
4420 | a: u32 | ||
4421 | } | ||
4422 | |||
4423 | impl FnOnce<(u32, )> for Adder { | ||
4424 | type Output = u32; | ||
4425 | extern "rust-call" fn call_once(self, b: (u32, )) -> Self::Output { | ||
4426 | self.a + b.0 | ||
4427 | } | ||
4428 | } | ||
4429 | |||
4430 | fn main() { | ||
4431 | let adder = Adder { a: 3 }; | ||
4432 | assert_eq!(adder(2), 5); | ||
4433 | } | ||
4434 | ``` | ||
4435 | "##, | ||
4436 | }, | ||
4437 | LintCompletion { | ||
4438 | label: "try_trait", | ||
4439 | description: r##"# `try_trait` | ||
4440 | |||
4441 | The tracking issue for this feature is: [#42327] | ||
4442 | |||
4443 | [#42327]: https://github.com/rust-lang/rust/issues/42327 | ||
4444 | |||
4445 | ------------------------ | ||
4446 | |||
4447 | This introduces a new trait `Try` for extending the `?` operator to types | ||
4448 | other than `Result` (a part of [RFC 1859]). The trait provides the canonical | ||
4449 | way to _view_ a type in terms of a success/failure dichotomy. This will | ||
4450 | allow `?` to supplant the `try_opt!` macro on `Option` and the `try_ready!` | ||
4451 | macro on `Poll`, among other things. | ||
4452 | |||
4453 | [RFC 1859]: https://github.com/rust-lang/rfcs/pull/1859 | ||
4454 | |||
4455 | Here's an example implementation of the trait: | ||
4456 | |||
4457 | ```rust,ignore (cannot-reimpl-Try) | ||
4458 | /// A distinct type to represent the `None` value of an `Option`. | ||
4459 | /// | ||
4460 | /// This enables using the `?` operator on `Option`; it's rarely useful alone. | ||
4461 | #[derive(Debug)] | ||
4462 | #[unstable(feature = "try_trait", issue = "42327")] | ||
4463 | pub struct None { _priv: () } | ||
4464 | |||
4465 | #[unstable(feature = "try_trait", issue = "42327")] | ||
4466 | impl<T> ops::Try for Option<T> { | ||
4467 | type Ok = T; | ||
4468 | type Error = None; | ||
4469 | |||
4470 | fn into_result(self) -> Result<T, None> { | ||
4471 | self.ok_or(None { _priv: () }) | ||
4472 | } | ||
4473 | |||
4474 | fn from_ok(v: T) -> Self { | ||
4475 | Some(v) | ||
4476 | } | ||
4477 | |||
4478 | fn from_error(_: None) -> Self { | ||
4479 | None | ||
4480 | } | ||
4481 | } | ||
4482 | ``` | ||
4483 | |||
4484 | Note the `Error` associated type here is a new marker. The `?` operator | ||
4485 | allows interconversion between different `Try` implementers only when | ||
4486 | the error type can be converted `Into` the error type of the enclosing | ||
4487 | function (or catch block). Having a distinct error type (as opposed to | ||
4488 | just `()`, or similar) restricts this to where it's semantically meaningful. | ||
4489 | "##, | ||
4490 | }, | ||
4491 | LintCompletion { | ||
4492 | label: "rt", | ||
4493 | description: r##"# `rt` | ||
4494 | |||
4495 | This feature is internal to the Rust compiler and is not intended for general use. | ||
4496 | |||
4497 | ------------------------ | ||
4498 | "##, | ||
4499 | }, | ||
4500 | LintCompletion { | ||
4501 | label: "fd", | ||
4502 | description: r##"# `fd` | ||
4503 | |||
4504 | This feature is internal to the Rust compiler and is not intended for general use. | ||
4505 | |||
4506 | ------------------------ | ||
4507 | "##, | ||
4508 | }, | ||
4509 | LintCompletion { | ||
4510 | label: "libstd_thread_internals", | ||
4511 | description: r##"# `libstd_thread_internals` | ||
4512 | |||
4513 | This feature is internal to the Rust compiler and is not intended for general use. | ||
4514 | |||
4515 | ------------------------ | ||
4516 | "##, | ||
4517 | }, | ||
4518 | ]; | ||
4519 | |||
4520 | pub(super) const CLIPPY_LINTS: &[LintCompletion] = &[ | ||
4521 | LintCompletion { | ||
4522 | label: "clippy::absurd_extreme_comparisons", | ||
4523 | description: r##"Checks for comparisons where one side of the relation is\neither the minimum or maximum value for its type and warns if it involves a\ncase that is always true or always false. Only integer and boolean types are\nchecked."##, | ||
4524 | }, | ||
4525 | LintCompletion { | ||
4526 | label: "clippy::almost_swapped", | ||
4527 | description: r##"Checks for `foo = bar; bar = foo` sequences."##, | ||
4528 | }, | ||
4529 | LintCompletion { | ||
4530 | label: "clippy::approx_constant", | ||
4531 | description: r##"Checks for floating point literals that approximate\nconstants which are defined in\n[`std::f32::consts`](https://doc.rust-lang.org/stable/std/f32/consts/#constants)\nor\n[`std::f64::consts`](https://doc.rust-lang.org/stable/std/f64/consts/#constants),\nrespectively, suggesting to use the predefined constant."##, | ||
4532 | }, | ||
4533 | LintCompletion { | ||
4534 | label: "clippy::as_conversions", | ||
4535 | description: r##"Checks for usage of `as` conversions.\n\nNote that this lint is specialized in linting *every single* use of `as`\nregardless of whether good alternatives exist or not.\nIf you want more precise lints for `as`, please consider using these separate lints:\n`unnecessary_cast`, `cast_lossless/possible_truncation/possible_wrap/precision_loss/sign_loss`,\n`fn_to_numeric_cast(_with_truncation)`, `char_lit_as_u8`, `ref_to_mut` and `ptr_as_ptr`.\nThere is a good explanation the reason why this lint should work in this way and how it is useful\n[in this issue](https://github.com/rust-lang/rust-clippy/issues/5122)."##, | ||
4536 | }, | ||
4537 | LintCompletion { | ||
4538 | label: "clippy::assertions_on_constants", | ||
4539 | description: r##"Checks for `assert!(true)` and `assert!(false)` calls."##, | ||
4540 | }, | ||
4541 | LintCompletion { | ||
4542 | label: "clippy::assign_op_pattern", | ||
4543 | description: r##"Checks for `a = a op b` or `a = b commutative_op a`\npatterns."##, | ||
4544 | }, | ||
4545 | LintCompletion { | ||
4546 | label: "clippy::assign_ops", | ||
4547 | description: r##"Nothing. This lint has been deprecated."##, | ||
4548 | }, | ||
4549 | LintCompletion { | ||
4550 | label: "clippy::async_yields_async", | ||
4551 | description: r##"Checks for async blocks that yield values of types\nthat can themselves be awaited."##, | ||
4552 | }, | ||
4553 | LintCompletion { | ||
4554 | label: "clippy::await_holding_lock", | ||
4555 | description: r##"Checks for calls to await while holding a\nnon-async-aware MutexGuard."##, | ||
4556 | }, | ||
4557 | LintCompletion { | ||
4558 | label: "clippy::await_holding_refcell_ref", | ||
4559 | description: r##"Checks for calls to await while holding a\n`RefCell` `Ref` or `RefMut`."##, | ||
4560 | }, | ||
4561 | LintCompletion { | ||
4562 | label: "clippy::bad_bit_mask", | ||
4563 | description: r##"Checks for incompatible bit masks in comparisons.\n\nThe formula for detecting if an expression of the type `_ <bit_op> m\n<cmp_op> c` (where `<bit_op>` is one of {`&`, `|`} and `<cmp_op>` is one of\n{`!=`, `>=`, `>`, `!=`, `>=`, `>`}) can be determined from the following\ntable:\n\n|Comparison |Bit Op|Example |is always|Formula |\n|------------|------|------------|---------|----------------------|\n|`==` or `!=`| `&` |`x & 2 == 3`|`false` |`c & m != c` |\n|`<` or `>=`| `&` |`x & 2 < 3` |`true` |`m < c` |\n|`>` or `<=`| `&` |`x & 1 > 1` |`false` |`m <= c` |\n|`==` or `!=`| `|` |`x | 1 == 0`|`false` |`c | m != c` |\n|`<` or `>=`| `|` |`x | 1 < 1` |`false` |`m >= c` |\n|`<=` or `>` | `|` |`x | 1 > 0` |`true` |`m > c` |"##, | ||
4564 | }, | ||
4565 | LintCompletion { | ||
4566 | label: "clippy::bind_instead_of_map", | ||
4567 | description: r##"Checks for usage of `_.and_then(|x| Some(y))`, `_.and_then(|x| Ok(y))` or\n`_.or_else(|x| Err(y))`."##, | ||
4568 | }, | ||
4569 | LintCompletion { | ||
4570 | label: "clippy::blacklisted_name", | ||
4571 | description: r##"Checks for usage of blacklisted names for variables, such\nas `foo`."##, | ||
4572 | }, | ||
4573 | LintCompletion { | ||
4574 | label: "clippy::blanket_clippy_restriction_lints", | ||
4575 | description: r##"Checks for `warn`/`deny`/`forbid` attributes targeting the whole clippy::restriction category."##, | ||
4576 | }, | ||
4577 | LintCompletion { | ||
4578 | label: "clippy::blocks_in_if_conditions", | ||
4579 | description: r##"Checks for `if` conditions that use blocks containing an\nexpression, statements or conditions that use closures with blocks."##, | ||
4580 | }, | ||
4581 | LintCompletion { | ||
4582 | label: "clippy::bool_comparison", | ||
4583 | description: r##"Checks for expressions of the form `x == true`,\n`x != true` and order comparisons such as `x < true` (or vice versa) and\nsuggest using the variable directly."##, | ||
4584 | }, | ||
4585 | LintCompletion { | ||
4586 | label: "clippy::borrow_interior_mutable_const", | ||
4587 | description: r##"Checks if `const` items which is interior mutable (e.g.,\ncontains a `Cell`, `Mutex`, `AtomicXxxx`, etc.) has been borrowed directly."##, | ||
4588 | }, | ||
4589 | LintCompletion { | ||
4590 | label: "clippy::borrowed_box", | ||
4591 | description: r##"Checks for use of `&Box<T>` anywhere in the code.\nCheck the [Box documentation](https://doc.rust-lang.org/std/boxed/index.html) for more information."##, | ||
4592 | }, | ||
4593 | LintCompletion { | ||
4594 | label: "clippy::box_vec", | ||
4595 | description: r##"Checks for use of `Box<Vec<_>>` anywhere in the code.\nCheck the [Box documentation](https://doc.rust-lang.org/std/boxed/index.html) for more information."##, | ||
4596 | }, | ||
4597 | LintCompletion { | ||
4598 | label: "clippy::boxed_local", | ||
4599 | description: r##"Checks for usage of `Box<T>` where an unboxed `T` would\nwork fine."##, | ||
4600 | }, | ||
4601 | LintCompletion { | ||
4602 | label: "clippy::builtin_type_shadow", | ||
4603 | description: r##"Warns if a generic shadows a built-in type."##, | ||
4604 | }, | ||
4605 | LintCompletion { | ||
4606 | label: "clippy::bytes_nth", | ||
4607 | description: r##"Checks for the use of `.bytes().nth()`."##, | ||
4608 | }, | ||
4609 | LintCompletion { | ||
4610 | label: "clippy::cargo_common_metadata", | ||
4611 | description: r##"Checks to see if all common metadata is defined in\n`Cargo.toml`. See: https://rust-lang-nursery.github.io/api-guidelines/documentation.html#cargotoml-includes-all-common-metadata-c-metadata"##, | ||
4612 | }, | ||
4613 | LintCompletion { | ||
4614 | label: "clippy::case_sensitive_file_extension_comparisons", | ||
4615 | description: r##"Checks for calls to `ends_with` with possible file extensions\nand suggests to use a case-insensitive approach instead."##, | ||
4616 | }, | ||
4617 | LintCompletion { | ||
4618 | label: "clippy::cast_lossless", | ||
4619 | description: r##"Checks for casts between numerical types that may\nbe replaced by safe conversion functions."##, | ||
4620 | }, | ||
4621 | LintCompletion { | ||
4622 | label: "clippy::cast_possible_truncation", | ||
4623 | description: r##"Checks for casts between numerical types that may\ntruncate large values. This is expected behavior, so the cast is `Allow` by\ndefault."##, | ||
4624 | }, | ||
4625 | LintCompletion { | ||
4626 | label: "clippy::cast_possible_wrap", | ||
4627 | description: r##"Checks for casts from an unsigned type to a signed type of\nthe same size. Performing such a cast is a 'no-op' for the compiler,\ni.e., nothing is changed at the bit level, and the binary representation of\nthe value is reinterpreted. This can cause wrapping if the value is too big\nfor the target signed type. However, the cast works as defined, so this lint\nis `Allow` by default."##, | ||
4628 | }, | ||
4629 | LintCompletion { | ||
4630 | label: "clippy::cast_precision_loss", | ||
4631 | description: r##"Checks for casts from any numerical to a float type where\nthe receiving type cannot store all values from the original type without\nrounding errors. This possible rounding is to be expected, so this lint is\n`Allow` by default.\n\nBasically, this warns on casting any integer with 32 or more bits to `f32`\nor any 64-bit integer to `f64`."##, | ||
4632 | }, | ||
4633 | LintCompletion { | ||
4634 | label: "clippy::cast_ptr_alignment", | ||
4635 | description: r##"Checks for casts, using `as` or `pointer::cast`,\nfrom a less-strictly-aligned pointer to a more-strictly-aligned pointer"##, | ||
4636 | }, | ||
4637 | LintCompletion { | ||
4638 | label: "clippy::cast_ref_to_mut", | ||
4639 | description: r##"Checks for casts of `&T` to `&mut T` anywhere in the code."##, | ||
4640 | }, | ||
4641 | LintCompletion { | ||
4642 | label: "clippy::cast_sign_loss", | ||
4643 | description: r##"Checks for casts from a signed to an unsigned numerical\ntype. In this case, negative values wrap around to large positive values,\nwhich can be quite surprising in practice. However, as the cast works as\ndefined, this lint is `Allow` by default."##, | ||
4644 | }, | ||
4645 | LintCompletion { | ||
4646 | label: "clippy::char_lit_as_u8", | ||
4647 | description: r##"Checks for expressions where a character literal is cast\nto `u8` and suggests using a byte literal instead."##, | ||
4648 | }, | ||
4649 | LintCompletion { | ||
4650 | label: "clippy::chars_last_cmp", | ||
4651 | description: r##"Checks for usage of `_.chars().last()` or\n`_.chars().next_back()` on a `str` to check if it ends with a given char."##, | ||
4652 | }, | ||
4653 | LintCompletion { | ||
4654 | label: "clippy::chars_next_cmp", | ||
4655 | description: r##"Checks for usage of `.chars().next()` on a `str` to check\nif it starts with a given char."##, | ||
4656 | }, | ||
4657 | LintCompletion { | ||
4658 | label: "clippy::checked_conversions", | ||
4659 | description: r##"Checks for explicit bounds checking when casting."##, | ||
4660 | }, | ||
4661 | LintCompletion { | ||
4662 | label: "clippy::clone_double_ref", | ||
4663 | description: r##"Checks for usage of `.clone()` on an `&&T`."##, | ||
4664 | }, | ||
4665 | LintCompletion { | ||
4666 | label: "clippy::clone_on_copy", | ||
4667 | description: r##"Checks for usage of `.clone()` on a `Copy` type."##, | ||
4668 | }, | ||
4669 | LintCompletion { | ||
4670 | label: "clippy::clone_on_ref_ptr", | ||
4671 | description: r##"Checks for usage of `.clone()` on a ref-counted pointer,\n(`Rc`, `Arc`, `rc::Weak`, or `sync::Weak`), and suggests calling Clone via unified\nfunction syntax instead (e.g., `Rc::clone(foo)`)."##, | ||
4672 | }, | ||
4673 | LintCompletion { label: "clippy::cmp_nan", description: r##"Checks for comparisons to NaN."## }, | ||
4674 | LintCompletion { | ||
4675 | label: "clippy::cmp_null", | ||
4676 | description: r##"This lint checks for equality comparisons with `ptr::null`"##, | ||
4677 | }, | ||
4678 | LintCompletion { | ||
4679 | label: "clippy::cmp_owned", | ||
4680 | description: r##"Checks for conversions to owned values just for the sake\nof a comparison."##, | ||
4681 | }, | ||
4682 | LintCompletion { | ||
4683 | label: "clippy::cognitive_complexity", | ||
4684 | description: r##"Checks for methods with high cognitive complexity."##, | ||
4685 | }, | ||
4686 | LintCompletion { | ||
4687 | label: "clippy::collapsible_else_if", | ||
4688 | description: r##"Checks for collapsible `else { if ... }` expressions\nthat can be collapsed to `else if ...`."##, | ||
4689 | }, | ||
4690 | LintCompletion { | ||
4691 | label: "clippy::collapsible_if", | ||
4692 | description: r##"Checks for nested `if` statements which can be collapsed\nby `&&`-combining their conditions."##, | ||
4693 | }, | ||
4694 | LintCompletion { | ||
4695 | label: "clippy::collapsible_match", | ||
4696 | description: r##"Finds nested `match` or `if let` expressions where the patterns may be \"collapsed\" together\nwithout adding any branches.\n\nNote that this lint is not intended to find _all_ cases where nested match patterns can be merged, but only\ncases where merging would most likely make the code more readable."##, | ||
4697 | }, | ||
4698 | LintCompletion { | ||
4699 | label: "clippy::comparison_chain", | ||
4700 | description: r##"Checks comparison chains written with `if` that can be\nrewritten with `match` and `cmp`."##, | ||
4701 | }, | ||
4702 | LintCompletion { | ||
4703 | label: "clippy::comparison_to_empty", | ||
4704 | description: r##"Checks for comparing to an empty slice such as `\"\"` or `[]`,\nand suggests using `.is_empty()` where applicable."##, | ||
4705 | }, | ||
4706 | LintCompletion { | ||
4707 | label: "clippy::copy_iterator", | ||
4708 | description: r##"Checks for types that implement `Copy` as well as\n`Iterator`."##, | ||
4709 | }, | ||
4710 | LintCompletion { | ||
4711 | label: "clippy::create_dir", | ||
4712 | description: r##"Checks usage of `std::fs::create_dir` and suggest using `std::fs::create_dir_all` instead."##, | ||
4713 | }, | ||
4714 | LintCompletion { | ||
4715 | label: "clippy::crosspointer_transmute", | ||
4716 | description: r##"Checks for transmutes between a type `T` and `*T`."##, | ||
4717 | }, | ||
4718 | LintCompletion { | ||
4719 | label: "clippy::dbg_macro", | ||
4720 | description: r##"Checks for usage of dbg!() macro."##, | ||
4721 | }, | ||
4722 | LintCompletion { | ||
4723 | label: "clippy::debug_assert_with_mut_call", | ||
4724 | description: r##"Checks for function/method calls with a mutable\nparameter in `debug_assert!`, `debug_assert_eq!` and `debug_assert_ne!` macros."##, | ||
4725 | }, | ||
4726 | LintCompletion { | ||
4727 | label: "clippy::decimal_literal_representation", | ||
4728 | description: r##"Warns if there is a better representation for a numeric literal."##, | ||
4729 | }, | ||
4730 | LintCompletion { | ||
4731 | label: "clippy::declare_interior_mutable_const", | ||
4732 | description: r##"Checks for declaration of `const` items which is interior\nmutable (e.g., contains a `Cell`, `Mutex`, `AtomicXxxx`, etc.)."##, | ||
4733 | }, | ||
4734 | LintCompletion { | ||
4735 | label: "clippy::default_numeric_fallback", | ||
4736 | description: r##"Checks for usage of unconstrained numeric literals which may cause default numeric fallback in type\ninference.\n\nDefault numeric fallback means that if numeric types have not yet been bound to concrete\ntypes at the end of type inference, then integer type is bound to `i32`, and similarly\nfloating type is bound to `f64`.\n\nSee [RFC0212](https://github.com/rust-lang/rfcs/blob/master/text/0212-restore-int-fallback.md) for more information about the fallback."##, | ||
4737 | }, | ||
4738 | LintCompletion { | ||
4739 | label: "clippy::default_trait_access", | ||
4740 | description: r##"Checks for literal calls to `Default::default()`."##, | ||
4741 | }, | ||
4742 | LintCompletion { | ||
4743 | label: "clippy::deprecated_cfg_attr", | ||
4744 | description: r##"Checks for `#[cfg_attr(rustfmt, rustfmt_skip)]` and suggests to replace it\nwith `#[rustfmt::skip]`."##, | ||
4745 | }, | ||
4746 | LintCompletion { | ||
4747 | label: "clippy::deprecated_semver", | ||
4748 | description: r##"Checks for `#[deprecated]` annotations with a `since`\nfield that is not a valid semantic version."##, | ||
4749 | }, | ||
4750 | LintCompletion { | ||
4751 | label: "clippy::deref_addrof", | ||
4752 | description: r##"Checks for usage of `*&` and `*&mut` in expressions."##, | ||
4753 | }, | ||
4754 | LintCompletion { | ||
4755 | label: "clippy::derive_hash_xor_eq", | ||
4756 | description: r##"Checks for deriving `Hash` but implementing `PartialEq`\nexplicitly or vice versa."##, | ||
4757 | }, | ||
4758 | LintCompletion { | ||
4759 | label: "clippy::derive_ord_xor_partial_ord", | ||
4760 | description: r##"Checks for deriving `Ord` but implementing `PartialOrd`\nexplicitly or vice versa."##, | ||
4761 | }, | ||
4762 | LintCompletion { | ||
4763 | label: "clippy::disallowed_method", | ||
4764 | description: r##"Denies the configured methods and functions in clippy.toml"##, | ||
4765 | }, | ||
4766 | LintCompletion { | ||
4767 | label: "clippy::diverging_sub_expression", | ||
4768 | description: r##"Checks for diverging calls that are not match arms or\nstatements."##, | ||
4769 | }, | ||
4770 | LintCompletion { | ||
4771 | label: "clippy::doc_markdown", | ||
4772 | description: r##"Checks for the presence of `_`, `::` or camel-case words\noutside ticks in documentation."##, | ||
4773 | }, | ||
4774 | LintCompletion { | ||
4775 | label: "clippy::double_comparisons", | ||
4776 | description: r##"Checks for double comparisons that could be simplified to a single expression."##, | ||
4777 | }, | ||
4778 | LintCompletion { | ||
4779 | label: "clippy::double_must_use", | ||
4780 | description: r##"Checks for a [`#[must_use]`] attribute without\nfurther information on functions and methods that return a type already\nmarked as `#[must_use]`.\n\n[`#[must_use]`]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute"##, | ||
4781 | }, | ||
4782 | LintCompletion { | ||
4783 | label: "clippy::double_neg", | ||
4784 | description: r##"Detects expressions of the form `--x`."##, | ||
4785 | }, | ||
4786 | LintCompletion { | ||
4787 | label: "clippy::double_parens", | ||
4788 | description: r##"Checks for unnecessary double parentheses."##, | ||
4789 | }, | ||
4790 | LintCompletion { | ||
4791 | label: "clippy::drop_bounds", | ||
4792 | description: r##"Nothing. This lint has been deprecated."##, | ||
4793 | }, | ||
4794 | LintCompletion { | ||
4795 | label: "clippy::drop_copy", | ||
4796 | description: r##"Checks for calls to `std::mem::drop` with a value\nthat derives the Copy trait"##, | ||
4797 | }, | ||
4798 | LintCompletion { | ||
4799 | label: "clippy::drop_ref", | ||
4800 | description: r##"Checks for calls to `std::mem::drop` with a reference\ninstead of an owned value."##, | ||
4801 | }, | ||
4802 | LintCompletion { | ||
4803 | label: "clippy::duplicate_underscore_argument", | ||
4804 | description: r##"Checks for function arguments having the similar names\ndiffering by an underscore."##, | ||
4805 | }, | ||
4806 | LintCompletion { | ||
4807 | label: "clippy::duration_subsec", | ||
4808 | description: r##"Checks for calculation of subsecond microseconds or milliseconds\nfrom other `Duration` methods."##, | ||
4809 | }, | ||
4810 | LintCompletion { | ||
4811 | label: "clippy::else_if_without_else", | ||
4812 | description: r##"Checks for usage of if expressions with an `else if` branch,\nbut without a final `else` branch."##, | ||
4813 | }, | ||
4814 | LintCompletion { | ||
4815 | label: "clippy::empty_enum", | ||
4816 | description: r##"Checks for `enum`s with no variants.\n\nAs of this writing, the `never_type` is still a\nnightly-only experimental API. Therefore, this lint is only triggered\nif the `never_type` is enabled."##, | ||
4817 | }, | ||
4818 | LintCompletion { | ||
4819 | label: "clippy::empty_line_after_outer_attr", | ||
4820 | description: r##"Checks for empty lines after outer attributes"##, | ||
4821 | }, | ||
4822 | LintCompletion { | ||
4823 | label: "clippy::empty_loop", | ||
4824 | description: r##"Checks for empty `loop` expressions."##, | ||
4825 | }, | ||
4826 | LintCompletion { | ||
4827 | label: "clippy::enum_clike_unportable_variant", | ||
4828 | description: r##"Checks for C-like enumerations that are\n`repr(isize/usize)` and have values that don't fit into an `i32`."##, | ||
4829 | }, | ||
4830 | LintCompletion { | ||
4831 | label: "clippy::enum_glob_use", | ||
4832 | description: r##"Checks for `use Enum::*`."##, | ||
4833 | }, | ||
4834 | LintCompletion { | ||
4835 | label: "clippy::enum_variant_names", | ||
4836 | description: r##"Detects enumeration variants that are prefixed or suffixed\nby the same characters."##, | ||
4837 | }, | ||
4838 | LintCompletion { | ||
4839 | label: "clippy::eq_op", | ||
4840 | description: r##"Checks for equal operands to comparison, logical and\nbitwise, difference and division binary operators (`==`, `>`, etc., `&&`,\n`||`, `&`, `|`, `^`, `-` and `/`)."##, | ||
4841 | }, | ||
4842 | LintCompletion { | ||
4843 | label: "clippy::erasing_op", | ||
4844 | description: r##"Checks for erasing operations, e.g., `x * 0`."##, | ||
4845 | }, | ||
4846 | LintCompletion { | ||
4847 | label: "clippy::eval_order_dependence", | ||
4848 | description: r##"Checks for a read and a write to the same variable where\nwhether the read occurs before or after the write depends on the evaluation\norder of sub-expressions."##, | ||
4849 | }, | ||
4850 | LintCompletion { | ||
4851 | label: "clippy::excessive_precision", | ||
4852 | description: r##"Checks for float literals with a precision greater\nthan that supported by the underlying type."##, | ||
4853 | }, | ||
4854 | LintCompletion { | ||
4855 | label: "clippy::exhaustive_enums", | ||
4856 | description: r##"Warns on any exported `enum`s that are not tagged `#[non_exhaustive]`"##, | ||
4857 | }, | ||
4858 | LintCompletion { | ||
4859 | label: "clippy::exhaustive_structs", | ||
4860 | description: r##"Warns on any exported `structs`s that are not tagged `#[non_exhaustive]`"##, | ||
4861 | }, | ||
4862 | LintCompletion { | ||
4863 | label: "clippy::exit", | ||
4864 | description: r##"`exit()` terminates the program and doesn't provide a\nstack trace."##, | ||
4865 | }, | ||
4866 | LintCompletion { | ||
4867 | label: "clippy::expect_fun_call", | ||
4868 | description: r##"Checks for calls to `.expect(&format!(...))`, `.expect(foo(..))`,\netc., and suggests to use `unwrap_or_else` instead"##, | ||
4869 | }, | ||
4870 | LintCompletion { | ||
4871 | label: "clippy::expect_used", | ||
4872 | description: r##"Checks for `.expect()` calls on `Option`s and `Result`s."##, | ||
4873 | }, | ||
4874 | LintCompletion { | ||
4875 | label: "clippy::expl_impl_clone_on_copy", | ||
4876 | description: r##"Checks for explicit `Clone` implementations for `Copy`\ntypes."##, | ||
4877 | }, | ||
4878 | LintCompletion { | ||
4879 | label: "clippy::explicit_counter_loop", | ||
4880 | description: r##"Checks `for` loops over slices with an explicit counter\nand suggests the use of `.enumerate()`."##, | ||
4881 | }, | ||
4882 | LintCompletion { | ||
4883 | label: "clippy::explicit_deref_methods", | ||
4884 | description: r##"Checks for explicit `deref()` or `deref_mut()` method calls."##, | ||
4885 | }, | ||
4886 | LintCompletion { | ||
4887 | label: "clippy::explicit_into_iter_loop", | ||
4888 | description: r##"Checks for loops on `y.into_iter()` where `y` will do, and\nsuggests the latter."##, | ||
4889 | }, | ||
4890 | LintCompletion { | ||
4891 | label: "clippy::explicit_iter_loop", | ||
4892 | description: r##"Checks for loops on `x.iter()` where `&x` will do, and\nsuggests the latter."##, | ||
4893 | }, | ||
4894 | LintCompletion { | ||
4895 | label: "clippy::explicit_write", | ||
4896 | description: r##"Checks for usage of `write!()` / `writeln()!` which can be\nreplaced with `(e)print!()` / `(e)println!()`"##, | ||
4897 | }, | ||
4898 | LintCompletion { | ||
4899 | label: "clippy::extend_from_slice", | ||
4900 | description: r##"Nothing. This lint has been deprecated."##, | ||
4901 | }, | ||
4902 | LintCompletion { | ||
4903 | label: "clippy::extra_unused_lifetimes", | ||
4904 | description: r##"Checks for lifetimes in generics that are never used\nanywhere else."##, | ||
4905 | }, | ||
4906 | LintCompletion { | ||
4907 | label: "clippy::fallible_impl_from", | ||
4908 | description: r##"Checks for impls of `From<..>` that contain `panic!()` or `unwrap()`"##, | ||
4909 | }, | ||
4910 | LintCompletion { | ||
4911 | label: "clippy::field_reassign_with_default", | ||
4912 | description: r##"Checks for immediate reassignment of fields initialized\nwith Default::default()."##, | ||
4913 | }, | ||
4914 | LintCompletion { | ||
4915 | label: "clippy::filetype_is_file", | ||
4916 | description: r##"Checks for `FileType::is_file()`."##, | ||
4917 | }, | ||
4918 | LintCompletion { | ||
4919 | label: "clippy::filter_map", | ||
4920 | description: r##"Checks for usage of `_.filter(_).map(_)`,\n`_.filter(_).flat_map(_)`, `_.filter_map(_).flat_map(_)` and similar."##, | ||
4921 | }, | ||
4922 | LintCompletion { | ||
4923 | label: "clippy::filter_map_identity", | ||
4924 | description: r##"Checks for usage of `filter_map(|x| x)`."##, | ||
4925 | }, | ||
4926 | LintCompletion { | ||
4927 | label: "clippy::filter_map_next", | ||
4928 | description: r##"Checks for usage of `_.filter_map(_).next()`."##, | ||
4929 | }, | ||
4930 | LintCompletion { | ||
4931 | label: "clippy::filter_next", | ||
4932 | description: r##"Checks for usage of `_.filter(_).next()`."##, | ||
4933 | }, | ||
4934 | LintCompletion { | ||
4935 | label: "clippy::find_map", | ||
4936 | description: r##"Nothing. This lint has been deprecated."##, | ||
4937 | }, | ||
4938 | LintCompletion { | ||
4939 | label: "clippy::flat_map_identity", | ||
4940 | description: r##"Checks for usage of `flat_map(|x| x)`."##, | ||
4941 | }, | ||
4942 | LintCompletion { | ||
4943 | label: "clippy::float_arithmetic", | ||
4944 | description: r##"Checks for float arithmetic."##, | ||
4945 | }, | ||
4946 | LintCompletion { | ||
4947 | label: "clippy::float_cmp", | ||
4948 | description: r##"Checks for (in-)equality comparisons on floating-point\nvalues (apart from zero), except in functions called `*eq*` (which probably\nimplement equality for a type involving floats)."##, | ||
4949 | }, | ||
4950 | LintCompletion { | ||
4951 | label: "clippy::float_cmp_const", | ||
4952 | description: r##"Checks for (in-)equality comparisons on floating-point\nvalue and constant, except in functions called `*eq*` (which probably\nimplement equality for a type involving floats)."##, | ||
4953 | }, | ||
4954 | LintCompletion { | ||
4955 | label: "clippy::float_equality_without_abs", | ||
4956 | description: r##"Checks for statements of the form `(a - b) < f32::EPSILON` or\n`(a - b) < f64::EPSILON`. Notes the missing `.abs()`."##, | ||
4957 | }, | ||
4958 | LintCompletion { | ||
4959 | label: "clippy::fn_address_comparisons", | ||
4960 | description: r##"Checks for comparisons with an address of a function item."##, | ||
4961 | }, | ||
4962 | LintCompletion { | ||
4963 | label: "clippy::fn_params_excessive_bools", | ||
4964 | description: r##"Checks for excessive use of\nbools in function definitions."##, | ||
4965 | }, | ||
4966 | LintCompletion { | ||
4967 | label: "clippy::fn_to_numeric_cast", | ||
4968 | description: r##"Checks for casts of function pointers to something other than usize"##, | ||
4969 | }, | ||
4970 | LintCompletion { | ||
4971 | label: "clippy::fn_to_numeric_cast_with_truncation", | ||
4972 | description: r##"Checks for casts of a function pointer to a numeric type not wide enough to\nstore address."##, | ||
4973 | }, | ||
4974 | LintCompletion { | ||
4975 | label: "clippy::for_kv_map", | ||
4976 | description: r##"Checks for iterating a map (`HashMap` or `BTreeMap`) and\nignoring either the keys or values."##, | ||
4977 | }, | ||
4978 | LintCompletion { | ||
4979 | label: "clippy::for_loops_over_fallibles", | ||
4980 | description: r##"Checks for `for` loops over `Option` or `Result` values."##, | ||
4981 | }, | ||
4982 | LintCompletion { | ||
4983 | label: "clippy::forget_copy", | ||
4984 | description: r##"Checks for calls to `std::mem::forget` with a value that\nderives the Copy trait"##, | ||
4985 | }, | ||
4986 | LintCompletion { | ||
4987 | label: "clippy::forget_ref", | ||
4988 | description: r##"Checks for calls to `std::mem::forget` with a reference\ninstead of an owned value."##, | ||
4989 | }, | ||
4990 | LintCompletion { | ||
4991 | label: "clippy::from_iter_instead_of_collect", | ||
4992 | description: r##"Checks for `from_iter()` function calls on types that implement the `FromIterator`\ntrait."##, | ||
4993 | }, | ||
4994 | LintCompletion { | ||
4995 | label: "clippy::from_over_into", | ||
4996 | description: r##"Searches for implementations of the `Into<..>` trait and suggests to implement `From<..>` instead."##, | ||
4997 | }, | ||
4998 | LintCompletion { | ||
4999 | label: "clippy::from_str_radix_10", | ||
5000 | description: r##"Checks for function invocations of the form `primitive::from_str_radix(s, 10)`"##, | ||
5001 | }, | ||
5002 | LintCompletion { | ||
5003 | label: "clippy::future_not_send", | ||
5004 | description: r##"This lint requires Future implementations returned from\nfunctions and methods to implement the `Send` marker trait. It is mostly\nused by library authors (public and internal) that target an audience where\nmultithreaded executors are likely to be used for running these Futures."##, | ||
5005 | }, | ||
5006 | LintCompletion { | ||
5007 | label: "clippy::get_last_with_len", | ||
5008 | description: r##"Checks for using `x.get(x.len() - 1)` instead of\n`x.last()`."##, | ||
5009 | }, | ||
5010 | LintCompletion { | ||
5011 | label: "clippy::get_unwrap", | ||
5012 | description: r##"Checks for use of `.get().unwrap()` (or\n`.get_mut().unwrap`) on a standard library type which implements `Index`"##, | ||
5013 | }, | ||
5014 | LintCompletion { | ||
5015 | label: "clippy::identity_op", | ||
5016 | description: r##"Checks for identity operations, e.g., `x + 0`."##, | ||
5017 | }, | ||
5018 | LintCompletion { | ||
5019 | label: "clippy::if_let_mutex", | ||
5020 | description: r##"Checks for `Mutex::lock` calls in `if let` expression\nwith lock calls in any of the else blocks."##, | ||
5021 | }, | ||
5022 | LintCompletion { | ||
5023 | label: "clippy::if_let_redundant_pattern_matching", | ||
5024 | description: r##"Nothing. This lint has been deprecated."##, | ||
5025 | }, | ||
5026 | LintCompletion { | ||
5027 | label: "clippy::if_let_some_result", | ||
5028 | description: r##"* Checks for unnecessary `ok()` in if let."##, | ||
5029 | }, | ||
5030 | LintCompletion { | ||
5031 | label: "clippy::if_not_else", | ||
5032 | description: r##"Checks for usage of `!` or `!=` in an if condition with an\nelse branch."##, | ||
5033 | }, | ||
5034 | LintCompletion { | ||
5035 | label: "clippy::if_same_then_else", | ||
5036 | description: r##"Checks for `if/else` with the same body as the *then* part\nand the *else* part."##, | ||
5037 | }, | ||
5038 | LintCompletion { | ||
5039 | label: "clippy::ifs_same_cond", | ||
5040 | description: r##"Checks for consecutive `if`s with the same condition."##, | ||
5041 | }, | ||
5042 | LintCompletion { | ||
5043 | label: "clippy::implicit_clone", | ||
5044 | description: r##"Checks for the usage of `_.to_owned()`, `vec.to_vec()`, or similar when calling `_.clone()` would be clearer."##, | ||
5045 | }, | ||
5046 | LintCompletion { | ||
5047 | label: "clippy::implicit_hasher", | ||
5048 | description: r##"Checks for public `impl` or `fn` missing generalization\nover different hashers and implicitly defaulting to the default hashing\nalgorithm (`SipHash`)."##, | ||
5049 | }, | ||
5050 | LintCompletion { | ||
5051 | label: "clippy::implicit_return", | ||
5052 | description: r##"Checks for missing return statements at the end of a block."##, | ||
5053 | }, | ||
5054 | LintCompletion { | ||
5055 | label: "clippy::implicit_saturating_sub", | ||
5056 | description: r##"Checks for implicit saturating subtraction."##, | ||
5057 | }, | ||
5058 | LintCompletion { | ||
5059 | label: "clippy::imprecise_flops", | ||
5060 | description: r##"Looks for floating-point expressions that\ncan be expressed using built-in methods to improve accuracy\nat the cost of performance."##, | ||
5061 | }, | ||
5062 | LintCompletion { | ||
5063 | label: "clippy::inconsistent_digit_grouping", | ||
5064 | description: r##"Warns if an integral or floating-point constant is\ngrouped inconsistently with underscores."##, | ||
5065 | }, | ||
5066 | LintCompletion { | ||
5067 | label: "clippy::inconsistent_struct_constructor", | ||
5068 | description: r##"Checks for struct constructors where the order of the field init\nshorthand in the constructor is inconsistent with the order in the struct definition."##, | ||
5069 | }, | ||
5070 | LintCompletion { | ||
5071 | label: "clippy::indexing_slicing", | ||
5072 | description: r##"Checks for usage of indexing or slicing. Arrays are special cases, this lint\ndoes report on arrays if we can tell that slicing operations are in bounds and does not\nlint on constant `usize` indexing on arrays because that is handled by rustc's `const_err` lint."##, | ||
5073 | }, | ||
5074 | LintCompletion { | ||
5075 | label: "clippy::ineffective_bit_mask", | ||
5076 | description: r##"Checks for bit masks in comparisons which can be removed\nwithout changing the outcome. The basic structure can be seen in the\nfollowing table:\n\n|Comparison| Bit Op |Example |equals |\n|----------|---------|-----------|-------|\n|`>` / `<=`|`|` / `^`|`x | 2 > 3`|`x > 3`|\n|`<` / `>=`|`|` / `^`|`x ^ 1 < 4`|`x < 4`|"##, | ||
5077 | }, | ||
5078 | LintCompletion { | ||
5079 | label: "clippy::inefficient_to_string", | ||
5080 | description: r##"Checks for usage of `.to_string()` on an `&&T` where\n`T` implements `ToString` directly (like `&&str` or `&&String`)."##, | ||
5081 | }, | ||
5082 | LintCompletion { | ||
5083 | label: "clippy::infallible_destructuring_match", | ||
5084 | description: r##"Checks for matches being used to destructure a single-variant enum\nor tuple struct where a `let` will suffice."##, | ||
5085 | }, | ||
5086 | LintCompletion { | ||
5087 | label: "clippy::infinite_iter", | ||
5088 | description: r##"Checks for iteration that is guaranteed to be infinite."##, | ||
5089 | }, | ||
5090 | LintCompletion { | ||
5091 | label: "clippy::inherent_to_string", | ||
5092 | description: r##"Checks for the definition of inherent methods with a signature of `to_string(&self) -> String`."##, | ||
5093 | }, | ||
5094 | LintCompletion { | ||
5095 | label: "clippy::inherent_to_string_shadow_display", | ||
5096 | description: r##"Checks for the definition of inherent methods with a signature of `to_string(&self) -> String` and if the type implementing this method also implements the `Display` trait."##, | ||
5097 | }, | ||
5098 | LintCompletion { | ||
5099 | label: "clippy::inline_always", | ||
5100 | description: r##"Checks for items annotated with `#[inline(always)]`,\nunless the annotated function is empty or simply panics."##, | ||
5101 | }, | ||
5102 | LintCompletion { | ||
5103 | label: "clippy::inline_asm_x86_att_syntax", | ||
5104 | description: r##"Checks for usage of AT&T x86 assembly syntax."##, | ||
5105 | }, | ||
5106 | LintCompletion { | ||
5107 | label: "clippy::inline_asm_x86_intel_syntax", | ||
5108 | description: r##"Checks for usage of Intel x86 assembly syntax."##, | ||
5109 | }, | ||
5110 | LintCompletion { | ||
5111 | label: "clippy::inline_fn_without_body", | ||
5112 | description: r##"Checks for `#[inline]` on trait methods without bodies"##, | ||
5113 | }, | ||
5114 | LintCompletion { | ||
5115 | label: "clippy::inspect_for_each", | ||
5116 | description: r##"Checks for usage of `inspect().for_each()`."##, | ||
5117 | }, | ||
5118 | LintCompletion { | ||
5119 | label: "clippy::int_plus_one", | ||
5120 | description: r##"Checks for usage of `x >= y + 1` or `x - 1 >= y` (and `<=`) in a block"##, | ||
5121 | }, | ||
5122 | LintCompletion { | ||
5123 | label: "clippy::integer_arithmetic", | ||
5124 | description: r##"Checks for integer arithmetic operations which could overflow or panic.\n\nSpecifically, checks for any operators (`+`, `-`, `*`, `<<`, etc) which are capable\nof overflowing according to the [Rust\nReference](https://doc.rust-lang.org/reference/expressions/operator-expr.html#overflow),\nor which can panic (`/`, `%`). No bounds analysis or sophisticated reasoning is\nattempted."##, | ||
5125 | }, | ||
5126 | LintCompletion { | ||
5127 | label: "clippy::integer_division", | ||
5128 | description: r##"Checks for division of integers"##, | ||
5129 | }, | ||
5130 | LintCompletion { | ||
5131 | label: "clippy::into_iter_on_array", | ||
5132 | description: r##"Nothing. This lint has been deprecated."##, | ||
5133 | }, | ||
5134 | LintCompletion { | ||
5135 | label: "clippy::into_iter_on_ref", | ||
5136 | description: r##"Checks for `into_iter` calls on references which should be replaced by `iter`\nor `iter_mut`."##, | ||
5137 | }, | ||
5138 | LintCompletion { | ||
5139 | label: "clippy::invalid_atomic_ordering", | ||
5140 | description: r##"Checks for usage of invalid atomic\nordering in atomic loads/stores/exchanges/updates and\nmemory fences."##, | ||
5141 | }, | ||
5142 | LintCompletion { | ||
5143 | label: "clippy::invalid_ref", | ||
5144 | description: r##"Nothing. This lint has been deprecated."##, | ||
5145 | }, | ||
5146 | LintCompletion { | ||
5147 | label: "clippy::invalid_regex", | ||
5148 | description: r##"Checks [regex](https://crates.io/crates/regex) creation\n(with `Regex::new`, `RegexBuilder::new`, or `RegexSet::new`) for correct\nregex syntax."##, | ||
5149 | }, | ||
5150 | LintCompletion { | ||
5151 | label: "clippy::invalid_upcast_comparisons", | ||
5152 | description: r##"Checks for comparisons where the relation is always either\ntrue or false, but where one side has been upcast so that the comparison is\nnecessary. Only integer types are checked."##, | ||
5153 | }, | ||
5154 | LintCompletion { | ||
5155 | label: "clippy::invisible_characters", | ||
5156 | description: r##"Checks for invisible Unicode characters in the code."##, | ||
5157 | }, | ||
5158 | LintCompletion { | ||
5159 | label: "clippy::items_after_statements", | ||
5160 | description: r##"Checks for items declared after some statement in a block."##, | ||
5161 | }, | ||
5162 | LintCompletion { | ||
5163 | label: "clippy::iter_cloned_collect", | ||
5164 | description: r##"Checks for the use of `.cloned().collect()` on slice to\ncreate a `Vec`."##, | ||
5165 | }, | ||
5166 | LintCompletion { | ||
5167 | label: "clippy::iter_next_loop", | ||
5168 | description: r##"Checks for loops on `x.next()`."##, | ||
5169 | }, | ||
5170 | LintCompletion { | ||
5171 | label: "clippy::iter_next_slice", | ||
5172 | description: r##"Checks for usage of `iter().next()` on a Slice or an Array"##, | ||
5173 | }, | ||
5174 | LintCompletion { | ||
5175 | label: "clippy::iter_nth", | ||
5176 | description: r##"Checks for use of `.iter().nth()` (and the related\n`.iter_mut().nth()`) on standard library types with O(1) element access."##, | ||
5177 | }, | ||
5178 | LintCompletion { | ||
5179 | label: "clippy::iter_nth_zero", | ||
5180 | description: r##"Checks for the use of `iter.nth(0)`."##, | ||
5181 | }, | ||
5182 | LintCompletion { | ||
5183 | label: "clippy::iter_skip_next", | ||
5184 | description: r##"Checks for use of `.skip(x).next()` on iterators."##, | ||
5185 | }, | ||
5186 | LintCompletion { | ||
5187 | label: "clippy::iterator_step_by_zero", | ||
5188 | description: r##"Checks for calling `.step_by(0)` on iterators which panics."##, | ||
5189 | }, | ||
5190 | LintCompletion { | ||
5191 | label: "clippy::just_underscores_and_digits", | ||
5192 | description: r##"Checks if you have variables whose name consists of just\nunderscores and digits."##, | ||
5193 | }, | ||
5194 | LintCompletion { | ||
5195 | label: "clippy::large_const_arrays", | ||
5196 | description: r##"Checks for large `const` arrays that should\nbe defined as `static` instead."##, | ||
5197 | }, | ||
5198 | LintCompletion { | ||
5199 | label: "clippy::large_digit_groups", | ||
5200 | description: r##"Warns if the digits of an integral or floating-point\nconstant are grouped into groups that\nare too large."##, | ||
5201 | }, | ||
5202 | LintCompletion { | ||
5203 | label: "clippy::large_enum_variant", | ||
5204 | description: r##"Checks for large size differences between variants on\n`enum`s."##, | ||
5205 | }, | ||
5206 | LintCompletion { | ||
5207 | label: "clippy::large_stack_arrays", | ||
5208 | description: r##"Checks for local arrays that may be too large."##, | ||
5209 | }, | ||
5210 | LintCompletion { | ||
5211 | label: "clippy::large_types_passed_by_value", | ||
5212 | description: r##"Checks for functions taking arguments by value, where\nthe argument type is `Copy` and large enough to be worth considering\npassing by reference. Does not trigger if the function is being exported,\nbecause that might induce API breakage, if the parameter is declared as mutable,\nor if the argument is a `self`."##, | ||
5213 | }, | ||
5214 | LintCompletion { | ||
5215 | label: "clippy::len_without_is_empty", | ||
5216 | description: r##"Checks for items that implement `.len()` but not\n`.is_empty()`."##, | ||
5217 | }, | ||
5218 | LintCompletion { | ||
5219 | label: "clippy::len_zero", | ||
5220 | description: r##"Checks for getting the length of something via `.len()`\njust to compare to zero, and suggests using `.is_empty()` where applicable."##, | ||
5221 | }, | ||
5222 | LintCompletion { | ||
5223 | label: "clippy::let_and_return", | ||
5224 | description: r##"Checks for `let`-bindings, which are subsequently\nreturned."##, | ||
5225 | }, | ||
5226 | LintCompletion { | ||
5227 | label: "clippy::let_underscore_drop", | ||
5228 | description: r##"Checks for `let _ = <expr>`\nwhere expr has a type that implements `Drop`"##, | ||
5229 | }, | ||
5230 | LintCompletion { | ||
5231 | label: "clippy::let_underscore_lock", | ||
5232 | description: r##"Checks for `let _ = sync_lock`"##, | ||
5233 | }, | ||
5234 | LintCompletion { | ||
5235 | label: "clippy::let_underscore_must_use", | ||
5236 | description: r##"Checks for `let _ = <expr>`\nwhere expr is #[must_use]"##, | ||
5237 | }, | ||
5238 | LintCompletion { | ||
5239 | label: "clippy::let_unit_value", | ||
5240 | description: r##"Checks for binding a unit value."##, | ||
5241 | }, | ||
5242 | LintCompletion { | ||
5243 | label: "clippy::linkedlist", | ||
5244 | description: r##"Checks for usage of any `LinkedList`, suggesting to use a\n`Vec` or a `VecDeque` (formerly called `RingBuf`)."##, | ||
5245 | }, | ||
5246 | LintCompletion { | ||
5247 | label: "clippy::logic_bug", | ||
5248 | description: r##"Checks for boolean expressions that contain terminals that\ncan be eliminated."##, | ||
5249 | }, | ||
5250 | LintCompletion { | ||
5251 | label: "clippy::lossy_float_literal", | ||
5252 | description: r##"Checks for whole number float literals that\ncannot be represented as the underlying type without loss."##, | ||
5253 | }, | ||
5254 | LintCompletion { | ||
5255 | label: "clippy::macro_use_imports", | ||
5256 | description: r##"Checks for `#[macro_use] use...`."##, | ||
5257 | }, | ||
5258 | LintCompletion { | ||
5259 | label: "clippy::main_recursion", | ||
5260 | description: r##"Checks for recursion using the entrypoint."##, | ||
5261 | }, | ||
5262 | LintCompletion { | ||
5263 | label: "clippy::manual_async_fn", | ||
5264 | description: r##"It checks for manual implementations of `async` functions."##, | ||
5265 | }, | ||
5266 | LintCompletion { | ||
5267 | label: "clippy::manual_filter_map", | ||
5268 | description: r##"Checks for usage of `_.filter(_).map(_)` that can be written more simply\nas `filter_map(_)`."##, | ||
5269 | }, | ||
5270 | LintCompletion { | ||
5271 | label: "clippy::manual_find_map", | ||
5272 | description: r##"Checks for usage of `_.find(_).map(_)` that can be written more simply\nas `find_map(_)`."##, | ||
5273 | }, | ||
5274 | LintCompletion { | ||
5275 | label: "clippy::manual_flatten", | ||
5276 | description: r##"Check for unnecessary `if let` usage in a for loop\nwhere only the `Some` or `Ok` variant of the iterator element is used."##, | ||
5277 | }, | ||
5278 | LintCompletion { | ||
5279 | label: "clippy::manual_map", | ||
5280 | description: r##"Checks for usages of `match` which could be implemented using `map`"##, | ||
5281 | }, | ||
5282 | LintCompletion { | ||
5283 | label: "clippy::manual_memcpy", | ||
5284 | description: r##"Checks for for-loops that manually copy items between\nslices that could be optimized by having a memcpy."##, | ||
5285 | }, | ||
5286 | LintCompletion { | ||
5287 | label: "clippy::manual_non_exhaustive", | ||
5288 | description: r##"Checks for manual implementations of the non-exhaustive pattern."##, | ||
5289 | }, | ||
5290 | LintCompletion { | ||
5291 | label: "clippy::manual_ok_or", | ||
5292 | description: r##"Finds patterns that reimplement `Option::ok_or`."##, | ||
5293 | }, | ||
5294 | LintCompletion { | ||
5295 | label: "clippy::manual_range_contains", | ||
5296 | description: r##"Checks for expressions like `x >= 3 && x < 8` that could\nbe more readably expressed as `(3..8).contains(x)`."##, | ||
5297 | }, | ||
5298 | LintCompletion { | ||
5299 | label: "clippy::manual_saturating_arithmetic", | ||
5300 | description: r##"Checks for `.checked_add/sub(x).unwrap_or(MAX/MIN)`."##, | ||
5301 | }, | ||
5302 | LintCompletion { | ||
5303 | label: "clippy::manual_strip", | ||
5304 | description: r##"Suggests using `strip_{prefix,suffix}` over `str::{starts,ends}_with` and slicing using\nthe pattern's length."##, | ||
5305 | }, | ||
5306 | LintCompletion { | ||
5307 | label: "clippy::manual_swap", | ||
5308 | description: r##"Checks for manual swapping."##, | ||
5309 | }, | ||
5310 | LintCompletion { | ||
5311 | label: "clippy::manual_unwrap_or", | ||
5312 | description: r##"Finds patterns that reimplement `Option::unwrap_or` or `Result::unwrap_or`."##, | ||
5313 | }, | ||
5314 | LintCompletion { | ||
5315 | label: "clippy::many_single_char_names", | ||
5316 | description: r##"Checks for too many variables whose name consists of a\nsingle character."##, | ||
5317 | }, | ||
5318 | LintCompletion { | ||
5319 | label: "clippy::map_clone", | ||
5320 | description: r##"Checks for usage of `map(|x| x.clone())` or\ndereferencing closures for `Copy` types, on `Iterator` or `Option`,\nand suggests `cloned()` or `copied()` instead"##, | ||
5321 | }, | ||
5322 | LintCompletion { | ||
5323 | label: "clippy::map_collect_result_unit", | ||
5324 | description: r##"Checks for usage of `_.map(_).collect::<Result<(), _>()`."##, | ||
5325 | }, | ||
5326 | LintCompletion { | ||
5327 | label: "clippy::map_entry", | ||
5328 | description: r##"Checks for uses of `contains_key` + `insert` on `HashMap`\nor `BTreeMap`."##, | ||
5329 | }, | ||
5330 | LintCompletion { | ||
5331 | label: "clippy::map_err_ignore", | ||
5332 | description: r##"Checks for instances of `map_err(|_| Some::Enum)`"##, | ||
5333 | }, | ||
5334 | LintCompletion { | ||
5335 | label: "clippy::map_flatten", | ||
5336 | description: r##"Checks for usage of `_.map(_).flatten(_)`,"##, | ||
5337 | }, | ||
5338 | LintCompletion { | ||
5339 | label: "clippy::map_identity", | ||
5340 | description: r##"Checks for instances of `map(f)` where `f` is the identity function."##, | ||
5341 | }, | ||
5342 | LintCompletion { | ||
5343 | label: "clippy::map_unwrap_or", | ||
5344 | description: r##"Checks for usage of `option.map(_).unwrap_or(_)` or `option.map(_).unwrap_or_else(_)` or\n`result.map(_).unwrap_or_else(_)`."##, | ||
5345 | }, | ||
5346 | LintCompletion { | ||
5347 | label: "clippy::match_as_ref", | ||
5348 | description: r##"Checks for match which is used to add a reference to an\n`Option` value."##, | ||
5349 | }, | ||
5350 | LintCompletion { | ||
5351 | label: "clippy::match_bool", | ||
5352 | description: r##"Checks for matches where match expression is a `bool`. It\nsuggests to replace the expression with an `if...else` block."##, | ||
5353 | }, | ||
5354 | LintCompletion { | ||
5355 | label: "clippy::match_like_matches_macro", | ||
5356 | description: r##"Checks for `match` or `if let` expressions producing a\n`bool` that could be written using `matches!`"##, | ||
5357 | }, | ||
5358 | LintCompletion { | ||
5359 | label: "clippy::match_on_vec_items", | ||
5360 | description: r##"Checks for `match vec[idx]` or `match vec[n..m]`."##, | ||
5361 | }, | ||
5362 | LintCompletion { | ||
5363 | label: "clippy::match_overlapping_arm", | ||
5364 | description: r##"Checks for overlapping match arms."##, | ||
5365 | }, | ||
5366 | LintCompletion { | ||
5367 | label: "clippy::match_ref_pats", | ||
5368 | description: r##"Checks for matches where all arms match a reference,\nsuggesting to remove the reference and deref the matched expression\ninstead. It also checks for `if let &foo = bar` blocks."##, | ||
5369 | }, | ||
5370 | LintCompletion { | ||
5371 | label: "clippy::match_same_arms", | ||
5372 | description: r##"Checks for `match` with identical arm bodies."##, | ||
5373 | }, | ||
5374 | LintCompletion { | ||
5375 | label: "clippy::match_single_binding", | ||
5376 | description: r##"Checks for useless match that binds to only one value."##, | ||
5377 | }, | ||
5378 | LintCompletion { | ||
5379 | label: "clippy::match_wild_err_arm", | ||
5380 | description: r##"Checks for arm which matches all errors with `Err(_)`\nand take drastic actions like `panic!`."##, | ||
5381 | }, | ||
5382 | LintCompletion { | ||
5383 | label: "clippy::match_wildcard_for_single_variants", | ||
5384 | description: r##"Checks for wildcard enum matches for a single variant."##, | ||
5385 | }, | ||
5386 | LintCompletion { | ||
5387 | label: "clippy::maybe_infinite_iter", | ||
5388 | description: r##"Checks for iteration that may be infinite."##, | ||
5389 | }, | ||
5390 | LintCompletion { | ||
5391 | label: "clippy::mem_discriminant_non_enum", | ||
5392 | description: r##"Checks for calls of `mem::discriminant()` on a non-enum type."##, | ||
5393 | }, | ||
5394 | LintCompletion { | ||
5395 | label: "clippy::mem_forget", | ||
5396 | description: r##"Checks for usage of `std::mem::forget(t)` where `t` is\n`Drop`."##, | ||
5397 | }, | ||
5398 | LintCompletion { | ||
5399 | label: "clippy::mem_replace_option_with_none", | ||
5400 | description: r##"Checks for `mem::replace()` on an `Option` with\n`None`."##, | ||
5401 | }, | ||
5402 | LintCompletion { | ||
5403 | label: "clippy::mem_replace_with_default", | ||
5404 | description: r##"Checks for `std::mem::replace` on a value of type\n`T` with `T::default()`."##, | ||
5405 | }, | ||
5406 | LintCompletion { | ||
5407 | label: "clippy::mem_replace_with_uninit", | ||
5408 | description: r##"Checks for `mem::replace(&mut _, mem::uninitialized())`\nand `mem::replace(&mut _, mem::zeroed())`."##, | ||
5409 | }, | ||
5410 | LintCompletion { | ||
5411 | label: "clippy::min_max", | ||
5412 | description: r##"Checks for expressions where `std::cmp::min` and `max` are\nused to clamp values, but switched so that the result is constant."##, | ||
5413 | }, | ||
5414 | LintCompletion { | ||
5415 | label: "clippy::misaligned_transmute", | ||
5416 | description: r##"Nothing. This lint has been deprecated."##, | ||
5417 | }, | ||
5418 | LintCompletion { | ||
5419 | label: "clippy::mismatched_target_os", | ||
5420 | description: r##"Checks for cfg attributes having operating systems used in target family position."##, | ||
5421 | }, | ||
5422 | LintCompletion { | ||
5423 | label: "clippy::misrefactored_assign_op", | ||
5424 | description: r##"Checks for `a op= a op b` or `a op= b op a` patterns."##, | ||
5425 | }, | ||
5426 | LintCompletion { | ||
5427 | label: "clippy::missing_const_for_fn", | ||
5428 | description: r##"Suggests the use of `const` in functions and methods where possible."##, | ||
5429 | }, | ||
5430 | LintCompletion { | ||
5431 | label: "clippy::missing_docs_in_private_items", | ||
5432 | description: r##"Warns if there is missing doc for any documentable item\n(public or private)."##, | ||
5433 | }, | ||
5434 | LintCompletion { | ||
5435 | label: "clippy::missing_errors_doc", | ||
5436 | description: r##"Checks the doc comments of publicly visible functions that\nreturn a `Result` type and warns if there is no `# Errors` section."##, | ||
5437 | }, | ||
5438 | LintCompletion { | ||
5439 | label: "clippy::missing_inline_in_public_items", | ||
5440 | description: r##"it lints if an exported function, method, trait method with default impl,\nor trait method impl is not `#[inline]`."##, | ||
5441 | }, | ||
5442 | LintCompletion { | ||
5443 | label: "clippy::missing_panics_doc", | ||
5444 | description: r##"Checks the doc comments of publicly visible functions that\nmay panic and warns if there is no `# Panics` section."##, | ||
5445 | }, | ||
5446 | LintCompletion { | ||
5447 | label: "clippy::missing_safety_doc", | ||
5448 | description: r##"Checks for the doc comments of publicly visible\nunsafe functions and warns if there is no `# Safety` section."##, | ||
5449 | }, | ||
5450 | LintCompletion { | ||
5451 | label: "clippy::mistyped_literal_suffixes", | ||
5452 | description: r##"Warns for mistyped suffix in literals"##, | ||
5453 | }, | ||
5454 | LintCompletion { | ||
5455 | label: "clippy::mixed_case_hex_literals", | ||
5456 | description: r##"Warns on hexadecimal literals with mixed-case letter\ndigits."##, | ||
5457 | }, | ||
5458 | LintCompletion { | ||
5459 | label: "clippy::module_inception", | ||
5460 | description: r##"Checks for modules that have the same name as their\nparent module"##, | ||
5461 | }, | ||
5462 | LintCompletion { | ||
5463 | label: "clippy::module_name_repetitions", | ||
5464 | description: r##"Detects type names that are prefixed or suffixed by the\ncontaining module's name."##, | ||
5465 | }, | ||
5466 | LintCompletion { | ||
5467 | label: "clippy::modulo_arithmetic", | ||
5468 | description: r##"Checks for modulo arithmetic."##, | ||
5469 | }, | ||
5470 | LintCompletion { | ||
5471 | label: "clippy::modulo_one", | ||
5472 | description: r##"Checks for getting the remainder of a division by one or minus\none."##, | ||
5473 | }, | ||
5474 | LintCompletion { | ||
5475 | label: "clippy::multiple_crate_versions", | ||
5476 | description: r##"Checks to see if multiple versions of a crate are being\nused."##, | ||
5477 | }, | ||
5478 | LintCompletion { | ||
5479 | label: "clippy::multiple_inherent_impl", | ||
5480 | description: r##"Checks for multiple inherent implementations of a struct"##, | ||
5481 | }, | ||
5482 | LintCompletion { | ||
5483 | label: "clippy::must_use_candidate", | ||
5484 | description: r##"Checks for public functions that have no\n[`#[must_use]`] attribute, but return something not already marked\nmust-use, have no mutable arg and mutate no statics.\n\n[`#[must_use]`]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute"##, | ||
5485 | }, | ||
5486 | LintCompletion { | ||
5487 | label: "clippy::must_use_unit", | ||
5488 | description: r##"Checks for a [`#[must_use]`] attribute on\nunit-returning functions and methods.\n\n[`#[must_use]`]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute"##, | ||
5489 | }, | ||
5490 | LintCompletion { | ||
5491 | label: "clippy::mut_from_ref", | ||
5492 | description: r##"This lint checks for functions that take immutable\nreferences and return mutable ones."##, | ||
5493 | }, | ||
5494 | LintCompletion { | ||
5495 | label: "clippy::mut_mut", | ||
5496 | description: r##"Checks for instances of `mut mut` references."##, | ||
5497 | }, | ||
5498 | LintCompletion { | ||
5499 | label: "clippy::mut_mutex_lock", | ||
5500 | description: r##"Checks for `&mut Mutex::lock` calls"##, | ||
5501 | }, | ||
5502 | LintCompletion { | ||
5503 | label: "clippy::mut_range_bound", | ||
5504 | description: r##"Checks for loops which have a range bound that is a mutable variable"##, | ||
5505 | }, | ||
5506 | LintCompletion { | ||
5507 | label: "clippy::mutable_key_type", | ||
5508 | description: r##"Checks for sets/maps with mutable key types."##, | ||
5509 | }, | ||
5510 | LintCompletion { | ||
5511 | label: "clippy::mutex_atomic", | ||
5512 | description: r##"Checks for usages of `Mutex<X>` where an atomic will do."##, | ||
5513 | }, | ||
5514 | LintCompletion { | ||
5515 | label: "clippy::mutex_integer", | ||
5516 | description: r##"Checks for usages of `Mutex<X>` where `X` is an integral\ntype."##, | ||
5517 | }, | ||
5518 | LintCompletion { | ||
5519 | label: "clippy::naive_bytecount", | ||
5520 | description: r##"Checks for naive byte counts"##, | ||
5521 | }, | ||
5522 | LintCompletion { | ||
5523 | label: "clippy::needless_arbitrary_self_type", | ||
5524 | description: r##"The lint checks for `self` in fn parameters that\nspecify the `Self`-type explicitly"##, | ||
5525 | }, | ||
5526 | LintCompletion { | ||
5527 | label: "clippy::needless_bool", | ||
5528 | description: r##"Checks for expressions of the form `if c { true } else {\nfalse }` (or vice versa) and suggests using the condition directly."##, | ||
5529 | }, | ||
5530 | LintCompletion { | ||
5531 | label: "clippy::needless_borrow", | ||
5532 | description: r##"Checks for address of operations (`&`) that are going to\nbe dereferenced immediately by the compiler."##, | ||
5533 | }, | ||
5534 | LintCompletion { | ||
5535 | label: "clippy::needless_borrowed_reference", | ||
5536 | description: r##"Checks for useless borrowed references."##, | ||
5537 | }, | ||
5538 | LintCompletion { | ||
5539 | label: "clippy::needless_collect", | ||
5540 | description: r##"Checks for functions collecting an iterator when collect\nis not needed."##, | ||
5541 | }, | ||
5542 | LintCompletion { | ||
5543 | label: "clippy::needless_continue", | ||
5544 | description: r##"The lint checks for `if`-statements appearing in loops\nthat contain a `continue` statement in either their main blocks or their\n`else`-blocks, when omitting the `else`-block possibly with some\nrearrangement of code can make the code easier to understand."##, | ||
5545 | }, | ||
5546 | LintCompletion { | ||
5547 | label: "clippy::needless_doctest_main", | ||
5548 | description: r##"Checks for `fn main() { .. }` in doctests"##, | ||
5549 | }, | ||
5550 | LintCompletion { | ||
5551 | label: "clippy::needless_lifetimes", | ||
5552 | description: r##"Checks for lifetime annotations which can be removed by\nrelying on lifetime elision."##, | ||
5553 | }, | ||
5554 | LintCompletion { | ||
5555 | label: "clippy::needless_pass_by_value", | ||
5556 | description: r##"Checks for functions taking arguments by value, but not\nconsuming them in its\nbody."##, | ||
5557 | }, | ||
5558 | LintCompletion { | ||
5559 | label: "clippy::needless_question_mark", | ||
5560 | description: r##"Suggests alternatives for useless applications of `?` in terminating expressions"##, | ||
5561 | }, | ||
5562 | LintCompletion { | ||
5563 | label: "clippy::needless_range_loop", | ||
5564 | description: r##"Checks for looping over the range of `0..len` of some\ncollection just to get the values by index."##, | ||
5565 | }, | ||
5566 | LintCompletion { | ||
5567 | label: "clippy::needless_return", | ||
5568 | description: r##"Checks for return statements at the end of a block."##, | ||
5569 | }, | ||
5570 | LintCompletion { | ||
5571 | label: "clippy::needless_update", | ||
5572 | description: r##"Checks for needlessly including a base struct on update\nwhen all fields are changed anyway.\n\nThis lint is not applied to structs marked with\n[non_exhaustive](https://doc.rust-lang.org/reference/attributes/type_system.html)."##, | ||
5573 | }, | ||
5574 | LintCompletion { | ||
5575 | label: "clippy::neg_cmp_op_on_partial_ord", | ||
5576 | description: r##"Checks for the usage of negated comparison operators on types which only implement\n`PartialOrd` (e.g., `f64`)."##, | ||
5577 | }, | ||
5578 | LintCompletion { | ||
5579 | label: "clippy::neg_multiply", | ||
5580 | description: r##"Checks for multiplication by -1 as a form of negation."##, | ||
5581 | }, | ||
5582 | LintCompletion { | ||
5583 | label: "clippy::never_loop", | ||
5584 | description: r##"Checks for loops that will always `break`, `return` or\n`continue` an outer loop."##, | ||
5585 | }, | ||
5586 | LintCompletion { | ||
5587 | label: "clippy::new_ret_no_self", | ||
5588 | description: r##"Checks for `new` not returning a type that contains `Self`."##, | ||
5589 | }, | ||
5590 | LintCompletion { | ||
5591 | label: "clippy::new_without_default", | ||
5592 | description: r##"Checks for types with a `fn new() -> Self` method and no\nimplementation of\n[`Default`](https://doc.rust-lang.org/std/default/trait.Default.html)."##, | ||
5593 | }, | ||
5594 | LintCompletion { | ||
5595 | label: "clippy::no_effect", | ||
5596 | description: r##"Checks for statements which have no effect."##, | ||
5597 | }, | ||
5598 | LintCompletion { | ||
5599 | label: "clippy::non_ascii_literal", | ||
5600 | description: r##"Checks for non-ASCII characters in string literals."##, | ||
5601 | }, | ||
5602 | LintCompletion { | ||
5603 | label: "clippy::nonminimal_bool", | ||
5604 | description: r##"Checks for boolean expressions that can be written more\nconcisely."##, | ||
5605 | }, | ||
5606 | LintCompletion { | ||
5607 | label: "clippy::nonsensical_open_options", | ||
5608 | description: r##"Checks for duplicate open options as well as combinations\nthat make no sense."##, | ||
5609 | }, | ||
5610 | LintCompletion { | ||
5611 | label: "clippy::not_unsafe_ptr_arg_deref", | ||
5612 | description: r##"Checks for public functions that dereference raw pointer\narguments but are not marked unsafe."##, | ||
5613 | }, | ||
5614 | LintCompletion { | ||
5615 | label: "clippy::ok_expect", | ||
5616 | description: r##"Checks for usage of `ok().expect(..)`."##, | ||
5617 | }, | ||
5618 | LintCompletion { | ||
5619 | label: "clippy::op_ref", | ||
5620 | description: r##"Checks for arguments to `==` which have their address\ntaken to satisfy a bound\nand suggests to dereference the other argument instead"##, | ||
5621 | }, | ||
5622 | LintCompletion { | ||
5623 | label: "clippy::option_as_ref_deref", | ||
5624 | description: r##"Checks for usage of `_.as_ref().map(Deref::deref)` or it's aliases (such as String::as_str)."##, | ||
5625 | }, | ||
5626 | LintCompletion { | ||
5627 | label: "clippy::option_env_unwrap", | ||
5628 | description: r##"Checks for usage of `option_env!(...).unwrap()` and\nsuggests usage of the `env!` macro."##, | ||
5629 | }, | ||
5630 | LintCompletion { | ||
5631 | label: "clippy::option_if_let_else", | ||
5632 | description: r##"Lints usage of `if let Some(v) = ... { y } else { x }` which is more\nidiomatically done with `Option::map_or` (if the else bit is a pure\nexpression) or `Option::map_or_else` (if the else bit is an impure\nexpression)."##, | ||
5633 | }, | ||
5634 | LintCompletion { | ||
5635 | label: "clippy::option_map_or_none", | ||
5636 | description: r##"Checks for usage of `_.map_or(None, _)`."##, | ||
5637 | }, | ||
5638 | LintCompletion { | ||
5639 | label: "clippy::option_map_unit_fn", | ||
5640 | description: r##"Checks for usage of `option.map(f)` where f is a function\nor closure that returns the unit type `()`."##, | ||
5641 | }, | ||
5642 | LintCompletion { | ||
5643 | label: "clippy::option_option", | ||
5644 | description: r##"Checks for use of `Option<Option<_>>` in function signatures and type\ndefinitions"##, | ||
5645 | }, | ||
5646 | LintCompletion { | ||
5647 | label: "clippy::or_fun_call", | ||
5648 | description: r##"Checks for calls to `.or(foo(..))`, `.unwrap_or(foo(..))`,\netc., and suggests to use `or_else`, `unwrap_or_else`, etc., or\n`unwrap_or_default` instead."##, | ||
5649 | }, | ||
5650 | LintCompletion { | ||
5651 | label: "clippy::out_of_bounds_indexing", | ||
5652 | description: r##"Checks for out of bounds array indexing with a constant\nindex."##, | ||
5653 | }, | ||
5654 | LintCompletion { | ||
5655 | label: "clippy::overflow_check_conditional", | ||
5656 | description: r##"Detects classic underflow/overflow checks."##, | ||
5657 | }, | ||
5658 | LintCompletion { label: "clippy::panic", description: r##"Checks for usage of `panic!`."## }, | ||
5659 | LintCompletion { | ||
5660 | label: "clippy::panic_in_result_fn", | ||
5661 | description: r##"Checks for usage of `panic!`, `unimplemented!`, `todo!`, `unreachable!` or assertions in a function of type result."##, | ||
5662 | }, | ||
5663 | LintCompletion { | ||
5664 | label: "clippy::panic_params", | ||
5665 | description: r##"Nothing. This lint has been deprecated."##, | ||
5666 | }, | ||
5667 | LintCompletion { | ||
5668 | label: "clippy::panicking_unwrap", | ||
5669 | description: r##"Checks for calls of `unwrap[_err]()` that will always fail."##, | ||
5670 | }, | ||
5671 | LintCompletion { | ||
5672 | label: "clippy::partialeq_ne_impl", | ||
5673 | description: r##"Checks for manual re-implementations of `PartialEq::ne`."##, | ||
5674 | }, | ||
5675 | LintCompletion { | ||
5676 | label: "clippy::path_buf_push_overwrite", | ||
5677 | description: r##"* Checks for [push](https://doc.rust-lang.org/std/path/struct.PathBuf.html#method.push)\ncalls on `PathBuf` that can cause overwrites."##, | ||
5678 | }, | ||
5679 | LintCompletion { | ||
5680 | label: "clippy::pattern_type_mismatch", | ||
5681 | description: r##"Checks for patterns that aren't exact representations of the types\nthey are applied to.\n\nTo satisfy this lint, you will have to adjust either the expression that is matched\nagainst or the pattern itself, as well as the bindings that are introduced by the\nadjusted patterns. For matching you will have to either dereference the expression\nwith the `*` operator, or amend the patterns to explicitly match against `&<pattern>`\nor `&mut <pattern>` depending on the reference mutability. For the bindings you need\nto use the inverse. You can leave them as plain bindings if you wish for the value\nto be copied, but you must use `ref mut <variable>` or `ref <variable>` to construct\na reference into the matched structure.\n\nIf you are looking for a way to learn about ownership semantics in more detail, it\nis recommended to look at IDE options available to you to highlight types, lifetimes\nand reference semantics in your code. The available tooling would expose these things\nin a general way even outside of the various pattern matching mechanics. Of course\nthis lint can still be used to highlight areas of interest and ensure a good understanding\nof ownership semantics."##, | ||
5682 | }, | ||
5683 | LintCompletion { | ||
5684 | label: "clippy::possible_missing_comma", | ||
5685 | description: r##"Checks for possible missing comma in an array. It lints if\nan array element is a binary operator expression and it lies on two lines."##, | ||
5686 | }, | ||
5687 | LintCompletion { | ||
5688 | label: "clippy::precedence", | ||
5689 | description: r##"Checks for operations where precedence may be unclear\nand suggests to add parentheses. Currently it catches the following:\n* mixed usage of arithmetic and bit shifting/combining operators without\nparentheses\n* a \"negative\" numeric literal (which is really a unary `-` followed by a\nnumeric literal)\n followed by a method call"##, | ||
5690 | }, | ||
5691 | LintCompletion { | ||
5692 | label: "clippy::print_literal", | ||
5693 | description: r##"This lint warns about the use of literals as `print!`/`println!` args."##, | ||
5694 | }, | ||
5695 | LintCompletion { | ||
5696 | label: "clippy::print_stderr", | ||
5697 | description: r##"Checks for printing on *stderr*. The purpose of this lint\nis to catch debugging remnants."##, | ||
5698 | }, | ||
5699 | LintCompletion { | ||
5700 | label: "clippy::print_stdout", | ||
5701 | description: r##"Checks for printing on *stdout*. The purpose of this lint\nis to catch debugging remnants."##, | ||
5702 | }, | ||
5703 | LintCompletion { | ||
5704 | label: "clippy::print_with_newline", | ||
5705 | description: r##"This lint warns when you use `print!()` with a format\nstring that ends in a newline."##, | ||
5706 | }, | ||
5707 | LintCompletion { | ||
5708 | label: "clippy::println_empty_string", | ||
5709 | description: r##"This lint warns when you use `println!(\"\")` to\nprint a newline."##, | ||
5710 | }, | ||
5711 | LintCompletion { | ||
5712 | label: "clippy::ptr_arg", | ||
5713 | description: r##"This lint checks for function arguments of type `&String`\nor `&Vec` unless the references are mutable. It will also suggest you\nreplace `.clone()` calls with the appropriate `.to_owned()`/`to_string()`\ncalls."##, | ||
5714 | }, | ||
5715 | LintCompletion { | ||
5716 | label: "clippy::ptr_as_ptr", | ||
5717 | description: r##"Checks for `as` casts between raw pointers without changing its mutability,\nnamely `*const T` to `*const U` and `*mut T` to `*mut U`."##, | ||
5718 | }, | ||
5719 | LintCompletion { | ||
5720 | label: "clippy::ptr_eq", | ||
5721 | description: r##"Use `std::ptr::eq` when applicable"##, | ||
5722 | }, | ||
5723 | LintCompletion { | ||
5724 | label: "clippy::ptr_offset_with_cast", | ||
5725 | description: r##"Checks for usage of the `offset` pointer method with a `usize` casted to an\n`isize`."##, | ||
5726 | }, | ||
5727 | LintCompletion { | ||
5728 | label: "clippy::pub_enum_variant_names", | ||
5729 | description: r##"Detects public enumeration variants that are\nprefixed or suffixed by the same characters."##, | ||
5730 | }, | ||
5731 | LintCompletion { | ||
5732 | label: "clippy::question_mark", | ||
5733 | description: r##"Checks for expressions that could be replaced by the question mark operator."##, | ||
5734 | }, | ||
5735 | LintCompletion { | ||
5736 | label: "clippy::range_minus_one", | ||
5737 | description: r##"Checks for inclusive ranges where 1 is subtracted from\nthe upper bound, e.g., `x..=(y-1)`."##, | ||
5738 | }, | ||
5739 | LintCompletion { | ||
5740 | label: "clippy::range_plus_one", | ||
5741 | description: r##"Checks for exclusive ranges where 1 is added to the\nupper bound, e.g., `x..(y+1)`."##, | ||
5742 | }, | ||
5743 | LintCompletion { | ||
5744 | label: "clippy::range_step_by_zero", | ||
5745 | description: r##"Nothing. This lint has been deprecated."##, | ||
5746 | }, | ||
5747 | LintCompletion { | ||
5748 | label: "clippy::range_zip_with_len", | ||
5749 | description: r##"Checks for zipping a collection with the range of\n`0.._.len()`."##, | ||
5750 | }, | ||
5751 | LintCompletion { | ||
5752 | label: "clippy::rc_buffer", | ||
5753 | description: r##"Checks for `Rc<T>` and `Arc<T>` when `T` is a mutable buffer type such as `String` or `Vec`."##, | ||
5754 | }, | ||
5755 | LintCompletion { | ||
5756 | label: "clippy::redundant_allocation", | ||
5757 | description: r##"Checks for use of redundant allocations anywhere in the code."##, | ||
5758 | }, | ||
5759 | LintCompletion { | ||
5760 | label: "clippy::redundant_clone", | ||
5761 | description: r##"Checks for a redundant `clone()` (and its relatives) which clones an owned\nvalue that is going to be dropped without further use."##, | ||
5762 | }, | ||
5763 | LintCompletion { | ||
5764 | label: "clippy::redundant_closure", | ||
5765 | description: r##"Checks for closures which just call another function where\nthe function can be called directly. `unsafe` functions or calls where types\nget adjusted are ignored."##, | ||
5766 | }, | ||
5767 | LintCompletion { | ||
5768 | label: "clippy::redundant_closure_call", | ||
5769 | description: r##"Detects closures called in the same expression where they\nare defined."##, | ||
5770 | }, | ||
5771 | LintCompletion { | ||
5772 | label: "clippy::redundant_closure_for_method_calls", | ||
5773 | description: r##"Checks for closures which only invoke a method on the closure\nargument and can be replaced by referencing the method directly."##, | ||
5774 | }, | ||
5775 | LintCompletion { | ||
5776 | label: "clippy::redundant_else", | ||
5777 | description: r##"Checks for `else` blocks that can be removed without changing semantics."##, | ||
5778 | }, | ||
5779 | LintCompletion { | ||
5780 | label: "clippy::redundant_field_names", | ||
5781 | description: r##"Checks for fields in struct literals where shorthands\ncould be used."##, | ||
5782 | }, | ||
5783 | LintCompletion { | ||
5784 | label: "clippy::redundant_pattern", | ||
5785 | description: r##"Checks for patterns in the form `name @ _`."##, | ||
5786 | }, | ||
5787 | LintCompletion { | ||
5788 | label: "clippy::redundant_pattern_matching", | ||
5789 | description: r##"Lint for redundant pattern matching over `Result`, `Option`,\n`std::task::Poll` or `std::net::IpAddr`"##, | ||
5790 | }, | ||
5791 | LintCompletion { | ||
5792 | label: "clippy::redundant_pub_crate", | ||
5793 | description: r##"Checks for items declared `pub(crate)` that are not crate visible because they\nare inside a private module."##, | ||
5794 | }, | ||
5795 | LintCompletion { | ||
5796 | label: "clippy::redundant_slicing", | ||
5797 | description: r##"Checks for redundant slicing expressions which use the full range, and\ndo not change the type."##, | ||
5798 | }, | ||
5799 | LintCompletion { | ||
5800 | label: "clippy::redundant_static_lifetimes", | ||
5801 | description: r##"Checks for constants and statics with an explicit `'static` lifetime."##, | ||
5802 | }, | ||
5803 | LintCompletion { | ||
5804 | label: "clippy::ref_in_deref", | ||
5805 | description: r##"Checks for references in expressions that use\nauto dereference."##, | ||
5806 | }, | ||
5807 | LintCompletion { | ||
5808 | label: "clippy::ref_option_ref", | ||
5809 | description: r##"Checks for usage of `&Option<&T>`."##, | ||
5810 | }, | ||
5811 | LintCompletion { | ||
5812 | label: "clippy::regex_macro", | ||
5813 | description: r##"Nothing. This lint has been deprecated."##, | ||
5814 | }, | ||
5815 | LintCompletion { | ||
5816 | label: "clippy::repeat_once", | ||
5817 | description: r##"Checks for usage of `.repeat(1)` and suggest the following method for each types.\n- `.to_string()` for `str`\n- `.clone()` for `String`\n- `.to_vec()` for `slice`"##, | ||
5818 | }, | ||
5819 | LintCompletion { | ||
5820 | label: "clippy::replace_consts", | ||
5821 | description: r##"Nothing. This lint has been deprecated."##, | ||
5822 | }, | ||
5823 | LintCompletion { | ||
5824 | label: "clippy::rest_pat_in_fully_bound_structs", | ||
5825 | description: r##"Checks for unnecessary '..' pattern binding on struct when all fields are explicitly matched."##, | ||
5826 | }, | ||
5827 | LintCompletion { | ||
5828 | label: "clippy::result_map_or_into_option", | ||
5829 | description: r##"Checks for usage of `_.map_or(None, Some)`."##, | ||
5830 | }, | ||
5831 | LintCompletion { | ||
5832 | label: "clippy::result_map_unit_fn", | ||
5833 | description: r##"Checks for usage of `result.map(f)` where f is a function\nor closure that returns the unit type `()`."##, | ||
5834 | }, | ||
5835 | LintCompletion { | ||
5836 | label: "clippy::result_unit_err", | ||
5837 | description: r##"Checks for public functions that return a `Result`\nwith an `Err` type of `()`. It suggests using a custom type that\nimplements [`std::error::Error`]."##, | ||
5838 | }, | ||
5839 | LintCompletion { | ||
5840 | label: "clippy::reversed_empty_ranges", | ||
5841 | description: r##"Checks for range expressions `x..y` where both `x` and `y`\nare constant and `x` is greater or equal to `y`."##, | ||
5842 | }, | ||
5843 | LintCompletion { | ||
5844 | label: "clippy::same_functions_in_if_condition", | ||
5845 | description: r##"Checks for consecutive `if`s with the same function call."##, | ||
5846 | }, | ||
5847 | LintCompletion { | ||
5848 | label: "clippy::same_item_push", | ||
5849 | description: r##"Checks whether a for loop is being used to push a constant\nvalue into a Vec."##, | ||
5850 | }, | ||
5851 | LintCompletion { | ||
5852 | label: "clippy::search_is_some", | ||
5853 | description: r##"Checks for an iterator or string search (such as `find()`,\n`position()`, or `rposition()`) followed by a call to `is_some()`."##, | ||
5854 | }, | ||
5855 | LintCompletion { | ||
5856 | label: "clippy::self_assignment", | ||
5857 | description: r##"Checks for explicit self-assignments."##, | ||
5858 | }, | ||
5859 | LintCompletion { | ||
5860 | label: "clippy::semicolon_if_nothing_returned", | ||
5861 | description: r##"Looks for blocks of expressions and fires if the last expression returns `()`\nbut is not followed by a semicolon."##, | ||
5862 | }, | ||
5863 | LintCompletion { | ||
5864 | label: "clippy::serde_api_misuse", | ||
5865 | description: r##"Checks for mis-uses of the serde API."##, | ||
5866 | }, | ||
5867 | LintCompletion { | ||
5868 | label: "clippy::shadow_reuse", | ||
5869 | description: r##"Checks for bindings that shadow other bindings already in\nscope, while reusing the original value."##, | ||
5870 | }, | ||
5871 | LintCompletion { | ||
5872 | label: "clippy::shadow_same", | ||
5873 | description: r##"Checks for bindings that shadow other bindings already in\nscope, while just changing reference level or mutability."##, | ||
5874 | }, | ||
5875 | LintCompletion { | ||
5876 | label: "clippy::shadow_unrelated", | ||
5877 | description: r##"Checks for bindings that shadow other bindings already in\nscope, either without a initialization or with one that does not even use\nthe original value."##, | ||
5878 | }, | ||
5879 | LintCompletion { | ||
5880 | label: "clippy::short_circuit_statement", | ||
5881 | description: r##"Checks for the use of short circuit boolean conditions as\na\nstatement."##, | ||
5882 | }, | ||
5883 | LintCompletion { | ||
5884 | label: "clippy::should_assert_eq", | ||
5885 | description: r##"Nothing. This lint has been deprecated."##, | ||
5886 | }, | ||
5887 | LintCompletion { | ||
5888 | label: "clippy::should_implement_trait", | ||
5889 | description: r##"Checks for methods that should live in a trait\nimplementation of a `std` trait (see [llogiq's blog\npost](http://llogiq.github.io/2015/07/30/traits.html) for further\ninformation) instead of an inherent implementation."##, | ||
5890 | }, | ||
5891 | LintCompletion { | ||
5892 | label: "clippy::similar_names", | ||
5893 | description: r##"Checks for names that are very similar and thus confusing."##, | ||
5894 | }, | ||
5895 | LintCompletion { | ||
5896 | label: "clippy::single_char_add_str", | ||
5897 | description: r##"Warns when using `push_str`/`insert_str` with a single-character string literal\nwhere `push`/`insert` with a `char` would work fine."##, | ||
5898 | }, | ||
5899 | LintCompletion { | ||
5900 | label: "clippy::single_char_pattern", | ||
5901 | description: r##"Checks for string methods that receive a single-character\n`str` as an argument, e.g., `_.split(\"x\")`."##, | ||
5902 | }, | ||
5903 | LintCompletion { | ||
5904 | label: "clippy::single_component_path_imports", | ||
5905 | description: r##"Checking for imports with single component use path."##, | ||
5906 | }, | ||
5907 | LintCompletion { | ||
5908 | label: "clippy::single_element_loop", | ||
5909 | description: r##"Checks whether a for loop has a single element."##, | ||
5910 | }, | ||
5911 | LintCompletion { | ||
5912 | label: "clippy::single_match", | ||
5913 | description: r##"Checks for matches with a single arm where an `if let`\nwill usually suffice."##, | ||
5914 | }, | ||
5915 | LintCompletion { | ||
5916 | label: "clippy::single_match_else", | ||
5917 | description: r##"Checks for matches with two arms where an `if let else` will\nusually suffice."##, | ||
5918 | }, | ||
5919 | LintCompletion { | ||
5920 | label: "clippy::size_of_in_element_count", | ||
5921 | description: r##"Detects expressions where\n`size_of::<T>` or `size_of_val::<T>` is used as a\ncount of elements of type `T`"##, | ||
5922 | }, | ||
5923 | LintCompletion { | ||
5924 | label: "clippy::skip_while_next", | ||
5925 | description: r##"Checks for usage of `_.skip_while(condition).next()`."##, | ||
5926 | }, | ||
5927 | LintCompletion { | ||
5928 | label: "clippy::slow_vector_initialization", | ||
5929 | description: r##"Checks slow zero-filled vector initialization"##, | ||
5930 | }, | ||
5931 | LintCompletion { | ||
5932 | label: "clippy::stable_sort_primitive", | ||
5933 | description: r##"When sorting primitive values (integers, bools, chars, as well\nas arrays, slices, and tuples of such items), it is better to\nuse an unstable sort than a stable sort."##, | ||
5934 | }, | ||
5935 | LintCompletion { | ||
5936 | label: "clippy::str_to_string", | ||
5937 | description: r##"This lint checks for `.to_string()` method calls on values of type `&str`."##, | ||
5938 | }, | ||
5939 | LintCompletion { | ||
5940 | label: "clippy::string_add", | ||
5941 | description: r##"Checks for all instances of `x + _` where `x` is of type\n`String`, but only if [`string_add_assign`](#string_add_assign) does *not*\nmatch."##, | ||
5942 | }, | ||
5943 | LintCompletion { | ||
5944 | label: "clippy::string_add_assign", | ||
5945 | description: r##"Checks for string appends of the form `x = x + y` (without\n`let`!)."##, | ||
5946 | }, | ||
5947 | LintCompletion { | ||
5948 | label: "clippy::string_extend_chars", | ||
5949 | description: r##"Checks for the use of `.extend(s.chars())` where s is a\n`&str` or `String`."##, | ||
5950 | }, | ||
5951 | LintCompletion { | ||
5952 | label: "clippy::string_from_utf8_as_bytes", | ||
5953 | description: r##"Check if the string is transformed to byte array and casted back to string."##, | ||
5954 | }, | ||
5955 | LintCompletion { | ||
5956 | label: "clippy::string_lit_as_bytes", | ||
5957 | description: r##"Checks for the `as_bytes` method called on string literals\nthat contain only ASCII characters."##, | ||
5958 | }, | ||
5959 | LintCompletion { | ||
5960 | label: "clippy::string_to_string", | ||
5961 | description: r##"This lint checks for `.to_string()` method calls on values of type `String`."##, | ||
5962 | }, | ||
5963 | LintCompletion { | ||
5964 | label: "clippy::struct_excessive_bools", | ||
5965 | description: r##"Checks for excessive\nuse of bools in structs."##, | ||
5966 | }, | ||
5967 | LintCompletion { | ||
5968 | label: "clippy::suboptimal_flops", | ||
5969 | description: r##"Looks for floating-point expressions that\ncan be expressed using built-in methods to improve both\naccuracy and performance."##, | ||
5970 | }, | ||
5971 | LintCompletion { | ||
5972 | label: "clippy::suspicious_arithmetic_impl", | ||
5973 | description: r##"Lints for suspicious operations in impls of arithmetic operators, e.g.\nsubtracting elements in an Add impl."##, | ||
5974 | }, | ||
5975 | LintCompletion { | ||
5976 | label: "clippy::suspicious_assignment_formatting", | ||
5977 | description: r##"Checks for use of the non-existent `=*`, `=!` and `=-`\noperators."##, | ||
5978 | }, | ||
5979 | LintCompletion { | ||
5980 | label: "clippy::suspicious_else_formatting", | ||
5981 | description: r##"Checks for formatting of `else`. It lints if the `else`\nis followed immediately by a newline or the `else` seems to be missing."##, | ||
5982 | }, | ||
5983 | LintCompletion { | ||
5984 | label: "clippy::suspicious_map", | ||
5985 | description: r##"Checks for calls to `map` followed by a `count`."##, | ||
5986 | }, | ||
5987 | LintCompletion { | ||
5988 | label: "clippy::suspicious_op_assign_impl", | ||
5989 | description: r##"Lints for suspicious operations in impls of OpAssign, e.g.\nsubtracting elements in an AddAssign impl."##, | ||
5990 | }, | ||
5991 | LintCompletion { | ||
5992 | label: "clippy::suspicious_operation_groupings", | ||
5993 | description: r##"Checks for unlikely usages of binary operators that are almost\ncertainly typos and/or copy/paste errors, given the other usages\nof binary operators nearby."##, | ||
5994 | }, | ||
5995 | LintCompletion { | ||
5996 | label: "clippy::suspicious_unary_op_formatting", | ||
5997 | description: r##"Checks the formatting of a unary operator on the right hand side\nof a binary operator. It lints if there is no space between the binary and unary operators,\nbut there is a space between the unary and its operand."##, | ||
5998 | }, | ||
5999 | LintCompletion { | ||
6000 | label: "clippy::tabs_in_doc_comments", | ||
6001 | description: r##"Checks doc comments for usage of tab characters."##, | ||
6002 | }, | ||
6003 | LintCompletion { | ||
6004 | label: "clippy::temporary_assignment", | ||
6005 | description: r##"Checks for construction of a structure or tuple just to\nassign a value in it."##, | ||
6006 | }, | ||
6007 | LintCompletion { | ||
6008 | label: "clippy::temporary_cstring_as_ptr", | ||
6009 | description: r##"Nothing. This lint has been deprecated."##, | ||
6010 | }, | ||
6011 | LintCompletion { | ||
6012 | label: "clippy::to_digit_is_some", | ||
6013 | description: r##"Checks for `.to_digit(..).is_some()` on `char`s."##, | ||
6014 | }, | ||
6015 | LintCompletion { | ||
6016 | label: "clippy::to_string_in_display", | ||
6017 | description: r##"Checks for uses of `to_string()` in `Display` traits."##, | ||
6018 | }, | ||
6019 | LintCompletion { label: "clippy::todo", description: r##"Checks for usage of `todo!`."## }, | ||
6020 | LintCompletion { | ||
6021 | label: "clippy::too_many_arguments", | ||
6022 | description: r##"Checks for functions with too many parameters."##, | ||
6023 | }, | ||
6024 | LintCompletion { | ||
6025 | label: "clippy::too_many_lines", | ||
6026 | description: r##"Checks for functions with a large amount of lines."##, | ||
6027 | }, | ||
6028 | LintCompletion { | ||
6029 | label: "clippy::toplevel_ref_arg", | ||
6030 | description: r##"Checks for function arguments and let bindings denoted as\n`ref`."##, | ||
6031 | }, | ||
6032 | LintCompletion { | ||
6033 | label: "clippy::trait_duplication_in_bounds", | ||
6034 | description: r##"Checks for cases where generics are being used and multiple\nsyntax specifications for trait bounds are used simultaneously."##, | ||
6035 | }, | ||
6036 | LintCompletion { | ||
6037 | label: "clippy::transmute_bytes_to_str", | ||
6038 | description: r##"Checks for transmutes from a `&[u8]` to a `&str`."##, | ||
6039 | }, | ||
6040 | LintCompletion { | ||
6041 | label: "clippy::transmute_float_to_int", | ||
6042 | description: r##"Checks for transmutes from a float to an integer."##, | ||
6043 | }, | ||
6044 | LintCompletion { | ||
6045 | label: "clippy::transmute_int_to_bool", | ||
6046 | description: r##"Checks for transmutes from an integer to a `bool`."##, | ||
6047 | }, | ||
6048 | LintCompletion { | ||
6049 | label: "clippy::transmute_int_to_char", | ||
6050 | description: r##"Checks for transmutes from an integer to a `char`."##, | ||
6051 | }, | ||
6052 | LintCompletion { | ||
6053 | label: "clippy::transmute_int_to_float", | ||
6054 | description: r##"Checks for transmutes from an integer to a float."##, | ||
6055 | }, | ||
6056 | LintCompletion { | ||
6057 | label: "clippy::transmute_ptr_to_ptr", | ||
6058 | description: r##"Checks for transmutes from a pointer to a pointer, or\nfrom a reference to a reference."##, | ||
6059 | }, | ||
6060 | LintCompletion { | ||
6061 | label: "clippy::transmute_ptr_to_ref", | ||
6062 | description: r##"Checks for transmutes from a pointer to a reference."##, | ||
6063 | }, | ||
6064 | LintCompletion { | ||
6065 | label: "clippy::transmutes_expressible_as_ptr_casts", | ||
6066 | description: r##"Checks for transmutes that could be a pointer cast."##, | ||
6067 | }, | ||
6068 | LintCompletion { | ||
6069 | label: "clippy::transmuting_null", | ||
6070 | description: r##"Checks for transmute calls which would receive a null pointer."##, | ||
6071 | }, | ||
6072 | LintCompletion { | ||
6073 | label: "clippy::trivial_regex", | ||
6074 | description: r##"Checks for trivial [regex](https://crates.io/crates/regex)\ncreation (with `Regex::new`, `RegexBuilder::new`, or `RegexSet::new`)."##, | ||
6075 | }, | ||
6076 | LintCompletion { | ||
6077 | label: "clippy::trivially_copy_pass_by_ref", | ||
6078 | description: r##"Checks for functions taking arguments by reference, where\nthe argument type is `Copy` and small enough to be more efficient to always\npass by value."##, | ||
6079 | }, | ||
6080 | LintCompletion { | ||
6081 | label: "clippy::try_err", | ||
6082 | description: r##"Checks for usages of `Err(x)?`."##, | ||
6083 | }, | ||
6084 | LintCompletion { | ||
6085 | label: "clippy::type_complexity", | ||
6086 | description: r##"Checks for types used in structs, parameters and `let`\ndeclarations above a certain complexity threshold."##, | ||
6087 | }, | ||
6088 | LintCompletion { | ||
6089 | label: "clippy::type_repetition_in_bounds", | ||
6090 | description: r##"This lint warns about unnecessary type repetitions in trait bounds"##, | ||
6091 | }, | ||
6092 | LintCompletion { | ||
6093 | label: "clippy::undropped_manually_drops", | ||
6094 | description: r##"Prevents the safe `std::mem::drop` function from being called on `std::mem::ManuallyDrop`."##, | ||
6095 | }, | ||
6096 | LintCompletion { | ||
6097 | label: "clippy::unicode_not_nfc", | ||
6098 | description: r##"Checks for string literals that contain Unicode in a form\nthat is not equal to its\n[NFC-recomposition](http://www.unicode.org/reports/tr15/#Norm_Forms)."##, | ||
6099 | }, | ||
6100 | LintCompletion { | ||
6101 | label: "clippy::unimplemented", | ||
6102 | description: r##"Checks for usage of `unimplemented!`."##, | ||
6103 | }, | ||
6104 | LintCompletion { | ||
6105 | label: "clippy::uninit_assumed_init", | ||
6106 | description: r##"Checks for `MaybeUninit::uninit().assume_init()`."##, | ||
6107 | }, | ||
6108 | LintCompletion { | ||
6109 | label: "clippy::unit_arg", | ||
6110 | description: r##"Checks for passing a unit value as an argument to a function without using a\nunit literal (`()`)."##, | ||
6111 | }, | ||
6112 | LintCompletion { | ||
6113 | label: "clippy::unit_cmp", | ||
6114 | description: r##"Checks for comparisons to unit. This includes all binary\ncomparisons (like `==` and `<`) and asserts."##, | ||
6115 | }, | ||
6116 | LintCompletion { | ||
6117 | label: "clippy::unit_return_expecting_ord", | ||
6118 | description: r##"Checks for functions that expect closures of type\nFn(...) -> Ord where the implemented closure returns the unit type.\nThe lint also suggests to remove the semi-colon at the end of the statement if present."##, | ||
6119 | }, | ||
6120 | LintCompletion { | ||
6121 | label: "clippy::unknown_clippy_lints", | ||
6122 | description: r##"Nothing. This lint has been deprecated."##, | ||
6123 | }, | ||
6124 | LintCompletion { | ||
6125 | label: "clippy::unnecessary_cast", | ||
6126 | description: r##"Checks for casts to the same type, casts of int literals to integer types\nand casts of float literals to float types."##, | ||
6127 | }, | ||
6128 | LintCompletion { | ||
6129 | label: "clippy::unnecessary_filter_map", | ||
6130 | description: r##"Checks for `filter_map` calls which could be replaced by `filter` or `map`.\nMore specifically it checks if the closure provided is only performing one of the\nfilter or map operations and suggests the appropriate option."##, | ||
6131 | }, | ||
6132 | LintCompletion { | ||
6133 | label: "clippy::unnecessary_fold", | ||
6134 | description: r##"Checks for using `fold` when a more succinct alternative exists.\nSpecifically, this checks for `fold`s which could be replaced by `any`, `all`,\n`sum` or `product`."##, | ||
6135 | }, | ||
6136 | LintCompletion { | ||
6137 | label: "clippy::unnecessary_lazy_evaluations", | ||
6138 | description: r##"As the counterpart to `or_fun_call`, this lint looks for unnecessary\nlazily evaluated closures on `Option` and `Result`.\n\nThis lint suggests changing the following functions, when eager evaluation results in\nsimpler code:\n - `unwrap_or_else` to `unwrap_or`\n - `and_then` to `and`\n - `or_else` to `or`\n - `get_or_insert_with` to `get_or_insert`\n - `ok_or_else` to `ok_or`"##, | ||
6139 | }, | ||
6140 | LintCompletion { | ||
6141 | label: "clippy::unnecessary_mut_passed", | ||
6142 | description: r##"Detects passing a mutable reference to a function that only\nrequires an immutable reference."##, | ||
6143 | }, | ||
6144 | LintCompletion { | ||
6145 | label: "clippy::unnecessary_operation", | ||
6146 | description: r##"Checks for expression statements that can be reduced to a\nsub-expression."##, | ||
6147 | }, | ||
6148 | LintCompletion { | ||
6149 | label: "clippy::unnecessary_sort_by", | ||
6150 | description: r##"Detects uses of `Vec::sort_by` passing in a closure\nwhich compares the two arguments, either directly or indirectly."##, | ||
6151 | }, | ||
6152 | LintCompletion { | ||
6153 | label: "clippy::unnecessary_unwrap", | ||
6154 | description: r##"Checks for calls of `unwrap[_err]()` that cannot fail."##, | ||
6155 | }, | ||
6156 | LintCompletion { | ||
6157 | label: "clippy::unnecessary_wraps", | ||
6158 | description: r##"Checks for private functions that only return `Ok` or `Some`."##, | ||
6159 | }, | ||
6160 | LintCompletion { | ||
6161 | label: "clippy::unneeded_field_pattern", | ||
6162 | description: r##"Checks for structure field patterns bound to wildcards."##, | ||
6163 | }, | ||
6164 | LintCompletion { | ||
6165 | label: "clippy::unneeded_wildcard_pattern", | ||
6166 | description: r##"Checks for tuple patterns with a wildcard\npattern (`_`) is next to a rest pattern (`..`).\n\n_NOTE_: While `_, ..` means there is at least one element left, `..`\nmeans there are 0 or more elements left. This can make a difference\nwhen refactoring, but shouldn't result in errors in the refactored code,\nsince the wildcard pattern isn't used anyway."##, | ||
6167 | }, | ||
6168 | LintCompletion { | ||
6169 | label: "clippy::unnested_or_patterns", | ||
6170 | description: r##"Checks for unnested or-patterns, e.g., `Some(0) | Some(2)` and\nsuggests replacing the pattern with a nested one, `Some(0 | 2)`.\n\nAnother way to think of this is that it rewrites patterns in\n*disjunctive normal form (DNF)* into *conjunctive normal form (CNF)*."##, | ||
6171 | }, | ||
6172 | LintCompletion { | ||
6173 | label: "clippy::unreachable", | ||
6174 | description: r##"Checks for usage of `unreachable!`."##, | ||
6175 | }, | ||
6176 | LintCompletion { | ||
6177 | label: "clippy::unreadable_literal", | ||
6178 | description: r##"Warns if a long integral or floating-point constant does\nnot contain underscores."##, | ||
6179 | }, | ||
6180 | LintCompletion { | ||
6181 | label: "clippy::unsafe_derive_deserialize", | ||
6182 | description: r##"Checks for deriving `serde::Deserialize` on a type that\nhas methods using `unsafe`."##, | ||
6183 | }, | ||
6184 | LintCompletion { | ||
6185 | label: "clippy::unsafe_removed_from_name", | ||
6186 | description: r##"Checks for imports that remove \"unsafe\" from an item's\nname."##, | ||
6187 | }, | ||
6188 | LintCompletion { | ||
6189 | label: "clippy::unsafe_vector_initialization", | ||
6190 | description: r##"Nothing. This lint has been deprecated."##, | ||
6191 | }, | ||
6192 | LintCompletion { | ||
6193 | label: "clippy::unseparated_literal_suffix", | ||
6194 | description: r##"Warns if literal suffixes are not separated by an\nunderscore."##, | ||
6195 | }, | ||
6196 | LintCompletion { | ||
6197 | label: "clippy::unsound_collection_transmute", | ||
6198 | description: r##"Checks for transmutes between collections whose\ntypes have different ABI, size or alignment."##, | ||
6199 | }, | ||
6200 | LintCompletion { | ||
6201 | label: "clippy::unstable_as_mut_slice", | ||
6202 | description: r##"Nothing. This lint has been deprecated."##, | ||
6203 | }, | ||
6204 | LintCompletion { | ||
6205 | label: "clippy::unstable_as_slice", | ||
6206 | description: r##"Nothing. This lint has been deprecated."##, | ||
6207 | }, | ||
6208 | LintCompletion { | ||
6209 | label: "clippy::unused_collect", | ||
6210 | description: r##"Nothing. This lint has been deprecated."##, | ||
6211 | }, | ||
6212 | LintCompletion { | ||
6213 | label: "clippy::unused_io_amount", | ||
6214 | description: r##"Checks for unused written/read amount."##, | ||
6215 | }, | ||
6216 | LintCompletion { | ||
6217 | label: "clippy::unused_label", | ||
6218 | description: r##"Nothing. This lint has been deprecated."##, | ||
6219 | }, | ||
6220 | LintCompletion { | ||
6221 | label: "clippy::unused_self", | ||
6222 | description: r##"Checks methods that contain a `self` argument but don't use it"##, | ||
6223 | }, | ||
6224 | LintCompletion { | ||
6225 | label: "clippy::unused_unit", | ||
6226 | description: r##"Checks for unit (`()`) expressions that can be removed."##, | ||
6227 | }, | ||
6228 | LintCompletion { | ||
6229 | label: "clippy::unusual_byte_groupings", | ||
6230 | description: r##"Warns if hexadecimal or binary literals are not grouped\nby nibble or byte."##, | ||
6231 | }, | ||
6232 | LintCompletion { | ||
6233 | label: "clippy::unwrap_in_result", | ||
6234 | description: r##"Checks for functions of type Result that contain `expect()` or `unwrap()`"##, | ||
6235 | }, | ||
6236 | LintCompletion { | ||
6237 | label: "clippy::unwrap_used", | ||
6238 | description: r##"Checks for `.unwrap()` calls on `Option`s and on `Result`s."##, | ||
6239 | }, | ||
6240 | LintCompletion { | ||
6241 | label: "clippy::upper_case_acronyms", | ||
6242 | description: r##"Checks for fully capitalized names and optionally names containing a capitalized acronym."##, | ||
6243 | }, | ||
6244 | LintCompletion { | ||
6245 | label: "clippy::use_debug", | ||
6246 | description: r##"Checks for use of `Debug` formatting. The purpose of this\nlint is to catch debugging remnants."##, | ||
6247 | }, | ||
6248 | LintCompletion { | ||
6249 | label: "clippy::use_self", | ||
6250 | description: r##"Checks for unnecessary repetition of structure name when a\nreplacement with `Self` is applicable."##, | ||
6251 | }, | ||
6252 | LintCompletion { | ||
6253 | label: "clippy::used_underscore_binding", | ||
6254 | description: r##"Checks for the use of bindings with a single leading\nunderscore."##, | ||
6255 | }, | ||
6256 | LintCompletion { | ||
6257 | label: "clippy::useless_asref", | ||
6258 | description: r##"Checks for usage of `.as_ref()` or `.as_mut()` where the\ntypes before and after the call are the same."##, | ||
6259 | }, | ||
6260 | LintCompletion { | ||
6261 | label: "clippy::useless_attribute", | ||
6262 | description: r##"Checks for `extern crate` and `use` items annotated with\nlint attributes.\n\nThis lint permits `#[allow(unused_imports)]`, `#[allow(deprecated)]`,\n`#[allow(unreachable_pub)]`, `#[allow(clippy::wildcard_imports)]` and\n`#[allow(clippy::enum_glob_use)]` on `use` items and `#[allow(unused_imports)]` on\n`extern crate` items with a `#[macro_use]` attribute."##, | ||
6263 | }, | ||
6264 | LintCompletion { | ||
6265 | label: "clippy::useless_conversion", | ||
6266 | description: r##"Checks for `Into`, `TryInto`, `From`, `TryFrom`, or `IntoIter` calls\nwhich uselessly convert to the same type."##, | ||
6267 | }, | ||
6268 | LintCompletion { | ||
6269 | label: "clippy::useless_format", | ||
6270 | description: r##"Checks for the use of `format!(\"string literal with no\nargument\")` and `format!(\"{}\", foo)` where `foo` is a string."##, | ||
6271 | }, | ||
6272 | LintCompletion { | ||
6273 | label: "clippy::useless_let_if_seq", | ||
6274 | description: r##"Checks for variable declarations immediately followed by a\nconditional affectation."##, | ||
6275 | }, | ||
6276 | LintCompletion { | ||
6277 | label: "clippy::useless_transmute", | ||
6278 | description: r##"Checks for transmutes to the original type of the object\nand transmutes that could be a cast."##, | ||
6279 | }, | ||
6280 | LintCompletion { | ||
6281 | label: "clippy::useless_vec", | ||
6282 | description: r##"Checks for usage of `&vec![..]` when using `&[..]` would\nbe possible."##, | ||
6283 | }, | ||
6284 | LintCompletion { | ||
6285 | label: "clippy::vec_box", | ||
6286 | description: r##"Checks for use of `Vec<Box<T>>` where T: Sized anywhere in the code.\nCheck the [Box documentation](https://doc.rust-lang.org/std/boxed/index.html) for more information."##, | ||
6287 | }, | ||
6288 | LintCompletion { | ||
6289 | label: "clippy::vec_init_then_push", | ||
6290 | description: r##"Checks for calls to `push` immediately after creating a new `Vec`."##, | ||
6291 | }, | ||
6292 | LintCompletion { | ||
6293 | label: "clippy::vec_resize_to_zero", | ||
6294 | description: r##"Finds occurrences of `Vec::resize(0, an_int)`"##, | ||
6295 | }, | ||
6296 | LintCompletion { | ||
6297 | label: "clippy::verbose_bit_mask", | ||
6298 | description: r##"Checks for bit masks that can be replaced by a call\nto `trailing_zeros`"##, | ||
6299 | }, | ||
6300 | LintCompletion { | ||
6301 | label: "clippy::verbose_file_reads", | ||
6302 | description: r##"Checks for use of File::read_to_end and File::read_to_string."##, | ||
6303 | }, | ||
6304 | LintCompletion { | ||
6305 | label: "clippy::vtable_address_comparisons", | ||
6306 | description: r##"Checks for comparisons with an address of a trait vtable."##, | ||
6307 | }, | ||
6308 | LintCompletion { | ||
6309 | label: "clippy::while_immutable_condition", | ||
6310 | description: r##"Checks whether variables used within while loop condition\ncan be (and are) mutated in the body."##, | ||
6311 | }, | ||
6312 | LintCompletion { | ||
6313 | label: "clippy::while_let_loop", | ||
6314 | description: r##"Detects `loop + match` combinations that are easier\nwritten as a `while let` loop."##, | ||
6315 | }, | ||
6316 | LintCompletion { | ||
6317 | label: "clippy::while_let_on_iterator", | ||
6318 | description: r##"Checks for `while let` expressions on iterators."##, | ||
6319 | }, | ||
6320 | LintCompletion { | ||
6321 | label: "clippy::wildcard_dependencies", | ||
6322 | description: r##"Checks for wildcard dependencies in the `Cargo.toml`."##, | ||
6323 | }, | ||
6324 | LintCompletion { | ||
6325 | label: "clippy::wildcard_enum_match_arm", | ||
6326 | description: r##"Checks for wildcard enum matches using `_`."##, | ||
6327 | }, | ||
6328 | LintCompletion { | ||
6329 | label: "clippy::wildcard_imports", | ||
6330 | description: r##"Checks for wildcard imports `use _::*`."##, | ||
6331 | }, | ||
6332 | LintCompletion { | ||
6333 | label: "clippy::wildcard_in_or_patterns", | ||
6334 | description: r##"Checks for wildcard pattern used with others patterns in same match arm."##, | ||
6335 | }, | ||
6336 | LintCompletion { | ||
6337 | label: "clippy::write_literal", | ||
6338 | description: r##"This lint warns about the use of literals as `write!`/`writeln!` args."##, | ||
6339 | }, | ||
6340 | LintCompletion { | ||
6341 | label: "clippy::write_with_newline", | ||
6342 | description: r##"This lint warns when you use `write!()` with a format\nstring that\nends in a newline."##, | ||
6343 | }, | ||
6344 | LintCompletion { | ||
6345 | label: "clippy::writeln_empty_string", | ||
6346 | description: r##"This lint warns when you use `writeln!(buf, \"\")` to\nprint a newline."##, | ||
6347 | }, | ||
6348 | LintCompletion { | ||
6349 | label: "clippy::wrong_pub_self_convention", | ||
6350 | description: r##"This is the same as\n[`wrong_self_convention`](#wrong_self_convention), but for public items."##, | ||
6351 | }, | ||
6352 | LintCompletion { | ||
6353 | label: "clippy::wrong_self_convention", | ||
6354 | description: r##"Checks for methods with certain name prefixes and which\ndoesn't match how self is taken. The actual rules are:\n\n|Prefix |`self` taken |\n|-------|----------------------|\n|`as_` |`&self` or `&mut self`|\n|`from_`| none |\n|`into_`|`self` |\n|`is_` |`&self` or none |\n|`to_` |`&self` |"##, | ||
6355 | }, | ||
6356 | LintCompletion { | ||
6357 | label: "clippy::wrong_transmute", | ||
6358 | description: r##"Checks for transmutes that can't ever be correct on any\narchitecture."##, | ||
6359 | }, | ||
6360 | LintCompletion { | ||
6361 | label: "clippy::zero_divided_by_zero", | ||
6362 | description: r##"Checks for `0.0 / 0.0`."##, | ||
6363 | }, | ||
6364 | LintCompletion { | ||
6365 | label: "clippy::zero_prefixed_literal", | ||
6366 | description: r##"Warns if an integral constant literal starts with `0`."##, | ||
6367 | }, | ||
6368 | LintCompletion { | ||
6369 | label: "clippy::zero_ptr", | ||
6370 | description: r##"Catch casts from `0` to some pointer type"##, | ||
6371 | }, | ||
6372 | LintCompletion { | ||
6373 | label: "clippy::zero_sized_map_values", | ||
6374 | description: r##"Checks for maps with zero-sized value types anywhere in the code."##, | ||
6375 | }, | ||
6376 | LintCompletion { | ||
6377 | label: "clippy::zst_offset", | ||
6378 | description: r##"Checks for `offset(_)`, `wrapping_`{`add`, `sub`}, etc. on raw pointers to\nzero-sized types"##, | ||
6379 | }, | ||
6380 | ]; | ||
diff --git a/crates/ide_db/src/apply_change.rs b/crates/ide_db/src/apply_change.rs index 23974cff8..104ee113f 100644 --- a/crates/ide_db/src/apply_change.rs +++ b/crates/ide_db/src/apply_change.rs | |||
@@ -32,7 +32,7 @@ struct RootChange { | |||
32 | 32 | ||
33 | impl fmt::Debug for RootChange { | 33 | impl fmt::Debug for RootChange { |
34 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | 34 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
35 | fmt.debug_struct("AnalysisChange") | 35 | fmt.debug_struct("RootChange") |
36 | .field("added", &self.added.len()) | 36 | .field("added", &self.added.len()) |
37 | .field("removed", &self.removed.len()) | 37 | .field("removed", &self.removed.len()) |
38 | .finish() | 38 | .finish() |
diff --git a/crates/ide_db/src/helpers.rs b/crates/ide_db/src/helpers.rs index bc7aee110..f9de8ce0e 100644 --- a/crates/ide_db/src/helpers.rs +++ b/crates/ide_db/src/helpers.rs | |||
@@ -45,6 +45,10 @@ impl FamousDefs<'_, '_> { | |||
45 | self.find_crate("core") | 45 | self.find_crate("core") |
46 | } | 46 | } |
47 | 47 | ||
48 | pub fn core_cmp_Ord(&self) -> Option<Trait> { | ||
49 | self.find_trait("core:cmp:Ord") | ||
50 | } | ||
51 | |||
48 | pub fn core_convert_From(&self) -> Option<Trait> { | 52 | pub fn core_convert_From(&self) -> Option<Trait> { |
49 | self.find_trait("core:convert:From") | 53 | self.find_trait("core:convert:From") |
50 | } | 54 | } |
diff --git a/crates/ide_db/src/helpers/famous_defs_fixture.rs b/crates/ide_db/src/helpers/famous_defs_fixture.rs index 5e88de64d..bb4e9666b 100644 --- a/crates/ide_db/src/helpers/famous_defs_fixture.rs +++ b/crates/ide_db/src/helpers/famous_defs_fixture.rs | |||
@@ -1,5 +1,15 @@ | |||
1 | //- /libcore.rs crate:core | 1 | //- /libcore.rs crate:core |
2 | //! Signatures of traits, types and functions from the core lib for use in tests. | 2 | //! Signatures of traits, types and functions from the core lib for use in tests. |
3 | pub mod cmp { | ||
4 | |||
5 | pub trait Ord { | ||
6 | fn cmp(&self, other: &Self) -> Ordering; | ||
7 | fn max(self, other: Self) -> Self; | ||
8 | fn min(self, other: Self) -> Self; | ||
9 | fn clamp(self, min: Self, max: Self) -> Self; | ||
10 | } | ||
11 | } | ||
12 | |||
3 | pub mod convert { | 13 | pub mod convert { |
4 | pub trait From<T> { | 14 | pub trait From<T> { |
5 | fn from(t: T) -> Self; | 15 | fn from(t: T) -> Self; |
@@ -109,6 +119,7 @@ pub mod option { | |||
109 | 119 | ||
110 | pub mod prelude { | 120 | pub mod prelude { |
111 | pub use crate::{ | 121 | pub use crate::{ |
122 | cmp::Ord, | ||
112 | convert::From, | 123 | convert::From, |
113 | default::Default, | 124 | default::Default, |
114 | iter::{IntoIterator, Iterator}, | 125 | iter::{IntoIterator, Iterator}, |
diff --git a/crates/ide_db/src/search.rs b/crates/ide_db/src/search.rs index ba8bea002..ddcfbd3f3 100644 --- a/crates/ide_db/src/search.rs +++ b/crates/ide_db/src/search.rs | |||
@@ -416,10 +416,11 @@ impl<'a> FindUsages<'a> { | |||
416 | sink: &mut dyn FnMut(FileId, FileReference) -> bool, | 416 | sink: &mut dyn FnMut(FileId, FileReference) -> bool, |
417 | ) -> bool { | 417 | ) -> bool { |
418 | match NameClass::classify(self.sema, name) { | 418 | match NameClass::classify(self.sema, name) { |
419 | Some(NameClass::PatFieldShorthand { local_def: _, field_ref }) => { | 419 | Some(NameClass::PatFieldShorthand { local_def: _, field_ref }) |
420 | if !matches!(self.def, Definition::Field(_) if &field_ref == self.def) { | 420 | if matches!( |
421 | return false; | 421 | self.def, Definition::Field(_) if &field_ref == self.def |
422 | } | 422 | ) => |
423 | { | ||
423 | let FileRange { file_id, range } = self.sema.original_range(name.syntax()); | 424 | let FileRange { file_id, range } = self.sema.original_range(name.syntax()); |
424 | let reference = FileReference { | 425 | let reference = FileReference { |
425 | range, | 426 | range, |
@@ -429,6 +430,12 @@ impl<'a> FindUsages<'a> { | |||
429 | }; | 430 | }; |
430 | sink(file_id, reference) | 431 | sink(file_id, reference) |
431 | } | 432 | } |
433 | Some(NameClass::ConstReference(def)) if *self.def == def => { | ||
434 | let FileRange { file_id, range } = self.sema.original_range(name.syntax()); | ||
435 | let reference = | ||
436 | FileReference { range, name: ast::NameLike::Name(name.clone()), access: None }; | ||
437 | sink(file_id, reference) | ||
438 | } | ||
432 | _ => false, // not a usage | 439 | _ => false, // not a usage |
433 | } | 440 | } |
434 | } | 441 | } |
diff --git a/crates/ide_db/src/source_change.rs b/crates/ide_db/src/source_change.rs index f76bac151..b36455d49 100644 --- a/crates/ide_db/src/source_change.rs +++ b/crates/ide_db/src/source_change.rs | |||
@@ -1,7 +1,7 @@ | |||
1 | //! This modules defines type to represent changes to the source code, that flow | 1 | //! This modules defines type to represent changes to the source code, that flow |
2 | //! from the server to the client. | 2 | //! from the server to the client. |
3 | //! | 3 | //! |
4 | //! It can be viewed as a dual for `AnalysisChange`. | 4 | //! It can be viewed as a dual for `Change`. |
5 | 5 | ||
6 | use std::{ | 6 | use std::{ |
7 | collections::hash_map::Entry, | 7 | collections::hash_map::Entry, |
diff --git a/crates/mbe/Cargo.toml b/crates/mbe/Cargo.toml index ef0907194..8b5cca22c 100644 --- a/crates/mbe/Cargo.toml +++ b/crates/mbe/Cargo.toml | |||
@@ -19,3 +19,5 @@ parser = { path = "../parser", version = "0.0.0" } | |||
19 | tt = { path = "../tt", version = "0.0.0" } | 19 | tt = { path = "../tt", version = "0.0.0" } |
20 | test_utils = { path = "../test_utils", version = "0.0.0" } | 20 | test_utils = { path = "../test_utils", version = "0.0.0" } |
21 | 21 | ||
22 | [dev-dependencies] | ||
23 | profile = { path = "../profile" } | ||
diff --git a/crates/mbe/src/benchmark.rs b/crates/mbe/src/benchmark.rs new file mode 100644 index 000000000..6d81be880 --- /dev/null +++ b/crates/mbe/src/benchmark.rs | |||
@@ -0,0 +1,211 @@ | |||
1 | //! This module add real world mbe example for benchmark tests | ||
2 | |||
3 | use rustc_hash::FxHashMap; | ||
4 | use syntax::{ | ||
5 | ast::{self, NameOwner}, | ||
6 | AstNode, SmolStr, | ||
7 | }; | ||
8 | use test_utils::{bench, bench_fixture, skip_slow_tests}; | ||
9 | |||
10 | use crate::{ | ||
11 | ast_to_token_tree, | ||
12 | parser::{Op, RepeatKind, Separator}, | ||
13 | MacroRules, | ||
14 | }; | ||
15 | |||
16 | #[test] | ||
17 | fn benchmark_parse_macro_rules() { | ||
18 | if skip_slow_tests() { | ||
19 | return; | ||
20 | } | ||
21 | let rules = macro_rules_fixtures_tt(); | ||
22 | let hash: usize = { | ||
23 | let _pt = bench("mbe parse macro rules"); | ||
24 | rules.values().map(|it| MacroRules::parse(it).unwrap().rules.len()).sum() | ||
25 | }; | ||
26 | assert_eq!(hash, 1144); | ||
27 | } | ||
28 | |||
29 | #[test] | ||
30 | fn benchmark_expand_macro_rules() { | ||
31 | if skip_slow_tests() { | ||
32 | return; | ||
33 | } | ||
34 | let rules = macro_rules_fixtures(); | ||
35 | let invocations = invocation_fixtures(&rules); | ||
36 | |||
37 | let hash: usize = { | ||
38 | let _pt = bench("mbe expand macro rules"); | ||
39 | invocations | ||
40 | .into_iter() | ||
41 | .map(|(id, tt)| { | ||
42 | let res = rules[&id].expand(&tt); | ||
43 | if res.err.is_some() { | ||
44 | // FIXME: | ||
45 | // Currently `invocation_fixtures` will generate some correct invocations but | ||
46 | // cannot be expanded by mbe. We ignore errors here. | ||
47 | // See: https://github.com/rust-analyzer/rust-analyzer/issues/4777 | ||
48 | eprintln!("err from {} {:?}", id, res.err); | ||
49 | } | ||
50 | res.value.token_trees.len() | ||
51 | }) | ||
52 | .sum() | ||
53 | }; | ||
54 | assert_eq!(hash, 66995); | ||
55 | } | ||
56 | |||
57 | fn macro_rules_fixtures() -> FxHashMap<String, MacroRules> { | ||
58 | macro_rules_fixtures_tt() | ||
59 | .into_iter() | ||
60 | .map(|(id, tt)| (id, MacroRules::parse(&tt).unwrap())) | ||
61 | .collect() | ||
62 | } | ||
63 | |||
64 | fn macro_rules_fixtures_tt() -> FxHashMap<String, tt::Subtree> { | ||
65 | let fixture = bench_fixture::numerous_macro_rules(); | ||
66 | let source_file = ast::SourceFile::parse(&fixture).ok().unwrap(); | ||
67 | |||
68 | source_file | ||
69 | .syntax() | ||
70 | .descendants() | ||
71 | .filter_map(ast::MacroRules::cast) | ||
72 | .map(|rule| { | ||
73 | let id = rule.name().unwrap().to_string(); | ||
74 | let (def_tt, _) = ast_to_token_tree(&rule.token_tree().unwrap()).unwrap(); | ||
75 | (id, def_tt) | ||
76 | }) | ||
77 | .collect() | ||
78 | } | ||
79 | |||
80 | // Generate random invocation fixtures from rules | ||
81 | fn invocation_fixtures(rules: &FxHashMap<String, MacroRules>) -> Vec<(String, tt::Subtree)> { | ||
82 | let mut seed = 123456789; | ||
83 | let mut res = Vec::new(); | ||
84 | |||
85 | for (name, it) in rules { | ||
86 | for rule in &it.rules { | ||
87 | // Generate twice | ||
88 | for _ in 0..2 { | ||
89 | let mut subtree = tt::Subtree::default(); | ||
90 | for op in rule.lhs.iter() { | ||
91 | collect_from_op(op, &mut subtree, &mut seed); | ||
92 | } | ||
93 | res.push((name.clone(), subtree)); | ||
94 | } | ||
95 | } | ||
96 | } | ||
97 | return res; | ||
98 | |||
99 | fn collect_from_op(op: &Op, parent: &mut tt::Subtree, seed: &mut usize) { | ||
100 | return match op { | ||
101 | Op::Var { kind, .. } => match kind.as_ref().map(|it| it.as_str()) { | ||
102 | Some("ident") => parent.token_trees.push(make_ident("foo")), | ||
103 | Some("ty") => parent.token_trees.push(make_ident("Foo")), | ||
104 | Some("tt") => parent.token_trees.push(make_ident("foo")), | ||
105 | Some("vis") => parent.token_trees.push(make_ident("pub")), | ||
106 | Some("pat") => parent.token_trees.push(make_ident("foo")), | ||
107 | Some("path") => parent.token_trees.push(make_ident("foo")), | ||
108 | Some("literal") => parent.token_trees.push(make_literal("1")), | ||
109 | Some("expr") => parent.token_trees.push(make_ident("foo").into()), | ||
110 | Some("lifetime") => { | ||
111 | parent.token_trees.push(make_punct('\'')); | ||
112 | parent.token_trees.push(make_ident("a")); | ||
113 | } | ||
114 | Some("block") => { | ||
115 | parent.token_trees.push(make_subtree(tt::DelimiterKind::Brace, None)) | ||
116 | } | ||
117 | Some("item") => { | ||
118 | parent.token_trees.push(make_ident("fn")); | ||
119 | parent.token_trees.push(make_ident("foo")); | ||
120 | parent.token_trees.push(make_subtree(tt::DelimiterKind::Parenthesis, None)); | ||
121 | parent.token_trees.push(make_subtree(tt::DelimiterKind::Brace, None)); | ||
122 | } | ||
123 | Some("meta") => { | ||
124 | parent.token_trees.push(make_ident("foo")); | ||
125 | parent.token_trees.push(make_subtree(tt::DelimiterKind::Parenthesis, None)); | ||
126 | } | ||
127 | |||
128 | None => (), | ||
129 | Some(kind) => panic!("Unhandled kind {}", kind), | ||
130 | }, | ||
131 | Op::Leaf(leaf) => parent.token_trees.push(leaf.clone().into()), | ||
132 | Op::Repeat { tokens, kind, separator } => { | ||
133 | let max = 10; | ||
134 | let cnt = match kind { | ||
135 | RepeatKind::ZeroOrMore => rand(seed) % max, | ||
136 | RepeatKind::OneOrMore => 1 + rand(seed) % max, | ||
137 | RepeatKind::ZeroOrOne => rand(seed) % 2, | ||
138 | }; | ||
139 | for i in 0..cnt { | ||
140 | for it in tokens.iter() { | ||
141 | collect_from_op(it, parent, seed); | ||
142 | } | ||
143 | if i + 1 != cnt { | ||
144 | if let Some(sep) = separator { | ||
145 | match sep { | ||
146 | Separator::Literal(it) => parent | ||
147 | .token_trees | ||
148 | .push(tt::Leaf::Literal(it.clone().into()).into()), | ||
149 | Separator::Ident(it) => parent | ||
150 | .token_trees | ||
151 | .push(tt::Leaf::Ident(it.clone().into()).into()), | ||
152 | Separator::Puncts(puncts) => { | ||
153 | for it in puncts { | ||
154 | parent | ||
155 | .token_trees | ||
156 | .push(tt::Leaf::Punct(it.clone().into()).into()) | ||
157 | } | ||
158 | } | ||
159 | }; | ||
160 | } | ||
161 | } | ||
162 | } | ||
163 | } | ||
164 | Op::Subtree { tokens, delimiter } => { | ||
165 | let mut subtree = | ||
166 | tt::Subtree { delimiter: delimiter.clone(), token_trees: Vec::new() }; | ||
167 | tokens.iter().for_each(|it| { | ||
168 | collect_from_op(it, &mut subtree, seed); | ||
169 | }); | ||
170 | parent.token_trees.push(subtree.into()); | ||
171 | } | ||
172 | }; | ||
173 | |||
174 | // Simple linear congruential generator for determistic result | ||
175 | fn rand(seed: &mut usize) -> usize { | ||
176 | let a = 1664525; | ||
177 | let c = 1013904223; | ||
178 | *seed = usize::wrapping_add(usize::wrapping_mul(*seed, a), c); | ||
179 | return *seed; | ||
180 | } | ||
181 | fn make_ident(ident: &str) -> tt::TokenTree { | ||
182 | tt::Leaf::Ident(tt::Ident { id: tt::TokenId::unspecified(), text: SmolStr::new(ident) }) | ||
183 | .into() | ||
184 | } | ||
185 | fn make_punct(char: char) -> tt::TokenTree { | ||
186 | tt::Leaf::Punct(tt::Punct { | ||
187 | id: tt::TokenId::unspecified(), | ||
188 | char, | ||
189 | spacing: tt::Spacing::Alone, | ||
190 | }) | ||
191 | .into() | ||
192 | } | ||
193 | fn make_literal(lit: &str) -> tt::TokenTree { | ||
194 | tt::Leaf::Literal(tt::Literal { | ||
195 | id: tt::TokenId::unspecified(), | ||
196 | text: SmolStr::new(lit), | ||
197 | }) | ||
198 | .into() | ||
199 | } | ||
200 | fn make_subtree( | ||
201 | kind: tt::DelimiterKind, | ||
202 | token_trees: Option<Vec<tt::TokenTree>>, | ||
203 | ) -> tt::TokenTree { | ||
204 | tt::Subtree { | ||
205 | delimiter: Some(tt::Delimiter { id: tt::TokenId::unspecified(), kind }), | ||
206 | token_trees: token_trees.unwrap_or_default(), | ||
207 | } | ||
208 | .into() | ||
209 | } | ||
210 | } | ||
211 | } | ||
diff --git a/crates/mbe/src/expander/matcher.rs b/crates/mbe/src/expander/matcher.rs index 800931cd1..e3bd4c09a 100644 --- a/crates/mbe/src/expander/matcher.rs +++ b/crates/mbe/src/expander/matcher.rs | |||
@@ -3,15 +3,13 @@ | |||
3 | use crate::{ | 3 | use crate::{ |
4 | expander::{Binding, Bindings, Fragment}, | 4 | expander::{Binding, Bindings, Fragment}, |
5 | parser::{Op, RepeatKind, Separator}, | 5 | parser::{Op, RepeatKind, Separator}, |
6 | subtree_source::SubtreeTokenSource, | ||
7 | tt_iter::TtIter, | 6 | tt_iter::TtIter, |
8 | ExpandError, MetaTemplate, | 7 | ExpandError, MetaTemplate, |
9 | }; | 8 | }; |
10 | 9 | ||
11 | use super::ExpandResult; | 10 | use super::ExpandResult; |
12 | use parser::{FragmentKind::*, TreeSink}; | 11 | use parser::FragmentKind::*; |
13 | use syntax::{SmolStr, SyntaxKind}; | 12 | use syntax::SmolStr; |
14 | use tt::buffer::{Cursor, TokenBuffer}; | ||
15 | 13 | ||
16 | impl Bindings { | 14 | impl Bindings { |
17 | fn push_optional(&mut self, name: &SmolStr) { | 15 | fn push_optional(&mut self, name: &SmolStr) { |
@@ -409,68 +407,6 @@ impl<'a> TtIter<'a> { | |||
409 | .into()) | 407 | .into()) |
410 | } | 408 | } |
411 | 409 | ||
412 | fn expect_fragment( | ||
413 | &mut self, | ||
414 | fragment_kind: parser::FragmentKind, | ||
415 | ) -> ExpandResult<Option<tt::TokenTree>> { | ||
416 | struct OffsetTokenSink<'a> { | ||
417 | cursor: Cursor<'a>, | ||
418 | error: bool, | ||
419 | } | ||
420 | |||
421 | impl<'a> TreeSink for OffsetTokenSink<'a> { | ||
422 | fn token(&mut self, kind: SyntaxKind, mut n_tokens: u8) { | ||
423 | if kind == SyntaxKind::LIFETIME_IDENT { | ||
424 | n_tokens = 2; | ||
425 | } | ||
426 | for _ in 0..n_tokens { | ||
427 | self.cursor = self.cursor.bump_subtree(); | ||
428 | } | ||
429 | } | ||
430 | fn start_node(&mut self, _kind: SyntaxKind) {} | ||
431 | fn finish_node(&mut self) {} | ||
432 | fn error(&mut self, _error: parser::ParseError) { | ||
433 | self.error = true; | ||
434 | } | ||
435 | } | ||
436 | |||
437 | let buffer = TokenBuffer::from_tokens(&self.inner.as_slice()); | ||
438 | let mut src = SubtreeTokenSource::new(&buffer); | ||
439 | let mut sink = OffsetTokenSink { cursor: buffer.begin(), error: false }; | ||
440 | |||
441 | parser::parse_fragment(&mut src, &mut sink, fragment_kind); | ||
442 | |||
443 | let mut err = None; | ||
444 | if !sink.cursor.is_root() || sink.error { | ||
445 | err = Some(err!("expected {:?}", fragment_kind)); | ||
446 | } | ||
447 | |||
448 | let mut curr = buffer.begin(); | ||
449 | let mut res = vec![]; | ||
450 | |||
451 | if sink.cursor.is_root() { | ||
452 | while curr != sink.cursor { | ||
453 | if let Some(token) = curr.token_tree() { | ||
454 | res.push(token); | ||
455 | } | ||
456 | curr = curr.bump(); | ||
457 | } | ||
458 | } | ||
459 | self.inner = self.inner.as_slice()[res.len()..].iter(); | ||
460 | if res.len() == 0 && err.is_none() { | ||
461 | err = Some(err!("no tokens consumed")); | ||
462 | } | ||
463 | let res = match res.len() { | ||
464 | 1 => Some(res[0].cloned()), | ||
465 | 0 => None, | ||
466 | _ => Some(tt::TokenTree::Subtree(tt::Subtree { | ||
467 | delimiter: None, | ||
468 | token_trees: res.into_iter().map(|it| it.cloned()).collect(), | ||
469 | })), | ||
470 | }; | ||
471 | ExpandResult { value: res, err } | ||
472 | } | ||
473 | |||
474 | fn eat_vis(&mut self) -> Option<tt::TokenTree> { | 410 | fn eat_vis(&mut self) -> Option<tt::TokenTree> { |
475 | let mut fork = self.clone(); | 411 | let mut fork = self.clone(); |
476 | match fork.expect_fragment(Visibility) { | 412 | match fork.expect_fragment(Visibility) { |
diff --git a/crates/mbe/src/lib.rs b/crates/mbe/src/lib.rs index d80bd7a33..4c298f85f 100644 --- a/crates/mbe/src/lib.rs +++ b/crates/mbe/src/lib.rs | |||
@@ -12,6 +12,9 @@ mod subtree_source; | |||
12 | #[cfg(test)] | 12 | #[cfg(test)] |
13 | mod tests; | 13 | mod tests; |
14 | 14 | ||
15 | #[cfg(test)] | ||
16 | mod benchmark; | ||
17 | |||
15 | use std::fmt; | 18 | use std::fmt; |
16 | 19 | ||
17 | use test_utils::mark; | 20 | use test_utils::mark; |
@@ -62,8 +65,8 @@ impl fmt::Display for ExpandError { | |||
62 | } | 65 | } |
63 | 66 | ||
64 | pub use crate::syntax_bridge::{ | 67 | pub use crate::syntax_bridge::{ |
65 | ast_to_token_tree, parse_to_token_tree, syntax_node_to_token_tree, token_tree_to_syntax_node, | 68 | ast_to_token_tree, parse_exprs_with_sep, parse_to_token_tree, syntax_node_to_token_tree, |
66 | TokenMap, | 69 | token_tree_to_syntax_node, TokenMap, |
67 | }; | 70 | }; |
68 | 71 | ||
69 | /// This struct contains AST for a single `macro_rules` definition. What might | 72 | /// This struct contains AST for a single `macro_rules` definition. What might |
diff --git a/crates/mbe/src/syntax_bridge.rs b/crates/mbe/src/syntax_bridge.rs index 0cdc175be..aacae1026 100644 --- a/crates/mbe/src/syntax_bridge.rs +++ b/crates/mbe/src/syntax_bridge.rs | |||
@@ -10,8 +10,8 @@ use syntax::{ | |||
10 | }; | 10 | }; |
11 | use tt::buffer::{Cursor, TokenBuffer}; | 11 | use tt::buffer::{Cursor, TokenBuffer}; |
12 | 12 | ||
13 | use crate::subtree_source::SubtreeTokenSource; | ||
14 | use crate::ExpandError; | 13 | use crate::ExpandError; |
14 | use crate::{subtree_source::SubtreeTokenSource, tt_iter::TtIter}; | ||
15 | 15 | ||
16 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] | 16 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] |
17 | pub enum TokenTextRange { | 17 | pub enum TokenTextRange { |
@@ -112,6 +112,43 @@ pub fn parse_to_token_tree(text: &str) -> Option<(tt::Subtree, TokenMap)> { | |||
112 | Some((subtree, conv.id_alloc.map)) | 112 | Some((subtree, conv.id_alloc.map)) |
113 | } | 113 | } |
114 | 114 | ||
115 | /// Split token tree with seperate expr: $($e:expr)SEP* | ||
116 | pub fn parse_exprs_with_sep(tt: &tt::Subtree, sep: char) -> Vec<tt::Subtree> { | ||
117 | if tt.token_trees.is_empty() { | ||
118 | return Vec::new(); | ||
119 | } | ||
120 | |||
121 | let mut iter = TtIter::new(tt); | ||
122 | let mut res = Vec::new(); | ||
123 | |||
124 | while iter.peek_n(0).is_some() { | ||
125 | let expanded = iter.expect_fragment(FragmentKind::Expr); | ||
126 | if expanded.err.is_some() { | ||
127 | break; | ||
128 | } | ||
129 | |||
130 | res.push(match expanded.value { | ||
131 | None => break, | ||
132 | Some(tt @ tt::TokenTree::Leaf(_)) => { | ||
133 | tt::Subtree { delimiter: None, token_trees: vec![tt.into()] } | ||
134 | } | ||
135 | Some(tt::TokenTree::Subtree(tt)) => tt, | ||
136 | }); | ||
137 | |||
138 | let mut fork = iter.clone(); | ||
139 | if fork.expect_char(sep).is_err() { | ||
140 | break; | ||
141 | } | ||
142 | iter = fork; | ||
143 | } | ||
144 | |||
145 | if iter.peek_n(0).is_some() { | ||
146 | res.push(tt::Subtree { delimiter: None, token_trees: iter.into_iter().cloned().collect() }); | ||
147 | } | ||
148 | |||
149 | res | ||
150 | } | ||
151 | |||
115 | impl TokenMap { | 152 | impl TokenMap { |
116 | pub fn token_by_range(&self, relative_range: TextRange) -> Option<tt::TokenId> { | 153 | pub fn token_by_range(&self, relative_range: TextRange) -> Option<tt::TokenId> { |
117 | let &(token_id, _) = self.entries.iter().find(|(_, range)| match range { | 154 | let &(token_id, _) = self.entries.iter().find(|(_, range)| match range { |
@@ -176,7 +213,7 @@ fn doc_comment_text(comment: &ast::Comment) -> SmolStr { | |||
176 | 213 | ||
177 | // Quote the string | 214 | // Quote the string |
178 | // Note that `tt::Literal` expect an escaped string | 215 | // Note that `tt::Literal` expect an escaped string |
179 | let text = format!("{:?}", text.escape_default().to_string()); | 216 | let text = format!("{:?}", text.escape_debug().to_string()); |
180 | text.into() | 217 | text.into() |
181 | } | 218 | } |
182 | 219 | ||
diff --git a/crates/mbe/src/tests.rs b/crates/mbe/src/tests.rs index 1c467facd..f1eadcd1e 100644 --- a/crates/mbe/src/tests.rs +++ b/crates/mbe/src/tests.rs | |||
@@ -970,6 +970,29 @@ fn test_meta_doc_comments() { | |||
970 | } | 970 | } |
971 | 971 | ||
972 | #[test] | 972 | #[test] |
973 | fn test_meta_doc_comments_non_latin() { | ||
974 | parse_macro( | ||
975 | r#" | ||
976 | macro_rules! foo { | ||
977 | ($(#[$ i:meta])+) => ( | ||
978 | $(#[$ i])+ | ||
979 | fn bar() {} | ||
980 | ) | ||
981 | } | ||
982 | "#, | ||
983 | ). | ||
984 | assert_expand_items( | ||
985 | r#"foo! { | ||
986 | /// 錦瑟無端五十弦,一弦一柱思華年。 | ||
987 | /** | ||
988 | 莊生曉夢迷蝴蝶,望帝春心託杜鵑。 | ||
989 | */ | ||
990 | }"#, | ||
991 | "# [doc = \" 錦瑟無端五十弦,一弦一柱思華年。\"] # [doc = \"\\\\n 莊生曉夢迷蝴蝶,望帝春心託杜鵑。\\\\n \"] fn bar () {}", | ||
992 | ); | ||
993 | } | ||
994 | |||
995 | #[test] | ||
973 | fn test_tt_block() { | 996 | fn test_tt_block() { |
974 | parse_macro( | 997 | parse_macro( |
975 | r#" | 998 | r#" |
diff --git a/crates/mbe/src/tt_iter.rs b/crates/mbe/src/tt_iter.rs index 46c420718..a362d31fc 100644 --- a/crates/mbe/src/tt_iter.rs +++ b/crates/mbe/src/tt_iter.rs | |||
@@ -1,5 +1,20 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | 2 | ||
3 | use crate::{subtree_source::SubtreeTokenSource, ExpandError, ExpandResult}; | ||
4 | |||
5 | use parser::TreeSink; | ||
6 | use syntax::SyntaxKind; | ||
7 | use tt::buffer::{Cursor, TokenBuffer}; | ||
8 | |||
9 | macro_rules! err { | ||
10 | () => { | ||
11 | ExpandError::BindingError(format!("")) | ||
12 | }; | ||
13 | ($($tt:tt)*) => { | ||
14 | ExpandError::BindingError(format!($($tt)*)) | ||
15 | }; | ||
16 | } | ||
17 | |||
3 | #[derive(Debug, Clone)] | 18 | #[derive(Debug, Clone)] |
4 | pub(crate) struct TtIter<'a> { | 19 | pub(crate) struct TtIter<'a> { |
5 | pub(crate) inner: std::slice::Iter<'a, tt::TokenTree>, | 20 | pub(crate) inner: std::slice::Iter<'a, tt::TokenTree>, |
@@ -56,6 +71,68 @@ impl<'a> TtIter<'a> { | |||
56 | } | 71 | } |
57 | } | 72 | } |
58 | 73 | ||
74 | pub(crate) fn expect_fragment( | ||
75 | &mut self, | ||
76 | fragment_kind: parser::FragmentKind, | ||
77 | ) -> ExpandResult<Option<tt::TokenTree>> { | ||
78 | struct OffsetTokenSink<'a> { | ||
79 | cursor: Cursor<'a>, | ||
80 | error: bool, | ||
81 | } | ||
82 | |||
83 | impl<'a> TreeSink for OffsetTokenSink<'a> { | ||
84 | fn token(&mut self, kind: SyntaxKind, mut n_tokens: u8) { | ||
85 | if kind == SyntaxKind::LIFETIME_IDENT { | ||
86 | n_tokens = 2; | ||
87 | } | ||
88 | for _ in 0..n_tokens { | ||
89 | self.cursor = self.cursor.bump_subtree(); | ||
90 | } | ||
91 | } | ||
92 | fn start_node(&mut self, _kind: SyntaxKind) {} | ||
93 | fn finish_node(&mut self) {} | ||
94 | fn error(&mut self, _error: parser::ParseError) { | ||
95 | self.error = true; | ||
96 | } | ||
97 | } | ||
98 | |||
99 | let buffer = TokenBuffer::from_tokens(&self.inner.as_slice()); | ||
100 | let mut src = SubtreeTokenSource::new(&buffer); | ||
101 | let mut sink = OffsetTokenSink { cursor: buffer.begin(), error: false }; | ||
102 | |||
103 | parser::parse_fragment(&mut src, &mut sink, fragment_kind); | ||
104 | |||
105 | let mut err = None; | ||
106 | if !sink.cursor.is_root() || sink.error { | ||
107 | err = Some(err!("expected {:?}", fragment_kind)); | ||
108 | } | ||
109 | |||
110 | let mut curr = buffer.begin(); | ||
111 | let mut res = vec![]; | ||
112 | |||
113 | if sink.cursor.is_root() { | ||
114 | while curr != sink.cursor { | ||
115 | if let Some(token) = curr.token_tree() { | ||
116 | res.push(token); | ||
117 | } | ||
118 | curr = curr.bump(); | ||
119 | } | ||
120 | } | ||
121 | self.inner = self.inner.as_slice()[res.len()..].iter(); | ||
122 | if res.len() == 0 && err.is_none() { | ||
123 | err = Some(err!("no tokens consumed")); | ||
124 | } | ||
125 | let res = match res.len() { | ||
126 | 1 => Some(res[0].cloned()), | ||
127 | 0 => None, | ||
128 | _ => Some(tt::TokenTree::Subtree(tt::Subtree { | ||
129 | delimiter: None, | ||
130 | token_trees: res.into_iter().map(|it| it.cloned()).collect(), | ||
131 | })), | ||
132 | }; | ||
133 | ExpandResult { value: res, err } | ||
134 | } | ||
135 | |||
59 | pub(crate) fn peek_n(&self, n: usize) -> Option<&tt::TokenTree> { | 136 | pub(crate) fn peek_n(&self, n: usize) -> Option<&tt::TokenTree> { |
60 | self.inner.as_slice().get(n) | 137 | self.inner.as_slice().get(n) |
61 | } | 138 | } |
diff --git a/crates/proc_macro_srv/src/rustc_server.rs b/crates/proc_macro_srv/src/rustc_server.rs index a8504f762..952b4a97f 100644 --- a/crates/proc_macro_srv/src/rustc_server.rs +++ b/crates/proc_macro_srv/src/rustc_server.rs | |||
@@ -14,7 +14,6 @@ use std::collections::HashMap; | |||
14 | use std::hash::Hash; | 14 | use std::hash::Hash; |
15 | use std::iter::FromIterator; | 15 | use std::iter::FromIterator; |
16 | use std::ops::Bound; | 16 | use std::ops::Bound; |
17 | use std::str::FromStr; | ||
18 | use std::{ascii, vec::IntoIter}; | 17 | use std::{ascii, vec::IntoIter}; |
19 | 18 | ||
20 | type Group = tt::Subtree; | 19 | type Group = tt::Subtree; |
@@ -278,6 +277,42 @@ impl server::FreeFunctions for Rustc { | |||
278 | } | 277 | } |
279 | } | 278 | } |
280 | 279 | ||
280 | fn subtree_replace_token_ids_with_unspecified(subtree: tt::Subtree) -> tt::Subtree { | ||
281 | tt::Subtree { | ||
282 | delimiter: subtree.delimiter.map(|d| tt::Delimiter { id: tt::TokenId::unspecified(), ..d }), | ||
283 | token_trees: subtree | ||
284 | .token_trees | ||
285 | .into_iter() | ||
286 | .map(|t| token_tree_replace_token_ids_with_unspecified(t)) | ||
287 | .collect(), | ||
288 | } | ||
289 | } | ||
290 | |||
291 | fn token_tree_replace_token_ids_with_unspecified(tt: tt::TokenTree) -> tt::TokenTree { | ||
292 | match tt { | ||
293 | tt::TokenTree::Leaf(leaf) => { | ||
294 | tt::TokenTree::Leaf(leaf_replace_token_ids_with_unspecified(leaf)) | ||
295 | } | ||
296 | tt::TokenTree::Subtree(subtree) => { | ||
297 | tt::TokenTree::Subtree(subtree_replace_token_ids_with_unspecified(subtree)) | ||
298 | } | ||
299 | } | ||
300 | } | ||
301 | |||
302 | fn leaf_replace_token_ids_with_unspecified(leaf: tt::Leaf) -> tt::Leaf { | ||
303 | match leaf { | ||
304 | tt::Leaf::Literal(lit) => { | ||
305 | tt::Leaf::Literal(tt::Literal { id: tt::TokenId::unspecified(), ..lit }) | ||
306 | } | ||
307 | tt::Leaf::Punct(punct) => { | ||
308 | tt::Leaf::Punct(tt::Punct { id: tt::TokenId::unspecified(), ..punct }) | ||
309 | } | ||
310 | tt::Leaf::Ident(ident) => { | ||
311 | tt::Leaf::Ident(tt::Ident { id: tt::TokenId::unspecified(), ..ident }) | ||
312 | } | ||
313 | } | ||
314 | } | ||
315 | |||
281 | impl server::TokenStream for Rustc { | 316 | impl server::TokenStream for Rustc { |
282 | fn new(&mut self) -> Self::TokenStream { | 317 | fn new(&mut self) -> Self::TokenStream { |
283 | Self::TokenStream::new() | 318 | Self::TokenStream::new() |
@@ -287,7 +322,8 @@ impl server::TokenStream for Rustc { | |||
287 | stream.is_empty() | 322 | stream.is_empty() |
288 | } | 323 | } |
289 | fn from_str(&mut self, src: &str) -> Self::TokenStream { | 324 | fn from_str(&mut self, src: &str) -> Self::TokenStream { |
290 | Self::TokenStream::from_str(src).expect("cannot parse string") | 325 | let (subtree, _) = mbe::parse_to_token_tree(src).expect("cannot parse string"); |
326 | TokenStream::with_subtree(subtree_replace_token_ids_with_unspecified(subtree)) | ||
291 | } | 327 | } |
292 | fn to_string(&mut self, stream: &Self::TokenStream) -> String { | 328 | fn to_string(&mut self, stream: &Self::TokenStream) -> String { |
293 | stream.to_string() | 329 | stream.to_string() |
diff --git a/crates/rust-analyzer/src/cli/analysis_bench.rs b/crates/rust-analyzer/src/cli/analysis_bench.rs index 8991f3bdb..ebbbf0596 100644 --- a/crates/rust-analyzer/src/cli/analysis_bench.rs +++ b/crates/rust-analyzer/src/cli/analysis_bench.rs | |||
@@ -68,7 +68,7 @@ impl BenchCmd { | |||
68 | load_out_dirs_from_check: self.load_output_dirs, | 68 | load_out_dirs_from_check: self.load_output_dirs, |
69 | with_proc_macro: self.with_proc_macro, | 69 | with_proc_macro: self.with_proc_macro, |
70 | }; | 70 | }; |
71 | let (mut host, vfs) = | 71 | let (mut host, vfs, _proc_macro) = |
72 | load_workspace_at(&self.path, &cargo_config, &load_cargo_config, &|_| {})?; | 72 | load_workspace_at(&self.path, &cargo_config, &load_cargo_config, &|_| {})?; |
73 | eprintln!("{:?}\n", start.elapsed()); | 73 | eprintln!("{:?}\n", start.elapsed()); |
74 | 74 | ||
diff --git a/crates/rust-analyzer/src/cli/analysis_stats.rs b/crates/rust-analyzer/src/cli/analysis_stats.rs index 9072d8944..ad0759bda 100644 --- a/crates/rust-analyzer/src/cli/analysis_stats.rs +++ b/crates/rust-analyzer/src/cli/analysis_stats.rs | |||
@@ -64,7 +64,7 @@ impl AnalysisStatsCmd { | |||
64 | load_out_dirs_from_check: self.load_output_dirs, | 64 | load_out_dirs_from_check: self.load_output_dirs, |
65 | with_proc_macro: self.with_proc_macro, | 65 | with_proc_macro: self.with_proc_macro, |
66 | }; | 66 | }; |
67 | let (host, vfs) = | 67 | let (host, vfs, _proc_macro) = |
68 | load_workspace_at(&self.path, &cargo_config, &load_cargo_config, &|_| {})?; | 68 | load_workspace_at(&self.path, &cargo_config, &load_cargo_config, &|_| {})?; |
69 | let db = host.raw_database(); | 69 | let db = host.raw_database(); |
70 | eprintln!("{:<20} {}", "Database loaded:", db_load_sw.elapsed()); | 70 | eprintln!("{:<20} {}", "Database loaded:", db_load_sw.elapsed()); |
diff --git a/crates/rust-analyzer/src/cli/diagnostics.rs b/crates/rust-analyzer/src/cli/diagnostics.rs index 876f6c44f..8b985716b 100644 --- a/crates/rust-analyzer/src/cli/diagnostics.rs +++ b/crates/rust-analyzer/src/cli/diagnostics.rs | |||
@@ -35,7 +35,8 @@ pub fn diagnostics( | |||
35 | ) -> Result<()> { | 35 | ) -> Result<()> { |
36 | let cargo_config = Default::default(); | 36 | let cargo_config = Default::default(); |
37 | let load_cargo_config = LoadCargoConfig { load_out_dirs_from_check, with_proc_macro }; | 37 | let load_cargo_config = LoadCargoConfig { load_out_dirs_from_check, with_proc_macro }; |
38 | let (host, _vfs) = load_workspace_at(path, &cargo_config, &load_cargo_config, &|_| {})?; | 38 | let (host, _vfs, _proc_macro) = |
39 | load_workspace_at(path, &cargo_config, &load_cargo_config, &|_| {})?; | ||
39 | let db = host.raw_database(); | 40 | let db = host.raw_database(); |
40 | let analysis = host.analysis(); | 41 | let analysis = host.analysis(); |
41 | 42 | ||
diff --git a/crates/rust-analyzer/src/cli/load_cargo.rs b/crates/rust-analyzer/src/cli/load_cargo.rs index 23442afac..310c36904 100644 --- a/crates/rust-analyzer/src/cli/load_cargo.rs +++ b/crates/rust-analyzer/src/cli/load_cargo.rs | |||
@@ -23,7 +23,7 @@ pub fn load_workspace_at( | |||
23 | cargo_config: &CargoConfig, | 23 | cargo_config: &CargoConfig, |
24 | load_config: &LoadCargoConfig, | 24 | load_config: &LoadCargoConfig, |
25 | progress: &dyn Fn(String), | 25 | progress: &dyn Fn(String), |
26 | ) -> Result<(AnalysisHost, vfs::Vfs)> { | 26 | ) -> Result<(AnalysisHost, vfs::Vfs, Option<ProcMacroClient>)> { |
27 | let root = AbsPathBuf::assert(std::env::current_dir()?.join(root)); | 27 | let root = AbsPathBuf::assert(std::env::current_dir()?.join(root)); |
28 | let root = ProjectManifest::discover_single(&root)?; | 28 | let root = ProjectManifest::discover_single(&root)?; |
29 | let workspace = ProjectWorkspace::load(root, cargo_config, progress)?; | 29 | let workspace = ProjectWorkspace::load(root, cargo_config, progress)?; |
@@ -35,7 +35,7 @@ pub fn load_workspace( | |||
35 | ws: ProjectWorkspace, | 35 | ws: ProjectWorkspace, |
36 | config: &LoadCargoConfig, | 36 | config: &LoadCargoConfig, |
37 | progress: &dyn Fn(String), | 37 | progress: &dyn Fn(String), |
38 | ) -> Result<(AnalysisHost, vfs::Vfs)> { | 38 | ) -> Result<(AnalysisHost, vfs::Vfs, Option<ProcMacroClient>)> { |
39 | let (sender, receiver) = unbounded(); | 39 | let (sender, receiver) = unbounded(); |
40 | let mut vfs = vfs::Vfs::default(); | 40 | let mut vfs = vfs::Vfs::default(); |
41 | let mut loader = { | 41 | let mut loader = { |
@@ -80,7 +80,7 @@ pub fn load_workspace( | |||
80 | log::debug!("crate graph: {:?}", crate_graph); | 80 | log::debug!("crate graph: {:?}", crate_graph); |
81 | let host = | 81 | let host = |
82 | load_crate_graph(crate_graph, project_folders.source_root_config, &mut vfs, &receiver); | 82 | load_crate_graph(crate_graph, project_folders.source_root_config, &mut vfs, &receiver); |
83 | Ok((host, vfs)) | 83 | Ok((host, vfs, proc_macro_client)) |
84 | } | 84 | } |
85 | 85 | ||
86 | fn load_crate_graph( | 86 | fn load_crate_graph( |
@@ -138,7 +138,8 @@ mod tests { | |||
138 | let cargo_config = Default::default(); | 138 | let cargo_config = Default::default(); |
139 | let load_cargo_config = | 139 | let load_cargo_config = |
140 | LoadCargoConfig { load_out_dirs_from_check: false, with_proc_macro: false }; | 140 | LoadCargoConfig { load_out_dirs_from_check: false, with_proc_macro: false }; |
141 | let (host, _vfs) = load_workspace_at(path, &cargo_config, &load_cargo_config, &|_| {})?; | 141 | let (host, _vfs, _proc_macro) = |
142 | load_workspace_at(path, &cargo_config, &load_cargo_config, &|_| {})?; | ||
142 | 143 | ||
143 | let n_crates = Crate::all(host.raw_database()).len(); | 144 | let n_crates = Crate::all(host.raw_database()).len(); |
144 | // RA has quite a few crates, but the exact count doesn't matter | 145 | // RA has quite a few crates, but the exact count doesn't matter |
diff --git a/crates/rust-analyzer/src/cli/ssr.rs b/crates/rust-analyzer/src/cli/ssr.rs index 71a8f8fb9..79f426fff 100644 --- a/crates/rust-analyzer/src/cli/ssr.rs +++ b/crates/rust-analyzer/src/cli/ssr.rs | |||
@@ -11,7 +11,7 @@ pub fn apply_ssr_rules(rules: Vec<SsrRule>) -> Result<()> { | |||
11 | let cargo_config = Default::default(); | 11 | let cargo_config = Default::default(); |
12 | let load_cargo_config = | 12 | let load_cargo_config = |
13 | LoadCargoConfig { load_out_dirs_from_check: true, with_proc_macro: true }; | 13 | LoadCargoConfig { load_out_dirs_from_check: true, with_proc_macro: true }; |
14 | let (host, vfs) = | 14 | let (host, vfs, _proc_macro) = |
15 | load_workspace_at(&std::env::current_dir()?, &cargo_config, &load_cargo_config, &|_| {})?; | 15 | load_workspace_at(&std::env::current_dir()?, &cargo_config, &load_cargo_config, &|_| {})?; |
16 | let db = host.raw_database(); | 16 | let db = host.raw_database(); |
17 | let mut match_finder = MatchFinder::at_first_file(db)?; | 17 | let mut match_finder = MatchFinder::at_first_file(db)?; |
@@ -38,7 +38,7 @@ pub fn search_for_patterns(patterns: Vec<SsrPattern>, debug_snippet: Option<Stri | |||
38 | let cargo_config = Default::default(); | 38 | let cargo_config = Default::default(); |
39 | let load_cargo_config = | 39 | let load_cargo_config = |
40 | LoadCargoConfig { load_out_dirs_from_check: true, with_proc_macro: true }; | 40 | LoadCargoConfig { load_out_dirs_from_check: true, with_proc_macro: true }; |
41 | let (host, _vfs) = | 41 | let (host, _vfs, _proc_macro) = |
42 | load_workspace_at(&std::env::current_dir()?, &cargo_config, &load_cargo_config, &|_| {})?; | 42 | load_workspace_at(&std::env::current_dir()?, &cargo_config, &load_cargo_config, &|_| {})?; |
43 | let db = host.raw_database(); | 43 | let db = host.raw_database(); |
44 | let mut match_finder = MatchFinder::at_first_file(db)?; | 44 | let mut match_finder = MatchFinder::at_first_file(db)?; |
diff --git a/crates/rust-analyzer/tests/rust-analyzer/main.rs b/crates/rust-analyzer/tests/rust-analyzer/main.rs index 80bde29b9..7545b4a34 100644 --- a/crates/rust-analyzer/tests/rust-analyzer/main.rs +++ b/crates/rust-analyzer/tests/rust-analyzer/main.rs | |||
@@ -54,7 +54,9 @@ version = "0.0.0" | |||
54 | use std::collections::Spam; | 54 | use std::collections::Spam; |
55 | "#, | 55 | "#, |
56 | ) | 56 | ) |
57 | .with_sysroot(true) | 57 | .with_config(serde_json::json!({ |
58 | "cargo": { "noSysroot": false } | ||
59 | })) | ||
58 | .server() | 60 | .server() |
59 | .wait_until_workspace_is_loaded(); | 61 | .wait_until_workspace_is_loaded(); |
60 | 62 | ||
@@ -451,7 +453,9 @@ fn main() {{}} | |||
451 | "#, | 453 | "#, |
452 | librs, libs | 454 | librs, libs |
453 | )) | 455 | )) |
454 | .with_sysroot(true) | 456 | .with_config(serde_json::json!({ |
457 | "cargo": { "noSysroot": false } | ||
458 | })) | ||
455 | .server() | 459 | .server() |
456 | .wait_until_workspace_is_loaded(); | 460 | .wait_until_workspace_is_loaded(); |
457 | 461 | ||
@@ -574,7 +578,10 @@ fn main() { | |||
574 | "###, | 578 | "###, |
575 | ) | 579 | ) |
576 | .with_config(serde_json::json!({ | 580 | .with_config(serde_json::json!({ |
577 | "cargo": { "loadOutDirsFromCheck": true } | 581 | "cargo": { |
582 | "loadOutDirsFromCheck": true, | ||
583 | "noSysroot": true, | ||
584 | } | ||
578 | })) | 585 | })) |
579 | .server() | 586 | .server() |
580 | .wait_until_workspace_is_loaded(); | 587 | .wait_until_workspace_is_loaded(); |
@@ -717,7 +724,10 @@ pub fn foo(_input: TokenStream) -> TokenStream { | |||
717 | "###, | 724 | "###, |
718 | ) | 725 | ) |
719 | .with_config(serde_json::json!({ | 726 | .with_config(serde_json::json!({ |
720 | "cargo": { "loadOutDirsFromCheck": true }, | 727 | "cargo": { |
728 | "loadOutDirsFromCheck": true, | ||
729 | "noSysroot": true, | ||
730 | }, | ||
721 | "procMacro": { | 731 | "procMacro": { |
722 | "enable": true, | 732 | "enable": true, |
723 | "server": PathBuf::from(env!("CARGO_BIN_EXE_rust-analyzer")), | 733 | "server": PathBuf::from(env!("CARGO_BIN_EXE_rust-analyzer")), |
diff --git a/crates/rust-analyzer/tests/rust-analyzer/support.rs b/crates/rust-analyzer/tests/rust-analyzer/support.rs index 453ce4fa8..6b774073d 100644 --- a/crates/rust-analyzer/tests/rust-analyzer/support.rs +++ b/crates/rust-analyzer/tests/rust-analyzer/support.rs | |||
@@ -20,7 +20,6 @@ use crate::testdir::TestDir; | |||
20 | 20 | ||
21 | pub(crate) struct Project<'a> { | 21 | pub(crate) struct Project<'a> { |
22 | fixture: &'a str, | 22 | fixture: &'a str, |
23 | with_sysroot: bool, | ||
24 | tmp_dir: Option<TestDir>, | 23 | tmp_dir: Option<TestDir>, |
25 | roots: Vec<PathBuf>, | 24 | roots: Vec<PathBuf>, |
26 | config: serde_json::Value, | 25 | config: serde_json::Value, |
@@ -32,8 +31,10 @@ impl<'a> Project<'a> { | |||
32 | fixture, | 31 | fixture, |
33 | tmp_dir: None, | 32 | tmp_dir: None, |
34 | roots: vec![], | 33 | roots: vec![], |
35 | with_sysroot: false, | 34 | config: serde_json::json!({ |
36 | config: serde_json::Value::Null, | 35 | // Loading standard library is costly, let's ignore it by default |
36 | "cargo": { "noSysroot": true } | ||
37 | }), | ||
37 | } | 38 | } |
38 | } | 39 | } |
39 | 40 | ||
@@ -47,11 +48,6 @@ impl<'a> Project<'a> { | |||
47 | self | 48 | self |
48 | } | 49 | } |
49 | 50 | ||
50 | pub(crate) fn with_sysroot(mut self, yes: bool) -> Project<'a> { | ||
51 | self.with_sysroot = yes; | ||
52 | self | ||
53 | } | ||
54 | |||
55 | pub(crate) fn with_config(mut self, config: serde_json::Value) -> Project<'a> { | 51 | pub(crate) fn with_config(mut self, config: serde_json::Value) -> Project<'a> { |
56 | self.config = config; | 52 | self.config = config; |
57 | self | 53 | self |
diff --git a/crates/syntax/Cargo.toml b/crates/syntax/Cargo.toml index e41171b57..d836c5d1a 100644 --- a/crates/syntax/Cargo.toml +++ b/crates/syntax/Cargo.toml | |||
@@ -13,7 +13,7 @@ doctest = false | |||
13 | [dependencies] | 13 | [dependencies] |
14 | itertools = "0.10.0" | 14 | itertools = "0.10.0" |
15 | rowan = "0.12.2" | 15 | rowan = "0.12.2" |
16 | rustc_lexer = { version = "707.0.0", package = "rustc-ap-rustc_lexer" } | 16 | rustc_lexer = { version = "708.0.0", package = "rustc-ap-rustc_lexer" } |
17 | rustc-hash = "1.1.0" | 17 | rustc-hash = "1.1.0" |
18 | arrayvec = "0.5.1" | 18 | arrayvec = "0.5.1" |
19 | once_cell = "1.3.1" | 19 | once_cell = "1.3.1" |
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index 5eee33545..b6c5de658 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs | |||
@@ -527,8 +527,11 @@ pub mod tokens { | |||
527 | 527 | ||
528 | use crate::{ast, AstNode, Parse, SourceFile, SyntaxKind::*, SyntaxToken}; | 528 | use crate::{ast, AstNode, Parse, SourceFile, SyntaxKind::*, SyntaxToken}; |
529 | 529 | ||
530 | pub(super) static SOURCE_FILE: Lazy<Parse<SourceFile>> = | 530 | pub(super) static SOURCE_FILE: Lazy<Parse<SourceFile>> = Lazy::new(|| { |
531 | Lazy::new(|| SourceFile::parse("const C: <()>::Item = (1 != 1, 2 == 2, !true, *p)\n;\n\n")); | 531 | SourceFile::parse( |
532 | "const C: <()>::Item = (1 != 1, 2 == 2, 3 < 3, 4 <= 4, 5 > 5, 6 >= 6, !true, *p)\n;\n\n", | ||
533 | ) | ||
534 | }); | ||
532 | 535 | ||
533 | pub fn single_space() -> SyntaxToken { | 536 | pub fn single_space() -> SyntaxToken { |
534 | SOURCE_FILE | 537 | SOURCE_FILE |
diff --git a/crates/test_utils/src/bench_fixture.rs b/crates/test_utils/src/bench_fixture.rs index aa1bea9bb..d775e2cc9 100644 --- a/crates/test_utils/src/bench_fixture.rs +++ b/crates/test_utils/src/bench_fixture.rs | |||
@@ -35,3 +35,8 @@ pub fn glorious_old_parser() -> String { | |||
35 | let path = project_dir().join("bench_data/glorious_old_parser"); | 35 | let path = project_dir().join("bench_data/glorious_old_parser"); |
36 | fs::read_to_string(&path).unwrap() | 36 | fs::read_to_string(&path).unwrap() |
37 | } | 37 | } |
38 | |||
39 | pub fn numerous_macro_rules() -> String { | ||
40 | let path = project_dir().join("bench_data/numerous_macro_rules"); | ||
41 | fs::read_to_string(&path).unwrap() | ||
42 | } | ||