|
9 | 9 | using System.Reflection;
|
10 | 10 | using System.Runtime.InteropServices;
|
11 | 11 | using Microsoft.CodeAnalysis.CSharp.Symbols;
|
| 12 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
12 | 13 | using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
|
13 | 14 | using Microsoft.CodeAnalysis.Test.Utilities;
|
14 | 15 | using Roslyn.Test.Utilities;
|
@@ -1656,6 +1657,198 @@ static void verifyMetadata(ModuleSymbol module)
|
1656 | 1657 | }
|
1657 | 1658 | }
|
1658 | 1659 |
|
| 1660 | + [Fact] |
| 1661 | + public void GetDeclaredSymbol() |
| 1662 | + { |
| 1663 | + var source = (""" |
| 1664 | + partial class C |
| 1665 | + { |
| 1666 | + public partial event System.Action E, F; |
| 1667 | + public partial event System.Action E { add { } remove { } } |
| 1668 | + public partial event System.Action F { add { } remove { } } |
| 1669 | +
|
| 1670 | + public partial C(); |
| 1671 | + public partial C() { } |
| 1672 | + } |
| 1673 | + """, "Program.cs"); |
| 1674 | + |
| 1675 | + var comp = CreateCompilation(source).VerifyDiagnostics(); |
| 1676 | + var tree = comp.SyntaxTrees.Single(); |
| 1677 | + var model = comp.GetSemanticModel(tree); |
| 1678 | + |
| 1679 | + var eventDefs = tree.GetRoot().DescendantNodes().OfType<VariableDeclaratorSyntax>().ToImmutableArray(); |
| 1680 | + Assert.Equal(2, eventDefs.Length); |
| 1681 | + |
| 1682 | + var eventImpls = tree.GetRoot().DescendantNodes().OfType<EventDeclarationSyntax>().ToImmutableArray(); |
| 1683 | + Assert.Equal(2, eventImpls.Length); |
| 1684 | + |
| 1685 | + { |
| 1686 | + var defSymbol = (IEventSymbol)model.GetDeclaredSymbol(eventDefs[0])!; |
| 1687 | + Assert.Equal("event System.Action C.E", defSymbol.ToTestDisplayString()); |
| 1688 | + |
| 1689 | + IEventSymbol implSymbol = model.GetDeclaredSymbol(eventImpls[0])!; |
| 1690 | + Assert.Equal("event System.Action C.E", implSymbol.ToTestDisplayString()); |
| 1691 | + |
| 1692 | + Assert.NotEqual(defSymbol, implSymbol); |
| 1693 | + Assert.Same(implSymbol, defSymbol.PartialImplementationPart); |
| 1694 | + Assert.Same(defSymbol, implSymbol.PartialDefinitionPart); |
| 1695 | + Assert.Null(implSymbol.PartialImplementationPart); |
| 1696 | + Assert.Null(defSymbol.PartialDefinitionPart); |
| 1697 | + Assert.True(defSymbol.IsPartialDefinition); |
| 1698 | + Assert.False(implSymbol.IsPartialDefinition); |
| 1699 | + |
| 1700 | + Assert.NotEqual(defSymbol.Locations.Single(), implSymbol.Locations.Single()); |
| 1701 | + } |
| 1702 | + |
| 1703 | + { |
| 1704 | + var defSymbol = (IEventSymbol)model.GetDeclaredSymbol(eventDefs[1])!; |
| 1705 | + Assert.Equal("event System.Action C.F", defSymbol.ToTestDisplayString()); |
| 1706 | + |
| 1707 | + IEventSymbol implSymbol = model.GetDeclaredSymbol(eventImpls[1])!; |
| 1708 | + Assert.Equal("event System.Action C.F", implSymbol.ToTestDisplayString()); |
| 1709 | + |
| 1710 | + Assert.NotEqual(defSymbol, implSymbol); |
| 1711 | + Assert.Same(implSymbol, defSymbol.PartialImplementationPart); |
| 1712 | + Assert.Same(defSymbol, implSymbol.PartialDefinitionPart); |
| 1713 | + Assert.Null(implSymbol.PartialImplementationPart); |
| 1714 | + Assert.Null(defSymbol.PartialDefinitionPart); |
| 1715 | + Assert.True(defSymbol.IsPartialDefinition); |
| 1716 | + Assert.False(implSymbol.IsPartialDefinition); |
| 1717 | + |
| 1718 | + Assert.NotEqual(defSymbol.Locations.Single(), implSymbol.Locations.Single()); |
| 1719 | + } |
| 1720 | + |
| 1721 | + { |
| 1722 | + var ctors = tree.GetRoot().DescendantNodes().OfType<ConstructorDeclarationSyntax>().ToImmutableArray(); |
| 1723 | + Assert.Equal(2, ctors.Length); |
| 1724 | + |
| 1725 | + IMethodSymbol defSymbol = model.GetDeclaredSymbol(ctors[0])!; |
| 1726 | + Assert.Equal("C..ctor()", defSymbol.ToTestDisplayString()); |
| 1727 | + |
| 1728 | + IMethodSymbol implSymbol = model.GetDeclaredSymbol(ctors[1])!; |
| 1729 | + Assert.Equal("C..ctor()", implSymbol.ToTestDisplayString()); |
| 1730 | + |
| 1731 | + Assert.NotEqual(defSymbol, implSymbol); |
| 1732 | + Assert.Same(implSymbol, defSymbol.PartialImplementationPart); |
| 1733 | + Assert.Same(defSymbol, implSymbol.PartialDefinitionPart); |
| 1734 | + Assert.Null(implSymbol.PartialImplementationPart); |
| 1735 | + Assert.Null(defSymbol.PartialDefinitionPart); |
| 1736 | + Assert.True(defSymbol.IsPartialDefinition); |
| 1737 | + Assert.False(implSymbol.IsPartialDefinition); |
| 1738 | + |
| 1739 | + Assert.NotEqual(defSymbol.Locations.Single(), implSymbol.Locations.Single()); |
| 1740 | + } |
| 1741 | + } |
| 1742 | + |
| 1743 | + [Fact] |
| 1744 | + public void GetDeclaredSymbol_GenericContainer() |
| 1745 | + { |
| 1746 | + var source = (""" |
| 1747 | + partial class C<T> |
| 1748 | + { |
| 1749 | + public partial event System.Action E; |
| 1750 | + public partial event System.Action E { add { } remove { } } |
| 1751 | +
|
| 1752 | + public partial C(); |
| 1753 | + public partial C() { } |
| 1754 | + } |
| 1755 | + """, "Program.cs"); |
| 1756 | + |
| 1757 | + var comp = CreateCompilation(source).VerifyDiagnostics(); |
| 1758 | + var tree = comp.SyntaxTrees.Single(); |
| 1759 | + var model = comp.GetSemanticModel(tree); |
| 1760 | + |
| 1761 | + { |
| 1762 | + var defSymbol = (IEventSymbol)model.GetDeclaredSymbol(tree.GetRoot().DescendantNodes().OfType<VariableDeclaratorSyntax>().Single())!; |
| 1763 | + Assert.Equal("event System.Action C<T>.E", defSymbol.ToTestDisplayString()); |
| 1764 | + |
| 1765 | + IEventSymbol implSymbol = model.GetDeclaredSymbol(tree.GetRoot().DescendantNodes().OfType<EventDeclarationSyntax>().Single())!; |
| 1766 | + Assert.Equal("event System.Action C<T>.E", implSymbol.ToTestDisplayString()); |
| 1767 | + |
| 1768 | + Assert.NotEqual(defSymbol, implSymbol); |
| 1769 | + Assert.Same(implSymbol, defSymbol.PartialImplementationPart); |
| 1770 | + Assert.Same(defSymbol, implSymbol.PartialDefinitionPart); |
| 1771 | + Assert.Null(implSymbol.PartialImplementationPart); |
| 1772 | + Assert.Null(defSymbol.PartialDefinitionPart); |
| 1773 | + Assert.True(defSymbol.IsPartialDefinition); |
| 1774 | + Assert.False(implSymbol.IsPartialDefinition); |
| 1775 | + |
| 1776 | + Assert.NotEqual(defSymbol.Locations.Single(), implSymbol.Locations.Single()); |
| 1777 | + |
| 1778 | + var intSymbol = comp.GetSpecialType(SpecialType.System_Int32); |
| 1779 | + var cOfTSymbol = defSymbol.ContainingType!; |
| 1780 | + var cOfIntSymbol = cOfTSymbol.Construct([intSymbol]); |
| 1781 | + |
| 1782 | + var defOfIntSymbol = (IEventSymbol)cOfIntSymbol.GetMember("E"); |
| 1783 | + Assert.Equal("event System.Action C<System.Int32>.E", defOfIntSymbol.ToTestDisplayString()); |
| 1784 | + Assert.Null(defOfIntSymbol.PartialImplementationPart); |
| 1785 | + Assert.Null(defOfIntSymbol.PartialDefinitionPart); |
| 1786 | + Assert.False(defOfIntSymbol.IsPartialDefinition); |
| 1787 | + } |
| 1788 | + |
| 1789 | + { |
| 1790 | + var ctors = tree.GetRoot().DescendantNodes().OfType<ConstructorDeclarationSyntax>().ToImmutableArray(); |
| 1791 | + Assert.Equal(2, ctors.Length); |
| 1792 | + |
| 1793 | + IMethodSymbol defSymbol = model.GetDeclaredSymbol(ctors[0])!; |
| 1794 | + Assert.Equal("C<T>..ctor()", defSymbol.ToTestDisplayString()); |
| 1795 | + |
| 1796 | + IMethodSymbol implSymbol = model.GetDeclaredSymbol(ctors[1])!; |
| 1797 | + Assert.Equal("C<T>..ctor()", implSymbol.ToTestDisplayString()); |
| 1798 | + |
| 1799 | + Assert.NotEqual(defSymbol, implSymbol); |
| 1800 | + Assert.Same(implSymbol, defSymbol.PartialImplementationPart); |
| 1801 | + Assert.Same(defSymbol, implSymbol.PartialDefinitionPart); |
| 1802 | + Assert.Null(implSymbol.PartialImplementationPart); |
| 1803 | + Assert.Null(defSymbol.PartialDefinitionPart); |
| 1804 | + Assert.True(defSymbol.IsPartialDefinition); |
| 1805 | + Assert.False(implSymbol.IsPartialDefinition); |
| 1806 | + |
| 1807 | + Assert.NotEqual(defSymbol.Locations.Single(), implSymbol.Locations.Single()); |
| 1808 | + |
| 1809 | + var intSymbol = comp.GetSpecialType(SpecialType.System_Int32); |
| 1810 | + var cOfTSymbol = defSymbol.ContainingType!; |
| 1811 | + var cOfIntSymbol = cOfTSymbol.Construct([intSymbol]); |
| 1812 | + |
| 1813 | + var defOfIntSymbol = (IMethodSymbol)cOfIntSymbol.GetMember(".ctor"); |
| 1814 | + Assert.Equal("C<System.Int32>..ctor()", defOfIntSymbol.ToTestDisplayString()); |
| 1815 | + Assert.Null(defOfIntSymbol.PartialImplementationPart); |
| 1816 | + Assert.Null(defOfIntSymbol.PartialDefinitionPart); |
| 1817 | + Assert.False(defOfIntSymbol.IsPartialDefinition); |
| 1818 | + } |
| 1819 | + } |
| 1820 | + |
| 1821 | + [Fact] |
| 1822 | + public void GetDeclaredSymbol_ConstructorParameter() |
| 1823 | + { |
| 1824 | + var source = (""" |
| 1825 | + partial class C |
| 1826 | + { |
| 1827 | + public partial C(int i); |
| 1828 | + public partial C(int i) { } |
| 1829 | + } |
| 1830 | + """, "Program.cs"); |
| 1831 | + |
| 1832 | + var comp = CreateCompilation(source).VerifyDiagnostics(); |
| 1833 | + var tree = comp.SyntaxTrees.Single(); |
| 1834 | + var model = comp.GetSemanticModel(tree); |
| 1835 | + |
| 1836 | + var parameters = tree.GetRoot().DescendantNodes().OfType<ParameterSyntax>().ToArray(); |
| 1837 | + Assert.Equal(2, parameters.Length); |
| 1838 | + |
| 1839 | + IParameterSymbol defSymbol = model.GetDeclaredSymbol(parameters[0])!; |
| 1840 | + Assert.Equal("System.Int32 i", defSymbol.ToTestDisplayString()); |
| 1841 | + |
| 1842 | + IParameterSymbol implSymbol = model.GetDeclaredSymbol(parameters[1])!; |
| 1843 | + Assert.Equal("System.Int32 i", implSymbol.ToTestDisplayString()); |
| 1844 | + |
| 1845 | + Assert.NotEqual(defSymbol, implSymbol); |
| 1846 | + Assert.Same(implSymbol, ((IMethodSymbol)defSymbol.ContainingSymbol).PartialImplementationPart!.Parameters.Single()); |
| 1847 | + Assert.Same(defSymbol, ((IMethodSymbol)implSymbol.ContainingSymbol).PartialDefinitionPart!.Parameters.Single()); |
| 1848 | + |
| 1849 | + Assert.NotEqual(defSymbol.Locations.Single(), implSymbol.Locations.Single()); |
| 1850 | + } |
| 1851 | + |
1659 | 1852 | [Fact]
|
1660 | 1853 | public void SequencePoints()
|
1661 | 1854 | {
|
|
0 commit comments