Skip to content

Commit

Permalink
Small fixes to source generator (#3466)
Browse files Browse the repository at this point in the history
  • Loading branch information
papafe authored Oct 31, 2023
1 parent d13be03 commit 10aaf6a
Show file tree
Hide file tree
Showing 144 changed files with 180 additions and 141 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Made WebSocket error logging more verbose when using `AppConfiguration.UseManagedWebSockets = true`. [#3459](https://github.com/realm/realm-dotnet/pull/3459)

### Fixed
* None
* Added an error that is raised when interface based Realm classes are used with a language version lower than 8.0. At the same time, removed the use of `not` in the generated code, so that it's compatible with a minumum C# version of 8.0. (Issue [#3265](https://github.com/realm/realm-dotnet/issues/3265))

### Compatibility
* Realm Studio: 13.0.0 or later.
Expand Down
2 changes: 1 addition & 1 deletion Realm/Realm.SourceGenerator/ClassCodeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ public override bool Equals(object? obj)
return !IsValid;
}
if (obj is not Realms.IRealmObjectBase iro)
if (!(obj is Realms.IRealmObjectBase iro))
{
return false;
}
Expand Down
12 changes: 11 additions & 1 deletion Realm/Realm.SourceGenerator/Diagnostics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ private enum Id
IndexedPrimaryKey = 28,
InvalidCollectionInitializer = 29,
InvalidCollectionInitializerInCtor = 30,
OldCSharpVersion = 100,
InvalidGeneratorConfiguration = 1000,
}

Expand All @@ -67,6 +68,15 @@ public static Diagnostic InvalidConfiguration(string field, string description)
Location.None);
}

public static Diagnostic OldCSharpVersion()
{
return CreateDiagnosticError(
Id.OldCSharpVersion,
"Unsupported version of C#",
$"It is not possible to use the Realm source generator with C# versions older than 8.0.",
Location.None);
}

public static Diagnostic UnexpectedError(string className, string message, string stackTrace)
{
return CreateDiagnosticError(
Expand All @@ -82,7 +92,7 @@ public static Diagnostic ClassUnclearDefinition(string className, Location locat
return CreateDiagnosticError(
Id.ClassUnclearDefinition,
"Realm classes cannot implement multiple class interfaces",
$"Class {className} is declared as implementing multiple class interfaces.A class can implement only one interface between IRealmObject, IEmbeddedObject, IAsymmetricObject.",
$"Class {className} is declared as implementing multiple class interfaces. A class can implement only one interface between IRealmObject, IEmbeddedObject, IAsymmetricObject.",
location);
}

Expand Down
2 changes: 2 additions & 0 deletions Realm/Realm.SourceGenerator/DiagnosticsEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public void Emit(ParsingResults parsingResults)
throw;
}
}

parsingResults.GeneralDiagnostics.ForEach(_context.ReportDiagnostic);
}

private static void SerializeDiagnostics(GeneratorExecutionContext context, ClassInfo classInfo)
Expand Down
10 changes: 10 additions & 0 deletions Realm/Realm.SourceGenerator/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ public ParsingResults Parse(IEnumerable<RealmClassDefinition> realmClasses)
var classNames = new HashSet<string>();
var duplicateClassNames = new HashSet<string>();

if (realmClasses.Any() &&
_context.Compilation is CSharpCompilation comp &&
comp.LanguageVersion < LanguageVersion.CSharp8)
{
result.GeneralDiagnostics.Add(Diagnostics.OldCSharpVersion());
return result;
}

foreach (var rc in realmClasses)
{
var classSymbol = rc.ClassSymbol;
Expand Down Expand Up @@ -556,5 +564,7 @@ private static string RemoveUsingKeyword(UsingDirectiveSyntax syntax)
internal record ParsingResults
{
public List<ClassInfo> ClassInfo { get; } = new();

public List<Diagnostic> GeneralDiagnostics { get; } = new();
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 10aaf6a

Please sign in to comment.