From 15434aa2a8ff7d170d1a55810d46a4f5aa8c36e1 Mon Sep 17 00:00:00 2001
From: rschoene <rene.schoene@tu-dresden.de>
Date: Mon, 21 Oct 2019 14:06:00 +0200
Subject: [PATCH] Add conflicting type for invalid redefinition errors.

---
 src/main/jastadd/Analysis.jrag                | 20 +++++++++++--------
 src/main/jastadd/Errors.jrag                  |  6 ++++--
 src/test/jastadd/errors/Inheritance.expected  |  4 ++--
 .../jastadd/errors/InheritanceLeft.expected   |  4 ++--
 4 files changed, 20 insertions(+), 14 deletions(-)

diff --git a/src/main/jastadd/Analysis.jrag b/src/main/jastadd/Analysis.jrag
index 89d6fb0..27f3a7d 100644
--- a/src/main/jastadd/Analysis.jrag
+++ b/src/main/jastadd/Analysis.jrag
@@ -76,26 +76,30 @@ aspect ComponentAnalysis {
     return null;
   }
 
-  /* Should this better return the component, where it is defined previously defined? */
   //--- isInvalidRedefinition ---
   /**
    * Check, if a component with the same name is already declared in some supertype
    */
-  syn boolean Component.isInvalidRedefinition() = false;
-  eq TokenComponent.isInvalidRedefinition() = hasInvalidRedefinitionOn(enclosingTypeDecl());
-  eq RelationComponent.isInvalidRedefinition() = hasInvalidRedefinitionOn(getTypeUse().decl());
+  syn boolean Component.isInvalidRedefinition() = invalidRedefinition() != null;
 
-  syn boolean Component.hasInvalidRedefinitionOn(TypeDecl td) {
-    if (td == null) return false;
+  /**
+   * Check, if a component with the same name is already declared in some supertype, and return it, if any
+   */
+  syn Component Component.invalidRedefinition() = null;
+  eq TokenComponent.invalidRedefinition() = invalidRedefinitionOn(enclosingTypeDecl());
+  eq RelationComponent.invalidRedefinition() = invalidRedefinitionOn(getTypeUse().decl());
+
+  syn Component Component.invalidRedefinitionOn(TypeDecl td) {
+    if (td == null) return null;
 
     while (td.hasSuper() && td.getSuper().decl() != null) {
       td = td.getSuper().decl();
       // find a matching component on the RHS of the (current) super type
       Component c = findComponent(td, getID());
 
-      if (c != null && !this.isEqual(c)) return true;
+      if (c != null && !this.isEqual(c)) return c;
     }
-    return false;
+    return null;
   }
 
   //--- isEqual ---
diff --git a/src/main/jastadd/Errors.jrag b/src/main/jastadd/Errors.jrag
index 0ff888a..b1ec073 100644
--- a/src/main/jastadd/Errors.jrag
+++ b/src/main/jastadd/Errors.jrag
@@ -21,12 +21,14 @@ aspect Errors {
     to Program.errors();
 
   RelationComponent contributes error("Role '" + name()
-    + "' is an invalid redefinition for type '" + getTypeUse().decl() + "'")
+    + "' is an invalid redefinition for type '" + getTypeUse().decl() + "', conflicts with supertype '"
+    + invalidRedefinition().enclosingTypeDecl() + "'")
     when isInvalidRedefinition()
     to Program.errors();
 
   TokenComponent contributes error("Token '" + name()
-    + "' is an invalid redefinition for type '" + enclosingTypeDecl() + "'")
+    + "' is an invalid redefinition for type '" + enclosingTypeDecl() + "', conflicts with supertype '"
+    + invalidRedefinition().enclosingTypeDecl() + "'")
     when isInvalidRedefinition()
     to Program.errors();
 
diff --git a/src/test/jastadd/errors/Inheritance.expected b/src/test/jastadd/errors/Inheritance.expected
index 3742ef5..b3382e0 100644
--- a/src/test/jastadd/errors/Inheritance.expected
+++ b/src/test/jastadd/errors/Inheritance.expected
@@ -1,3 +1,3 @@
 Errors:
-$FILENAME Line 4, column 12: Token 'X' is an invalid redefinition for type 'B3'
-$FILENAME Line 7, column 5: Role 'X' is an invalid redefinition for type 'B2'
+$FILENAME Line 4, column 12: Token 'X' is an invalid redefinition for type 'B3', conflicts with supertype 'A'
+$FILENAME Line 7, column 5: Role 'X' is an invalid redefinition for type 'B2', conflicts with supertype 'A'
diff --git a/src/test/jastadd/errors/InheritanceLeft.expected b/src/test/jastadd/errors/InheritanceLeft.expected
index e94e4ef..a897e47 100644
--- a/src/test/jastadd/errors/InheritanceLeft.expected
+++ b/src/test/jastadd/errors/InheritanceLeft.expected
@@ -1,3 +1,3 @@
 Errors:
-$FILENAME Line 4, column 12: Token 'X' is an invalid redefinition for type 'B3'
-$FILENAME Line 7, column 10: Role 'X' is an invalid redefinition for type 'B2'
+$FILENAME Line 4, column 12: Token 'X' is an invalid redefinition for type 'B3', conflicts with supertype 'A'
+$FILENAME Line 7, column 10: Role 'X' is an invalid redefinition for type 'B2', conflicts with supertype 'A'
-- 
GitLab