-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Resolving ILLink warnings on System.Private.Xml (Part 1) #49413
Changes from all commits
538bb29
f688780
7cbd958
8b57fe5
6656141
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -196,13 +196,14 @@ private QilExpression Compile(Compiler compiler) | |
} | ||
|
||
// Create list of all early bound objects | ||
Dictionary<string, Type?> scriptClasses = compiler.Scripts.ScriptClasses; | ||
Scripts.TrimSafeDictionary scriptClasses = compiler.Scripts.ScriptClasses; | ||
List<EarlyBoundInfo> ebTypes = new List<EarlyBoundInfo>(scriptClasses.Count); | ||
foreach (KeyValuePair<string, Type?> pair in scriptClasses) | ||
foreach (string key in scriptClasses.Keys) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the change here to enumerate the keys and then index into the dictionary? Why not just expose There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem here is that if we keep it as it was, then we would get a warning (even when using TrimmerSafeDictionary) since the Type would be comming form method: |
||
{ | ||
if (pair.Value != null) | ||
Type? value = scriptClasses[key]; | ||
if (value != null) | ||
{ | ||
ebTypes.Add(new EarlyBoundInfo(pair.Key, pair.Value)); | ||
ebTypes.Add(new EarlyBoundInfo(key, value)); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,23 +4,25 @@ | |
// <spec>http://devdiv/Documents/Whidbey/CLR/CurrentSpecs/BCL/CodeDom%20Activation.doc</spec> | ||
//------------------------------------------------------------------------------ | ||
|
||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Xml.Xsl.Runtime; | ||
|
||
namespace System.Xml.Xsl.Xslt | ||
{ | ||
internal class Scripts | ||
{ | ||
private readonly Compiler _compiler; | ||
private readonly Dictionary<string, Type?> _nsToType = new Dictionary<string, Type?>(); | ||
private readonly TrimSafeDictionary _nsToType = new TrimSafeDictionary(); | ||
private readonly XmlExtensionFunctionTable _extFuncs = new XmlExtensionFunctionTable(); | ||
|
||
public Scripts(Compiler compiler) | ||
{ | ||
_compiler = compiler; | ||
} | ||
|
||
public Dictionary<string, Type?> ScriptClasses | ||
public TrimSafeDictionary ScriptClasses | ||
{ | ||
get { return _nsToType; } | ||
} | ||
|
@@ -41,5 +43,29 @@ public Scripts(Compiler compiler) | |
} | ||
return null; | ||
} | ||
|
||
internal class TrimSafeDictionary | ||
{ | ||
private readonly Dictionary<string, Type?> _backingDictionary = new Dictionary<string, Type?>(); | ||
|
||
public Type? this[string key] | ||
{ | ||
[UnconditionalSuppressMessage("TrimAnalysis", "IL2073:MissingDynamicallyAccessedMembers", | ||
Justification = "The getter of the dictionary is not annotated to preserve the constructor, but the sources that are adding the items to " + | ||
"the dictionary are annotated so we can supress the message as we know the constructor will be preserved.")] | ||
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we just put the attribute on the whole property? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Chatted offline, this is a problem specific to Indexers and have logged the following issue for investigation tracking: dotnet/linker#1902 |
||
get => _backingDictionary[key]; | ||
[param: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] | ||
set => _backingDictionary[key] = value; | ||
} | ||
|
||
public ICollection<string> Keys => _backingDictionary.Keys; | ||
|
||
public int Count => _backingDictionary.Count; | ||
|
||
public bool ContainsKey(string key) => _backingDictionary.ContainsKey(key); | ||
|
||
public bool TryGetValue(string key, [MaybeNullWhen(false)] out Type? value) => _backingDictionary.TryGetValue(key, out value); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -371,15 +371,10 @@ public Processor( | |
|
||
_scriptExtensions = new Hashtable(_stylesheet.ScriptObjectTypes.Count); | ||
{ | ||
foreach (DictionaryEntry entry in _stylesheet.ScriptObjectTypes) | ||
// Scripts are not supported on stylesheets | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need a test for this or something? Are we concerned about the behavior change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a functional change as far as I can tell - we should have the proper tests and get it reviewed by the area owners. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, @krwq AFAICT Script objects on xsls are not supported at all in .NET Core. I tried with several different approaches trying to create an xsl that had scripts and all entrypoints that I found trhough XslCompiledTransform would all eventually throw PNSE. I think that this has been dead code that we just originally imported from .NET Framework, but never really did anything as it throws PNSE when you actually invoke the transform operation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIRC that's correct and intentional. @joperezr you can double check by creating an xslt which works on full fx and make sure it throws on 5.0 - check for all types of scripts. |
||
if (_stylesheet.ScriptObjectTypes.Count > 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. might be a good idea to also simplify parsing this to save even more bytes |
||
{ | ||
string namespaceUri = (string)entry.Key; | ||
if (GetExtensionObject(namespaceUri) != null) | ||
{ | ||
throw XsltException.Create(SR.Xslt_ScriptDub, namespaceUri); | ||
} | ||
_scriptExtensions.Add(namespaceUri, Activator.CreateInstance((Type)entry.Value!, | ||
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, null, null)); | ||
throw new PlatformNotSupportedException(SR.CompilingScriptsNotSupported); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it worth/possible to add a test here? I'm not asking for a bunch of work, but if it is reasonable (say an hour) it might be nice to ensure this scenario throws PNSE correctly. #Resolved There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I can add a unit test for this. This is actually addressing a warning on the old xslt which is not currently covered by this PR (except for this one) but I can add a test here that ensures that using scripts on old xslt throws PNSE at the compile stage which is earlier than this code comes into play. #Resolved |
||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(minor) I assume we don't need to worry about the size of these objects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@krwq do you know if that's the case? Also, do you know if EarlyBoundInfos get used for anything other than Scripts in Xsls? Because if that's the case, we can potentially just remove this whole type as it would be currently just dead code.